put sequence header if needed */
| 147 | |
| 148 | /* put sequence header if needed */ |
| 149 | static void mpeg1_encode_sequence_header(MPEG12EncContext *mpeg12) |
| 150 | { |
| 151 | MPVEncContext *const s = &mpeg12->mpeg.s; |
| 152 | unsigned int vbv_buffer_size, fps, v; |
| 153 | int constraint_parameter_flag; |
| 154 | AVRational framerate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index]; |
| 155 | uint64_t time_code; |
| 156 | int64_t best_aspect_error = INT64_MAX; |
| 157 | AVRational aspect_ratio = s->c.avctx->sample_aspect_ratio; |
| 158 | int aspect_ratio_info; |
| 159 | |
| 160 | put_bits_assume_flushed(&s->pb); |
| 161 | |
| 162 | if (!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)) |
| 163 | return; |
| 164 | |
| 165 | if (aspect_ratio.num == 0 || aspect_ratio.den == 0) |
| 166 | aspect_ratio = (AVRational){1,1}; // pixel aspect 1.1 (VGA) |
| 167 | |
| 168 | /* MPEG-1 header repeated every GOP */ |
| 169 | put_header(s, SEQ_START_CODE); |
| 170 | |
| 171 | put_sbits(&s->pb, 12, s->c.width & 0xFFF); |
| 172 | put_sbits(&s->pb, 12, s->c.height & 0xFFF); |
| 173 | |
| 174 | for (int i = 1; i < 15; i++) { |
| 175 | int64_t error = aspect_ratio.num * (1LL<<32) / aspect_ratio.den; |
| 176 | if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1) |
| 177 | error -= (1LL<<32) / ff_mpeg1_aspect[i]; |
| 178 | else |
| 179 | error -= (1LL<<32)*ff_mpeg2_aspect[i].num * s->c.height / s->c.width / ff_mpeg2_aspect[i].den; |
| 180 | |
| 181 | error = FFABS(error); |
| 182 | |
| 183 | if (error - 2 <= best_aspect_error) { |
| 184 | best_aspect_error = error; |
| 185 | aspect_ratio_info = i; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | put_bits(&s->pb, 4, aspect_ratio_info); |
| 190 | put_bits(&s->pb, 4, mpeg12->frame_rate_index); |
| 191 | |
| 192 | if (s->c.avctx->rc_max_rate) { |
| 193 | v = (s->c.avctx->rc_max_rate + 399) / 400; |
| 194 | if (v > 0x3ffff && s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO) |
| 195 | v = 0x3ffff; |
| 196 | } else { |
| 197 | v = 0x3FFFF; |
| 198 | } |
| 199 | |
| 200 | if (s->c.avctx->rc_buffer_size) |
| 201 | vbv_buffer_size = s->c.avctx->rc_buffer_size; |
| 202 | else |
| 203 | /* VBV calculation: Scaled so that a VCD has the proper |
| 204 | * VBV size of 40 kilobytes */ |
| 205 | vbv_buffer_size = av_rescale_rnd(mpeg12->mpeg.bit_rate, 20, 1151929 / 2, AV_ROUND_ZERO) * 8 * 1024; |
| 206 | vbv_buffer_size = (vbv_buffer_size + 16383) / 16384; |
no test coverage detected