FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12enc.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 632 711 88.9%
Functions: 16 16 100.0%
Branches: 354 435 81.4%

Line Branch Exec Source
1 /*
2 * MPEG-1/2 encoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * MPEG-1/2 encoder
26 */
27
28 #include <assert.h>
29 #include <stdint.h>
30
31 #include "config.h"
32 #include "config_components.h"
33 #include "libavutil/attributes.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/log.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/thread.h"
38 #include "libavutil/timecode.h"
39 #include "libavutil/stereo3d.h"
40
41 #include "avcodec.h"
42 #include "codec_internal.h"
43 #include "mathops.h"
44 #include "mpeg12.h"
45 #include "mpeg12data.h"
46 #include "mpeg12enc.h"
47 #include "mpeg12vlc.h"
48 #include "mpegutils.h"
49 #include "mpegvideo.h"
50 #include "mpegvideodata.h"
51 #include "mpegvideoenc.h"
52 #include "profiles.h"
53 #include "put_bits.h"
54 #include "rl.h"
55
56 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
57 static const uint8_t svcd_scan_offset_placeholder[] = {
58 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
59 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 };
61
62 static uint8_t mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1];
63 static uint8_t fcode_tab[MAX_MV * 2 + 1];
64
65 static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2];
66 static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2];
67
68 static uint8_t mpeg12_max_level[MAX_LEVEL + 1];
69 static uint8_t mpeg12_index_run[MAX_RUN + 1];
70
71 /* simple include everything table for dc, first byte is bits
72 * number next 3 are code */
73 static uint32_t mpeg1_lum_dc_uni[512];
74 static uint32_t mpeg1_chr_dc_uni[512];
75
76 typedef struct MPEG12EncContext {
77 MPVMainEncContext mpeg;
78 AVRational frame_rate_ext;
79 unsigned frame_rate_index;
80
81 int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num
82
83 int64_t timecode_frame_start; ///< GOP timecode frame start number, in non drop frame format
84 AVTimecode tc; ///< timecode context
85 char *tc_opt_str; ///< timecode option string
86 int drop_frame_timecode; ///< timecode is in drop frame format.
87 int scan_offset; ///< reserve space for SVCD scan offset user data.
88
89 int a53_cc;
90 int seq_disp_ext;
91 int video_format;
92 #define VIDEO_FORMAT_COMPONENT 0
93 #define VIDEO_FORMAT_PAL 1
94 #define VIDEO_FORMAT_NTSC 2
95 #define VIDEO_FORMAT_SECAM 3
96 #define VIDEO_FORMAT_MAC 4
97 #define VIDEO_FORMAT_UNSPECIFIED 5
98 } MPEG12EncContext;
99
100 #define A53_MAX_CC_COUNT 0x1f
101 #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */
102
103 137 av_cold void ff_mpeg1_init_uni_ac_vlc(const int8_t max_level[],
104 const uint8_t index_run[],
105 const uint16_t table_vlc[][2],
106 uint8_t uni_ac_vlc_len[])
107 {
108 int i;
109
110
2/2
✓ Branch 0 taken 17536 times.
✓ Branch 1 taken 137 times.
17673 for (i = 0; i < 128; i++) {
111 17536 int level = i - 64;
112 int run;
113
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 17399 times.
17536 if (!level)
114 137 continue;
115
2/2
✓ Branch 0 taken 1113536 times.
✓ Branch 1 taken 17399 times.
1130935 for (run = 0; run < 64; run++) {
116 int len, code;
117 1113536 int alevel = FFABS(level);
118
119
2/2
✓ Branch 0 taken 1082942 times.
✓ Branch 1 taken 30594 times.
1113536 if (alevel > max_level[run])
120 1082942 code = 111; /* rl->n */
121 else
122 30594 code = index_run[run] + alevel - 1;
123
124
2/2
✓ Branch 0 taken 30414 times.
✓ Branch 1 taken 1083122 times.
1113536 if (code < 111) { /* rl->n */
125 /* length of VLC and sign */
126 30414 len = table_vlc[code][1] + 1;
127 } else {
128 1083122 len = table_vlc[MPEG12_RL_NB_ELEMS][1] + 6;
129
130
1/2
✓ Branch 0 taken 1083122 times.
✗ Branch 1 not taken.
1083122 if (alevel < 128)
131 1083122 len += 8;
132 else
133 len += 16;
134 }
135
136 1113536 uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
137 }
138 }
139 137 }
140
141 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
142 325205 static void put_header(MPVEncContext *const s, uint32_t header)
143 {
144 325205 align_put_bits(&s->pb);
145 325205 put_bits32(&s->pb, header);
146 325205 }
147
148 /* put sequence header if needed */
149 5162 static void mpeg1_encode_sequence_header(MPEG12EncContext *mpeg12)
150 {
151 5162 MPVEncContext *const s = &mpeg12->mpeg.s;
152 unsigned int vbv_buffer_size, fps, v;
153 int constraint_parameter_flag;
154 5162 AVRational framerate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index];
155 uint64_t time_code;
156 5162 int64_t best_aspect_error = INT64_MAX;
157 5162 AVRational aspect_ratio = s->c.avctx->sample_aspect_ratio;
158 int aspect_ratio_info;
159
160 5162 put_bits_assume_flushed(&s->pb);
161
162
2/2
✓ Branch 0 taken 4699 times.
✓ Branch 1 taken 463 times.
5162 if (!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY))
163 4699 return;
164
165
3/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 411 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
463 if (aspect_ratio.num == 0 || aspect_ratio.den == 0)
166 411 aspect_ratio = (AVRational){1,1}; // pixel aspect 1.1 (VGA)
167
168 /* MPEG-1 header repeated every GOP */
169 463 put_header(s, SEQ_START_CODE);
170
171 463 put_sbits(&s->pb, 12, s->c.width & 0xFFF);
172 463 put_sbits(&s->pb, 12, s->c.height & 0xFFF);
173
174
2/2
✓ Branch 0 taken 6482 times.
✓ Branch 1 taken 463 times.
6945 for (int i = 1; i < 15; i++) {
175 6482 int64_t error = aspect_ratio.num * (1LL<<32) / aspect_ratio.den;
176
4/4
✓ Branch 0 taken 5740 times.
✓ Branch 1 taken 742 times.
✓ Branch 2 taken 410 times.
✓ Branch 3 taken 5330 times.
6482 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
177 1152 error -= (1LL<<32) / ff_mpeg1_aspect[i];
178 else
179 5330 error -= (1LL<<32)*ff_mpeg2_aspect[i].num * s->c.height / s->c.width / ff_mpeg2_aspect[i].den;
180
181 6482 error = FFABS(error);
182
183
2/2
✓ Branch 0 taken 510 times.
✓ Branch 1 taken 5972 times.
6482 if (error - 2 <= best_aspect_error) {
184 510 best_aspect_error = error;
185 510 aspect_ratio_info = i;
186 }
187 }
188
189 463 put_bits(&s->pb, 4, aspect_ratio_info);
190 463 put_bits(&s->pb, 4, mpeg12->frame_rate_index);
191
192
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 310 times.
463 if (s->c.avctx->rc_max_rate) {
193 153 v = (s->c.avctx->rc_max_rate + 399) / 400;
194
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
153 if (v > 0x3ffff && s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
195 v = 0x3ffff;
196 } else {
197 310 v = 0x3FFFF;
198 }
199
200
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 310 times.
463 if (s->c.avctx->rc_buffer_size)
201 153 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 310 vbv_buffer_size = av_rescale_rnd(mpeg12->mpeg.bit_rate, 20, 1151929 / 2, AV_ROUND_ZERO) * 8 * 1024;
206 463 vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
207
208 463 put_sbits(&s->pb, 18, v);
209 463 put_bits(&s->pb, 1, 1); // marker
210 463 put_sbits(&s->pb, 10, vbv_buffer_size);
211
212 463 constraint_parameter_flag =
213 923 s->c.width <= 768 &&
214
2/2
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 150 times.
460 s->c.height <= 576 &&
215
2/2
✓ Branch 0 taken 287 times.
✓ Branch 1 taken 23 times.
310 s->c.mb_width * s->c.mb_height <= 396 &&
216
2/2
✓ Branch 0 taken 281 times.
✓ Branch 1 taken 6 times.
287 s->c.mb_width * s->c.mb_height * framerate.num <= 396 * 25 * framerate.den &&
217
1/2
✓ Branch 0 taken 281 times.
✗ Branch 1 not taken.
281 framerate.num <= framerate.den * 30 &&
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281 times.
281 s->c.avctx->me_range &&
219 s->c.avctx->me_range < 128 &&
220 vbv_buffer_size <= 20 &&
221
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 3 times.
923 v <= 1856000 / 400 &&
222 s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO;
223
224 463 put_bits(&s->pb, 1, constraint_parameter_flag);
225
226 463 ff_write_quant_matrix(&s->pb, s->c.avctx->intra_matrix);
227 463 ff_write_quant_matrix(&s->pb, s->c.avctx->inter_matrix);
228
229
2/2
✓ Branch 0 taken 410 times.
✓ Branch 1 taken 53 times.
463 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
230 const AVFrameSideData *side_data;
231 410 int width = s->c.width;
232 410 int height = s->c.height;
233 int use_seq_disp_ext;
234
235 410 put_header(s, EXT_START_CODE);
236 410 put_bits(&s->pb, 4, 1); // seq ext
237
238 410 put_bits(&s->pb, 1, s->c.avctx->profile == AV_PROFILE_MPEG2_422); // escx 1 for 4:2:2 profile
239
240 410 put_bits(&s->pb, 3, s->c.avctx->profile); // profile
241 410 put_bits(&s->pb, 4, s->c.avctx->level); // level
242
243 410 put_bits(&s->pb, 1, s->c.progressive_sequence);
244 410 put_bits(&s->pb, 2, s->c.chroma_format);
245 410 put_bits(&s->pb, 2, s->c.width >> 12);
246 410 put_bits(&s->pb, 2, s->c.height >> 12);
247 410 put_bits(&s->pb, 12, v >> 18); // bitrate ext
248 410 put_bits(&s->pb, 1, 1); // marker
249 410 put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
250 410 put_bits(&s->pb, 1, s->c.low_delay);
251 410 put_bits(&s->pb, 2, mpeg12->frame_rate_ext.num-1); // frame_rate_ext_n
252 410 put_bits(&s->pb, 5, mpeg12->frame_rate_ext.den-1); // frame_rate_ext_d
253
254 410 side_data = av_frame_get_side_data(s->c.cur_pic.ptr->f, AV_FRAME_DATA_PANSCAN);
255
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 405 times.
410 if (side_data) {
256 5 const AVPanScan *pan_scan = (AVPanScan *)side_data->data;
257
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (pan_scan->width && pan_scan->height) {
258 5 width = pan_scan->width >> 4;
259 5 height = pan_scan->height >> 4;
260 }
261 }
262
263 1230 use_seq_disp_ext = (width != s->c.width ||
264
1/2
✓ Branch 0 taken 410 times.
✗ Branch 1 not taken.
410 height != s->c.height ||
265
2/2
✓ Branch 0 taken 401 times.
✓ Branch 1 taken 9 times.
410 s->c.avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
266
2/2
✓ Branch 0 taken 398 times.
✓ Branch 1 taken 3 times.
401 s->c.avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
267
3/4
✓ Branch 0 taken 410 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 372 times.
✓ Branch 3 taken 26 times.
1192 s->c.avctx->colorspace != AVCOL_SPC_UNSPECIFIED ||
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 372 times.
372 mpeg12->video_format != VIDEO_FORMAT_UNSPECIFIED);
269
270
1/2
✓ Branch 0 taken 410 times.
✗ Branch 1 not taken.
410 if (mpeg12->seq_disp_ext == 1 ||
271
3/4
✓ Branch 0 taken 410 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 372 times.
410 (mpeg12->seq_disp_ext == -1 && use_seq_disp_ext)) {
272 38 put_header(s, EXT_START_CODE);
273 38 put_bits(&s->pb, 4, 2); // sequence display extension
274 38 put_bits(&s->pb, 3, mpeg12->video_format); // video_format
275 38 put_bits(&s->pb, 1, 1); // colour_description
276 38 put_bits(&s->pb, 8, s->c.avctx->color_primaries); // colour_primaries
277 38 put_bits(&s->pb, 8, s->c.avctx->color_trc); // transfer_characteristics
278 38 put_bits(&s->pb, 8, s->c.avctx->colorspace); // matrix_coefficients
279 38 put_bits(&s->pb, 14, width); // display_horizontal_size
280 38 put_bits(&s->pb, 1, 1); // marker_bit
281 38 put_bits(&s->pb, 14, height); // display_vertical_size
282 38 put_bits(&s->pb, 3, 0); // remaining 3 bits are zero padding
283 }
284 }
285
286 463 put_header(s, GOP_START_CODE);
287 463 put_bits(&s->pb, 1, mpeg12->drop_frame_timecode); // drop frame flag
288 /* time code: we must convert from the real frame rate to a
289 * fake MPEG frame rate in case of low frame rate */
290 463 fps = (framerate.num + framerate.den / 2) / framerate.den;
291 463 time_code = s->c.cur_pic.ptr->coded_picture_number +
292 463 mpeg12->timecode_frame_start;
293
294 463 mpeg12->gop_picture_number = s->c.cur_pic.ptr->coded_picture_number;
295
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 463 times.
463 av_assert0(mpeg12->drop_frame_timecode == !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
297
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 454 times.
463 if (mpeg12->drop_frame_timecode)
298 9 time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
299
300 463 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
301 463 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
302 463 put_bits(&s->pb, 1, 1);
303 463 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
304 463 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
305 463 put_bits(&s->pb, 1, !!(s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) ||
306
5/6
✓ Branch 0 taken 463 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 295 times.
✓ Branch 3 taken 168 times.
✓ Branch 4 taken 57 times.
✓ Branch 5 taken 238 times.
463 mpeg12->mpeg.intra_only || !mpeg12->gop_picture_number);
307 463 put_bits(&s->pb, 1, 0); // broken link
308 }
309
310 1814369 static inline void encode_mb_skip_run(MPVEncContext *const s, int run)
311 {
312
2/2
✓ Branch 0 taken 69199 times.
✓ Branch 1 taken 1814369 times.
1883568 while (run >= 33) {
313 69199 put_bits(&s->pb, 11, 0x008);
314 69199 run -= 33;
315 }
316 1814369 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
317 1814369 ff_mpeg12_mbAddrIncrTable[run][0]);
318 1814369 }
319
320 496011 static av_always_inline void put_qscale(MPVEncContext *const s)
321 {
322 496011 put_bits(&s->pb, 5, s->c.qscale);
323 496011 }
324
325 313853 void ff_mpeg1_encode_slice_header(MPVEncContext *const s)
326 {
327
3/4
✓ Branch 0 taken 306580 times.
✓ Branch 1 taken 7273 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 306580 times.
313853 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO && s->c.height > 2800) {
328 put_header(s, SLICE_MIN_START_CODE + (s->c.mb_y & 127));
329 /* slice_vertical_position_extension */
330 put_bits(&s->pb, 3, s->c.mb_y >> 7);
331 } else {
332 av_assert1(s->c.mb_y <= SLICE_MAX_START_CODE - SLICE_MIN_START_CODE);
333 313853 put_header(s, SLICE_MIN_START_CODE + s->c.mb_y);
334 }
335 313853 put_qscale(s);
336 /* slice extra information */
337 313853 put_bits(&s->pb, 1, 0);
338 313853 }
339
340 5162 static int mpeg1_encode_picture_header(MPVMainEncContext *const m)
341 {
342 5162 MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)m;
343 5162 MPVEncContext *const s = &m->s;
344 const AVFrameSideData *side_data;
345
346 5162 put_bits_assume_flushed(&s->pb);
347
348 5162 mpeg1_encode_sequence_header(mpeg12);
349
350 /* MPEG-1 picture header */
351 5162 put_header(s, PICTURE_START_CODE);
352 /* temporal reference */
353
354 5162 put_bits(&s->pb, 10,
355 5162 (s->picture_number - mpeg12->gop_picture_number) & 0x3ff);
356 5162 put_bits(&s->pb, 3, s->c.pict_type);
357
358 5162 m->vbv_delay_pos = put_bytes_count(&s->pb, 0);
359 5162 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
360
361 // RAL: Forward f_code also needed for B-frames
362
2/2
✓ Branch 0 taken 1170 times.
✓ Branch 1 taken 3992 times.
5162 if (s->c.pict_type == AV_PICTURE_TYPE_P ||
363
2/2
✓ Branch 0 taken 707 times.
✓ Branch 1 taken 463 times.
1170 s->c.pict_type == AV_PICTURE_TYPE_B) {
364 4699 put_bits(&s->pb, 1, 0); /* half pel coordinates */
365
2/2
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 4272 times.
4699 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
366 427 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
367 else
368 4272 put_bits(&s->pb, 3, 7); /* forward_f_code */
369 }
370
371 // RAL: Backward f_code necessary for B-frames
372
2/2
✓ Branch 0 taken 707 times.
✓ Branch 1 taken 4455 times.
5162 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
373 707 put_bits(&s->pb, 1, 0); /* half pel coordinates */
374
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 563 times.
707 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
375 144 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
376 else
377 563 put_bits(&s->pb, 3, 7); /* backward_f_code */
378 }
379
380 5162 put_bits(&s->pb, 1, 0); /* extra bit picture */
381
382 5162 s->c.frame_pred_frame_dct = 1;
383
2/2
✓ Branch 0 taken 4682 times.
✓ Branch 1 taken 480 times.
5162 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
384 4682 put_header(s, EXT_START_CODE);
385 4682 put_bits(&s->pb, 4, 8); /* pic ext */
386
2/2
✓ Branch 0 taken 973 times.
✓ Branch 1 taken 3709 times.
4682 if (s->c.pict_type == AV_PICTURE_TYPE_P ||
387
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 410 times.
973 s->c.pict_type == AV_PICTURE_TYPE_B) {
388 4272 put_bits(&s->pb, 4, s->f_code);
389 4272 put_bits(&s->pb, 4, s->f_code);
390 } else {
391 410 put_bits(&s->pb, 8, 255);
392 }
393
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 4119 times.
4682 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
394 563 put_bits(&s->pb, 4, s->b_code);
395 563 put_bits(&s->pb, 4, s->b_code);
396 } else {
397 4119 put_bits(&s->pb, 8, 255);
398 }
399 4682 put_bits(&s->pb, 2, s->c.intra_dc_precision);
400
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4682 times.
4682 av_assert0(s->c.picture_structure == PICT_FRAME);
402 4682 put_bits(&s->pb, 2, s->c.picture_structure);
403
2/2
✓ Branch 0 taken 3532 times.
✓ Branch 1 taken 1150 times.
4682 if (s->c.progressive_sequence)
404 3532 put_bits(&s->pb, 1, 0); /* no repeat */
405 else
406 1150 put_bits(&s->pb, 1, !!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
407 /* XXX: optimize the generation of this flag with entropy measures */
408 4682 s->c.frame_pred_frame_dct = s->c.progressive_sequence;
409
410 4682 put_bits(&s->pb, 1, s->c.frame_pred_frame_dct);
411 4682 put_bits(&s->pb, 1, s->c.concealment_motion_vectors);
412 4682 put_bits(&s->pb, 1, s->c.q_scale_type);
413 4682 put_bits(&s->pb, 1, s->c.intra_vlc_format);
414 4682 put_bits(&s->pb, 1, s->c.alternate_scan);
415 4682 put_bits(&s->pb, 1, s->c.repeat_first_field);
416 4682 s->c.progressive_frame = s->c.progressive_sequence;
417 /* chroma_420_type */
418
2/2
✓ Branch 0 taken 4332 times.
✓ Branch 1 taken 350 times.
9014 put_bits(&s->pb, 1, s->c.chroma_format ==
419 4332 CHROMA_420 ? s->c.progressive_frame : 0);
420 4682 put_bits(&s->pb, 1, s->c.progressive_frame);
421 4682 put_bits(&s->pb, 1, 0); /* composite_display_flag */
422 }
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5162 times.
5162 if (mpeg12->scan_offset) {
424 int i;
425
426 put_header(s, USER_START_CODE);
427 for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
428 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
429 }
430 5162 side_data = av_frame_get_side_data(s->c.cur_pic.ptr->f,
431 AV_FRAME_DATA_STEREO3D);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5162 times.
5162 if (side_data) {
433 const AVStereo3D *stereo = (AVStereo3D *)side_data->data;
434 uint8_t fpa_type;
435
436 switch (stereo->type) {
437 case AV_STEREO3D_SIDEBYSIDE:
438 fpa_type = 0x03;
439 break;
440 case AV_STEREO3D_TOPBOTTOM:
441 fpa_type = 0x04;
442 break;
443 case AV_STEREO3D_2D:
444 fpa_type = 0x08;
445 break;
446 case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
447 fpa_type = 0x23;
448 break;
449 default:
450 fpa_type = 0;
451 break;
452 }
453
454 if (fpa_type != 0) {
455 put_header(s, USER_START_CODE);
456 // S3D_video_format_signaling_identifier
457 put_bits32(&s->pb, MKBETAG('J','P','3','D'));
458 put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
459
460 put_bits(&s->pb, 1, 1); // reserved_bit
461 put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
462 put_bits(&s->pb, 16, 0x04FF); // reserved_data
463 }
464 }
465
466
2/2
✓ Branch 0 taken 4682 times.
✓ Branch 1 taken 480 times.
5162 if (CONFIG_MPEG2VIDEO_ENCODER && mpeg12->a53_cc) {
467 4682 side_data = av_frame_get_side_data(s->c.cur_pic.ptr->f,
468 AV_FRAME_DATA_A53_CC);
469
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 4547 times.
4682 if (side_data) {
470
3/4
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
135 if (side_data->size <= A53_MAX_CC_COUNT * 3 && side_data->size % 3 == 0) {
471 134 put_header (s, USER_START_CODE);
472
473 134 put_bits32(&s->pb, MKBETAG('G','A','9','4')); // user_identifier
474 134 put_bits(&s->pb, 8, 3); // user_data_type_code
475 134 put_bits(&s->pb, 8,
476 134 (side_data->size / 3 & A53_MAX_CC_COUNT) | 0x40); // flags, cc_count
477 134 put_bits(&s->pb, 8, 0xff); // em_data
478
479
2/2
✓ Branch 0 taken 8040 times.
✓ Branch 1 taken 134 times.
8174 for (int i = 0; i < side_data->size; i++)
480 8040 put_bits(&s->pb, 8, side_data->data[i]);
481
482 134 put_bits(&s->pb, 8, 0xff); // marker_bits
483 } else {
484 1 av_log(s->c.avctx, AV_LOG_WARNING,
485 "Closed Caption size (%zu) can not exceed "
486 1 "93 bytes and must be a multiple of 3\n", side_data->size);
487 }
488 }
489 }
490
491 5162 s->c.mb_y = 0;
492 5162 ff_mpeg1_encode_slice_header(s);
493
494 5162 return 0;
495 }
496
497 1465388 static inline void put_mb_modes(MPVEncContext *const s, int n, int bits,
498 int has_mv, int field_motion)
499 {
500 1465388 put_bits(&s->pb, n, bits);
501
2/2
✓ Branch 0 taken 751049 times.
✓ Branch 1 taken 714339 times.
1465388 if (!s->c.frame_pred_frame_dct) {
502
2/2
✓ Branch 0 taken 407012 times.
✓ Branch 1 taken 344037 times.
751049 if (has_mv)
503 /* motion_type: frame/field */
504 407012 put_bits(&s->pb, 2, 2 - field_motion);
505 751049 put_bits(&s->pb, 1, s->c.interlaced_dct);
506 }
507 1465388 }
508
509 // RAL: Parameter added: f_or_b_code
510 3339732 static void mpeg1_encode_motion(MPVEncContext *const s, int val, int f_or_b_code)
511 {
512
2/2
✓ Branch 0 taken 1369061 times.
✓ Branch 1 taken 1970671 times.
3339732 if (val == 0) {
513 /* zero vector, corresponds to ff_mpeg12_mbMotionVectorTable[0] */
514 1369061 put_bits(&s->pb, 1, 0x01);
515 } else {
516 int code, sign, bits;
517 1970671 int bit_size = f_or_b_code - 1;
518 1970671 int range = 1 << bit_size;
519 /* modulo encoding */
520 1970671 val = sign_extend(val, 5 + bit_size);
521
522
2/2
✓ Branch 0 taken 950702 times.
✓ Branch 1 taken 1019969 times.
1970671 if (val >= 0) {
523 950702 val--;
524 950702 code = (val >> bit_size) + 1;
525 950702 bits = val & (range - 1);
526 950702 sign = 0;
527 } else {
528 1019969 val = -val;
529 1019969 val--;
530 1019969 code = (val >> bit_size) + 1;
531 1019969 bits = val & (range - 1);
532 1019969 sign = 1;
533 }
534
535 av_assert2(code > 0 && code <= 16);
536
537 1970671 put_bits(&s->pb,
538 1970671 ff_mpeg12_mbMotionVectorTable[code][1],
539 1970671 ff_mpeg12_mbMotionVectorTable[code][0]);
540
541 1970671 put_bits(&s->pb, 1, sign);
542
2/2
✓ Branch 0 taken 1420820 times.
✓ Branch 1 taken 549851 times.
1970671 if (bit_size > 0)
543 1420820 put_bits(&s->pb, bit_size, bits);
544 }
545 3339732 }
546
547 3480152 static inline void encode_dc(MPVEncContext *const s, int diff, int component)
548 {
549 3480152 unsigned int diff_u = diff + 255;
550
2/2
✓ Branch 0 taken 165026 times.
✓ Branch 1 taken 3315126 times.
3480152 if (diff_u >= 511) {
551 int index;
552
553
2/2
✓ Branch 0 taken 114879 times.
✓ Branch 1 taken 50147 times.
165026 if (diff < 0) {
554 114879 index = av_log2_16bit(-2 * diff);
555 114879 diff--;
556 } else {
557 50147 index = av_log2_16bit(2 * diff);
558 }
559
2/2
✓ Branch 0 taken 93845 times.
✓ Branch 1 taken 71181 times.
165026 if (component == 0)
560 93845 put_bits(&s->pb,
561 93845 ff_mpeg12_vlc_dc_lum_bits[index] + index,
562 93845 (ff_mpeg12_vlc_dc_lum_code[index] << index) +
563 93845 av_zero_extend(diff, index));
564 else
565 71181 put_bits(&s->pb,
566 71181 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
567 71181 (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
568 71181 av_zero_extend(diff, index));
569 } else {
570
2/2
✓ Branch 0 taken 1832575 times.
✓ Branch 1 taken 1482551 times.
3315126 if (component == 0)
571 1832575 put_bits(&s->pb,
572 1832575 mpeg1_lum_dc_uni[diff + 255] & 0xFF,
573 1832575 mpeg1_lum_dc_uni[diff + 255] >> 8);
574 else
575 1482551 put_bits(&s->pb,
576 1482551 mpeg1_chr_dc_uni[diff + 255] & 0xFF,
577 1482551 mpeg1_chr_dc_uni[diff + 255] >> 8);
578 }
579 3480152 }
580
581 7057892 static void mpeg1_encode_block(MPVEncContext *const s, const int16_t block[], int n)
582 {
583 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
584 int code, component;
585 7057892 const uint16_t (*table_vlc)[2] = ff_mpeg1_vlc_table;
586
587 7057892 last_index = s->c.block_last_index[n];
588
589 /* DC coef */
590
2/2
✓ Branch 0 taken 3480152 times.
✓ Branch 1 taken 3577740 times.
7057892 if (s->c.mb_intra) {
591
2/2
✓ Branch 0 taken 1553732 times.
✓ Branch 1 taken 1926420 times.
3480152 component = (n <= 3 ? 0 : (n & 1) + 1);
592 3480152 dc = block[0]; /* overflow is impossible */
593 3480152 diff = dc - s->last_dc[component];
594 3480152 encode_dc(s, diff, component);
595 3480152 s->last_dc[component] = dc;
596 3480152 i = 1;
597
2/2
✓ Branch 0 taken 2641700 times.
✓ Branch 1 taken 838452 times.
3480152 if (s->c.intra_vlc_format)
598 2641700 table_vlc = ff_mpeg2_vlc_table;
599 } else {
600 /* encode the first coefficient: needs to be done here because
601 * it is handled slightly differently */
602 3577740 level = block[0];
603
2/2
✓ Branch 0 taken 1068539 times.
✓ Branch 1 taken 2509201 times.
3577740 if (abs(level) == 1) {
604 1068539 code = ((uint32_t)level >> 31); /* the sign bit */
605 1068539 put_bits(&s->pb, 2, code | 0x02);
606 1068539 i = 1;
607 } else {
608 2509201 i = 0;
609 2509201 last_non_zero = -1;
610 2509201 goto next_coef;
611 }
612 }
613
614 /* now quantify & encode AC coefs */
615 4548691 last_non_zero = i - 1;
616
617
2/2
✓ Branch 0 taken 164444006 times.
✓ Branch 1 taken 7057892 times.
171501898 for (; i <= last_index; i++) {
618 164444006 j = s->c.intra_scantable.permutated[i];
619 164444006 level = block[j];
620
621 166953207 next_coef:
622 /* encode using VLC */
623
2/2
✓ Branch 0 taken 69056497 times.
✓ Branch 1 taken 97896710 times.
166953207 if (level != 0) {
624 69056497 run = i - last_non_zero - 1;
625
626 69056497 alevel = level;
627 69056497 MASK_ABS(sign, alevel);
628 69056497 sign &= 1;
629
630
2/2
✓ Branch 0 taken 66856160 times.
✓ Branch 1 taken 2200337 times.
69056497 if (alevel <= mpeg12_max_level[run]) {
631 66856160 code = mpeg12_index_run[run] + alevel - 1;
632 /* store the VLC & sign at once */
633 66856160 put_bits(&s->pb, table_vlc[code][1] + 1,
634 66856160 (table_vlc[code][0] << 1) + sign);
635 } else {
636 /* Escape seems to be pretty rare <5% so I do not optimize it.
637 * The following encodes run together with the common escape
638 * value of both tables 000001b. */
639 2200337 put_bits(&s->pb, 6 + 6, 0x01 << 6 | run);
640 /* escape: only clip in this case */
641
2/2
✓ Branch 0 taken 82680 times.
✓ Branch 1 taken 2117657 times.
2200337 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO) {
642
1/2
✓ Branch 0 taken 82680 times.
✗ Branch 1 not taken.
82680 if (alevel < 128) {
643 82680 put_sbits(&s->pb, 8, level);
644 } else {
645 if (level < 0)
646 put_bits(&s->pb, 16, 0x8001 + level + 255);
647 else
648 put_sbits(&s->pb, 16, level);
649 }
650 } else {
651 2117657 put_sbits(&s->pb, 12, level);
652 }
653 }
654 69056497 last_non_zero = i;
655 }
656 }
657 /* end of block */
658 7057892 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
659 7057892 }
660
661 2193830 static av_always_inline void mpeg1_encode_mb_internal(MPVEncContext *const s,
662 const int16_t block[8][64],
663 int motion_x, int motion_y,
664 int mb_block_count,
665 int chroma_y_shift)
666 {
667 /* MPEG-1 is always 420. */
668 #define IS_MPEG1(s) (chroma_y_shift == 1 && (s)->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
669 int i, cbp;
670 2193830 const int mb_x = s->c.mb_x;
671 2193830 const int mb_y = s->c.mb_y;
672
4/4
✓ Branch 0 taken 345693 times.
✓ Branch 1 taken 1848137 times.
✓ Branch 2 taken 341036 times.
✓ Branch 3 taken 4657 times.
2193830 const int first_mb = mb_x == s->c.resync_mb_x && mb_y == s->c.resync_mb_y;
673
674 /* compute cbp */
675 2193830 cbp = 0;
676
2/2
✓ Branch 0 taken 14448890 times.
✓ Branch 1 taken 2193830 times.
16642720 for (i = 0; i < mb_block_count; i++)
677
2/2
✓ Branch 0 taken 7057892 times.
✓ Branch 1 taken 7390998 times.
14448890 if (s->c.block_last_index[i] >= 0)
678 7057892 cbp |= 1 << (mb_block_count - 1 - i);
679
680
6/6
✓ Branch 0 taken 728442 times.
✓ Branch 1 taken 1465388 times.
✓ Branch 2 taken 702989 times.
✓ Branch 3 taken 25453 times.
✓ Branch 4 taken 682766 times.
✓ Branch 5 taken 20223 times.
2193830 if (cbp == 0 && !first_mb && s->c.mv_type == MV_TYPE_16X16 &&
681
2/2
✓ Branch 0 taken 25940 times.
✓ Branch 1 taken 656826 times.
682766 (mb_x != s->c.mb_width - 1 ||
682
6/6
✓ Branch 0 taken 24032 times.
✓ Branch 1 taken 1908 times.
✓ Branch 2 taken 22072 times.
✓ Branch 3 taken 1960 times.
✓ Branch 4 taken 552 times.
✓ Branch 5 taken 21520 times.
25940 (mb_y != s->c.end_mb_y - 1 && IS_MPEG1(s))) &&
683
4/4
✓ Branch 0 taken 487142 times.
✓ Branch 1 taken 170236 times.
✓ Branch 2 taken 112023 times.
✓ Branch 3 taken 375119 times.
657378 ((s->c.pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
684
4/4
✓ Branch 0 taken 170236 times.
✓ Branch 1 taken 112023 times.
✓ Branch 2 taken 22008 times.
✓ Branch 3 taken 148228 times.
282259 (s->c.pict_type == AV_PICTURE_TYPE_B && s->c.mv_dir == s->last_mv_dir &&
685 22008 (((s->c.mv_dir & MV_DIR_FORWARD)
686 20311 ? ((s->c.mv[0][0][0] - s->c.last_mv[0][0][0]) |
687
4/4
✓ Branch 0 taken 20311 times.
✓ Branch 1 taken 1697 times.
✓ Branch 2 taken 4342 times.
✓ Branch 3 taken 17666 times.
44016 (s->c.mv[0][0][1] - s->c.last_mv[0][0][1])) : 0) |
688 22008 ((s->c.mv_dir & MV_DIR_BACKWARD)
689 21679 ? ((s->c.mv[1][0][0] - s->c.last_mv[1][0][0]) |
690
2/2
✓ Branch 0 taken 21679 times.
✓ Branch 1 taken 329 times.
22008 (s->c.mv[1][0][1] - s->c.last_mv[1][0][1])) : 0)) == 0))) {
691 379461 s->mb_skip_run++;
692 379461 s->c.qscale -= s->dquant;
693 379461 s->misc_bits++;
694 379461 s->last_bits++;
695
2/2
✓ Branch 0 taken 375119 times.
✓ Branch 1 taken 4342 times.
379461 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
696 375119 s->c.last_mv[0][0][0] =
697 375119 s->c.last_mv[0][0][1] =
698 375119 s->c.last_mv[0][1][0] =
699 375119 s->c.last_mv[0][1][1] = 0;
700 }
701 } else {
702
2/2
✓ Branch 0 taken 341036 times.
✓ Branch 1 taken 1473333 times.
1814369 if (first_mb) {
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 341036 times.
341036 av_assert0(s->mb_skip_run == 0);
704 341036 encode_mb_skip_run(s, s->c.mb_x);
705 } else {
706 1473333 encode_mb_skip_run(s, s->mb_skip_run);
707 }
708
709
2/2
✓ Branch 0 taken 407920 times.
✓ Branch 1 taken 1406449 times.
1814369 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
710
3/4
✓ Branch 0 taken 43090 times.
✓ Branch 1 taken 364830 times.
✓ Branch 2 taken 43090 times.
✗ Branch 3 not taken.
407920 if (s->dquant && cbp) {
711 /* macroblock_type: macroblock_quant = 1 */
712 43090 put_mb_modes(s, 2, 1, 0, 0);
713 43090 put_qscale(s);
714 } else {
715 /* macroblock_type: macroblock_quant = 0 */
716 364830 put_mb_modes(s, 1, 1, 0, 0);
717 364830 s->c.qscale -= s->dquant;
718 }
719 407920 s->misc_bits += get_bits_diff(s);
720 407920 s->i_count++;
721
2/2
✓ Branch 0 taken 73685 times.
✓ Branch 1 taken 1332764 times.
1406449 } else if (s->c.mb_intra) {
722
3/4
✓ Branch 0 taken 11835 times.
✓ Branch 1 taken 61850 times.
✓ Branch 2 taken 11835 times.
✗ Branch 3 not taken.
73685 if (s->dquant && cbp) {
723 11835 put_mb_modes(s, 6, 0x01, 0, 0);
724 11835 put_qscale(s);
725 } else {
726 61850 put_mb_modes(s, 5, 0x03, 0, 0);
727 61850 s->c.qscale -= s->dquant;
728 }
729 73685 s->misc_bits += get_bits_diff(s);
730 73685 s->i_count++;
731 73685 memset(s->c.last_mv, 0, sizeof(s->c.last_mv));
732
2/2
✓ Branch 0 taken 751821 times.
✓ Branch 1 taken 580943 times.
1332764 } else if (s->c.pict_type == AV_PICTURE_TYPE_P) {
733
2/2
✓ Branch 0 taken 728972 times.
✓ Branch 1 taken 22849 times.
751821 if (s->c.mv_type == MV_TYPE_16X16) {
734
2/2
✓ Branch 0 taken 576147 times.
✓ Branch 1 taken 152825 times.
728972 if (cbp != 0) {
735
2/2
✓ Branch 0 taken 63768 times.
✓ Branch 1 taken 512379 times.
576147 if ((motion_x | motion_y) == 0) {
736
2/2
✓ Branch 0 taken 1554 times.
✓ Branch 1 taken 62214 times.
63768 if (s->dquant) {
737 /* macroblock_pattern & quant */
738 1554 put_mb_modes(s, 5, 1, 0, 0);
739 1554 put_qscale(s);
740 } else {
741 /* macroblock_pattern only */
742 62214 put_mb_modes(s, 2, 1, 0, 0);
743 }
744 63768 s->misc_bits += get_bits_diff(s);
745 } else {
746
2/2
✓ Branch 0 taken 72389 times.
✓ Branch 1 taken 439990 times.
512379 if (s->dquant) {
747 72389 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
748 72389 put_qscale(s);
749 } else {
750 439990 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
751 }
752 512379 s->misc_bits += get_bits_diff(s);
753 // RAL: f_code parameter added
754 512379 mpeg1_encode_motion(s,
755 512379 motion_x - s->c.last_mv[0][0][0],
756 s->f_code);
757 // RAL: f_code parameter added
758 512379 mpeg1_encode_motion(s,
759 512379 motion_y - s->c.last_mv[0][0][1],
760 s->f_code);
761 512379 s->mv_bits += get_bits_diff(s);
762 }
763 } else {
764 152825 put_bits(&s->pb, 3, 1); /* motion only */
765
2/2
✓ Branch 0 taken 24679 times.
✓ Branch 1 taken 128146 times.
152825 if (!s->c.frame_pred_frame_dct)
766 24679 put_bits(&s->pb, 2, 2); /* motion_type: frame */
767 152825 s->misc_bits += get_bits_diff(s);
768 // RAL: f_code parameter added
769 152825 mpeg1_encode_motion(s,
770 152825 motion_x - s->c.last_mv[0][0][0],
771 s->f_code);
772 // RAL: f_code parameter added
773 152825 mpeg1_encode_motion(s,
774 152825 motion_y - s->c.last_mv[0][0][1],
775 s->f_code);
776 152825 s->c.qscale -= s->dquant;
777 152825 s->mv_bits += get_bits_diff(s);
778 }
779 728972 s->c.last_mv[0][1][0] = s->c.last_mv[0][0][0] = motion_x;
780 728972 s->c.last_mv[0][1][1] = s->c.last_mv[0][0][1] = motion_y;
781 } else {
782 av_assert2(!s->c.frame_pred_frame_dct && s->c.mv_type == MV_TYPE_FIELD);
783
784
2/2
✓ Branch 0 taken 21344 times.
✓ Branch 1 taken 1505 times.
22849 if (cbp) {
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21344 times.
21344 if (s->dquant) {
786 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
787 put_qscale(s);
788 } else {
789 21344 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
790 }
791 } else {
792 1505 put_bits(&s->pb, 3, 1); /* motion only */
793 1505 put_bits(&s->pb, 2, 1); /* motion_type: field */
794 1505 s->c.qscale -= s->dquant;
795 }
796 22849 s->misc_bits += get_bits_diff(s);
797
2/2
✓ Branch 0 taken 45698 times.
✓ Branch 1 taken 22849 times.
68547 for (i = 0; i < 2; i++) {
798 45698 put_bits(&s->pb, 1, s->c.field_select[0][i]);
799 45698 mpeg1_encode_motion(s,
800 45698 s->c.mv[0][i][0] - s->c.last_mv[0][i][0],
801 s->f_code);
802 45698 mpeg1_encode_motion(s,
803 45698 s->c.mv[0][i][1] - (s->c.last_mv[0][i][1] >> 1),
804 s->f_code);
805 45698 s->c.last_mv[0][i][0] = s->c.mv[0][i][0];
806 45698 s->c.last_mv[0][i][1] = 2 * s->c.mv[0][i][1];
807 }
808 22849 s->mv_bits += get_bits_diff(s);
809 }
810
2/2
✓ Branch 0 taken 597491 times.
✓ Branch 1 taken 154330 times.
751821 if (cbp) {
811
2/2
✓ Branch 0 taken 522679 times.
✓ Branch 1 taken 74812 times.
597491 if (chroma_y_shift) {
812 522679 put_bits(&s->pb,
813 522679 ff_mpeg12_mbPatTable[cbp][1],
814 522679 ff_mpeg12_mbPatTable[cbp][0]);
815 } else {
816 74812 put_bits(&s->pb,
817 74812 ff_mpeg12_mbPatTable[cbp >> 2][1],
818 74812 ff_mpeg12_mbPatTable[cbp >> 2][0]);
819 74812 put_sbits(&s->pb, 2, cbp);
820 }
821 }
822 } else {
823
2/2
✓ Branch 0 taken 488110 times.
✓ Branch 1 taken 92833 times.
580943 if (s->c.mv_type == MV_TYPE_16X16) {
824
2/2
✓ Branch 0 taken 312597 times.
✓ Branch 1 taken 175513 times.
488110 if (cbp) { // With coded bloc pattern
825
2/2
✓ Branch 0 taken 53290 times.
✓ Branch 1 taken 259307 times.
312597 if (s->dquant) {
826
2/2
✓ Branch 0 taken 11218 times.
✓ Branch 1 taken 42072 times.
53290 if (s->c.mv_dir == MV_DIR_FORWARD)
827 11218 put_mb_modes(s, 6, 3, 1, 0);
828 else
829 42072 put_mb_modes(s, 8 - s->c.mv_dir, 2, 1, 0);
830 53290 put_qscale(s);
831 } else {
832 259307 put_mb_modes(s, 5 - s->c.mv_dir, 3, 1, 0);
833 }
834 } else { // No coded bloc pattern
835 175513 put_bits(&s->pb, 5 - s->c.mv_dir, 2);
836
2/2
✓ Branch 0 taken 82021 times.
✓ Branch 1 taken 93492 times.
175513 if (!s->c.frame_pred_frame_dct)
837 82021 put_bits(&s->pb, 2, 2); /* motion_type: frame */
838 175513 s->c.qscale -= s->dquant;
839 }
840 488110 s->misc_bits += get_bits_diff(s);
841
2/2
✓ Branch 0 taken 351301 times.
✓ Branch 1 taken 136809 times.
488110 if (s->c.mv_dir & MV_DIR_FORWARD) {
842 351301 mpeg1_encode_motion(s,
843 351301 s->c.mv[0][0][0] - s->c.last_mv[0][0][0],
844 s->f_code);
845 351301 mpeg1_encode_motion(s,
846 351301 s->c.mv[0][0][1] - s->c.last_mv[0][0][1],
847 s->f_code);
848 351301 s->c.last_mv[0][0][0] =
849 351301 s->c.last_mv[0][1][0] = s->c.mv[0][0][0];
850 351301 s->c.last_mv[0][0][1] =
851 351301 s->c.last_mv[0][1][1] = s->c.mv[0][0][1];
852 }
853
2/2
✓ Branch 0 taken 369943 times.
✓ Branch 1 taken 118167 times.
488110 if (s->c.mv_dir & MV_DIR_BACKWARD) {
854 369943 mpeg1_encode_motion(s,
855 369943 s->c.mv[1][0][0] - s->c.last_mv[1][0][0],
856 s->b_code);
857 369943 mpeg1_encode_motion(s,
858 369943 s->c.mv[1][0][1] - s->c.last_mv[1][0][1],
859 s->b_code);
860 369943 s->c.last_mv[1][0][0] =
861 369943 s->c.last_mv[1][1][0] = s->c.mv[1][0][0];
862 369943 s->c.last_mv[1][0][1] =
863 369943 s->c.last_mv[1][1][1] = s->c.mv[1][0][1];
864 }
865 } else {
866 av_assert2(s->c.mv_type == MV_TYPE_FIELD);
867 av_assert2(!s->c.frame_pred_frame_dct);
868
2/2
✓ Branch 0 taken 73695 times.
✓ Branch 1 taken 19138 times.
92833 if (cbp) { // With coded bloc pattern
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73695 times.
73695 if (s->dquant) {
870 if (s->c.mv_dir == MV_DIR_FORWARD)
871 put_mb_modes(s, 6, 3, 1, 1);
872 else
873 put_mb_modes(s, 8 - s->c.mv_dir, 2, 1, 1);
874 put_qscale(s);
875 } else {
876 73695 put_mb_modes(s, 5 - s->c.mv_dir, 3, 1, 1);
877 }
878 } else { // No coded bloc pattern
879 19138 put_bits(&s->pb, 5 - s->c.mv_dir, 2);
880 19138 put_bits(&s->pb, 2, 1); /* motion_type: field */
881 19138 s->c.qscale -= s->dquant;
882 }
883 92833 s->misc_bits += get_bits_diff(s);
884
2/2
✓ Branch 0 taken 59191 times.
✓ Branch 1 taken 33642 times.
92833 if (s->c.mv_dir & MV_DIR_FORWARD) {
885
2/2
✓ Branch 0 taken 118382 times.
✓ Branch 1 taken 59191 times.
177573 for (i = 0; i < 2; i++) {
886 118382 put_bits(&s->pb, 1, s->c.field_select[0][i]);
887 118382 mpeg1_encode_motion(s,
888 118382 s->c.mv[0][i][0] - s->c.last_mv[0][i][0],
889 s->f_code);
890 118382 mpeg1_encode_motion(s,
891 118382 s->c.mv[0][i][1] - (s->c.last_mv[0][i][1] >> 1),
892 s->f_code);
893 118382 s->c.last_mv[0][i][0] = s->c.mv[0][i][0];
894 118382 s->c.last_mv[0][i][1] = s->c.mv[0][i][1] * 2;
895 }
896 }
897
2/2
✓ Branch 0 taken 59669 times.
✓ Branch 1 taken 33164 times.
92833 if (s->c.mv_dir & MV_DIR_BACKWARD) {
898
2/2
✓ Branch 0 taken 119338 times.
✓ Branch 1 taken 59669 times.
179007 for (i = 0; i < 2; i++) {
899 119338 put_bits(&s->pb, 1, s->c.field_select[1][i]);
900 119338 mpeg1_encode_motion(s,
901 119338 s->c.mv[1][i][0] - s->c.last_mv[1][i][0],
902 s->b_code);
903 119338 mpeg1_encode_motion(s,
904 119338 s->c.mv[1][i][1] - (s->c.last_mv[1][i][1] >> 1),
905 s->b_code);
906 119338 s->c.last_mv[1][i][0] = s->c.mv[1][i][0];
907 119338 s->c.last_mv[1][i][1] = s->c.mv[1][i][1] * 2;
908 }
909 }
910 }
911 580943 s->mv_bits += get_bits_diff(s);
912
2/2
✓ Branch 0 taken 386292 times.
✓ Branch 1 taken 194651 times.
580943 if (cbp) {
913
2/2
✓ Branch 0 taken 206130 times.
✓ Branch 1 taken 180162 times.
386292 if (chroma_y_shift) {
914 206130 put_bits(&s->pb,
915 206130 ff_mpeg12_mbPatTable[cbp][1],
916 206130 ff_mpeg12_mbPatTable[cbp][0]);
917 } else {
918 180162 put_bits(&s->pb,
919 180162 ff_mpeg12_mbPatTable[cbp >> 2][1],
920 180162 ff_mpeg12_mbPatTable[cbp >> 2][0]);
921 180162 put_sbits(&s->pb, 2, cbp);
922 }
923 }
924 }
925
2/2
✓ Branch 0 taken 12170828 times.
✓ Branch 1 taken 1814369 times.
13985197 for (i = 0; i < mb_block_count; i++)
926
2/2
✓ Branch 0 taken 7057892 times.
✓ Branch 1 taken 5112936 times.
12170828 if (cbp & (1 << (mb_block_count - 1 - i)))
927 7057892 mpeg1_encode_block(s, block[i], i);
928 1814369 s->mb_skip_run = 0;
929
2/2
✓ Branch 0 taken 481605 times.
✓ Branch 1 taken 1332764 times.
1814369 if (s->c.mb_intra)
930 481605 s->i_tex_bits += get_bits_diff(s);
931 else
932 1332764 s->p_tex_bits += get_bits_diff(s);
933 }
934 2193830 }
935
936 2193830 static void mpeg12_encode_mb(MPVEncContext *const s, int16_t block[][64],
937 int motion_x, int motion_y)
938 {
939
2/2
✓ Branch 0 taken 1712225 times.
✓ Branch 1 taken 481605 times.
2193830 if (!s->c.mb_intra)
940 1712225 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 128 << s->c.intra_dc_precision;
941
2/2
✓ Branch 0 taken 1550875 times.
✓ Branch 1 taken 642955 times.
2193830 if (s->c.chroma_format == CHROMA_420)
942 1550875 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6, 1);
943 else
944 642955 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8, 0);
945 2193830 }
946
947 64 static av_cold void mpeg12_encode_init_static(void)
948 {
949 64 ff_rl_init_level_run(mpeg12_max_level, mpeg12_index_run,
950 ff_mpeg12_run, ff_mpeg12_level, MPEG12_RL_NB_ELEMS);
951
952 64 ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run,
953 ff_mpeg1_vlc_table, uni_mpeg1_ac_vlc_len);
954 64 ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run,
955 ff_mpeg2_vlc_table, uni_mpeg2_ac_vlc_len);
956
957 /* build unified dc encoding tables */
958
2/2
✓ Branch 0 taken 32704 times.
✓ Branch 1 taken 64 times.
32768 for (int i = -255; i < 256; i++) {
959 int adiff, index;
960 int bits, code;
961 32704 int diff = i;
962
963 32704 adiff = FFABS(diff);
964
2/2
✓ Branch 0 taken 16320 times.
✓ Branch 1 taken 16384 times.
32704 if (diff < 0)
965 16320 diff--;
966 32704 index = av_log2(2 * adiff);
967
968 32704 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
969 32704 code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
970 32704 av_zero_extend(diff, index);
971 32704 mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
972
973 32704 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
974 32704 code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
975 32704 av_zero_extend(diff, index);
976 32704 mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
977 }
978
979
2/2
✓ Branch 0 taken 448 times.
✓ Branch 1 taken 64 times.
512 for (int f_code = 1; f_code <= MAX_FCODE; f_code++)
980
2/2
✓ Branch 0 taken 7340480 times.
✓ Branch 1 taken 448 times.
7340928 for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) {
981 int len;
982
983
2/2
✓ Branch 0 taken 448 times.
✓ Branch 1 taken 7340032 times.
7340480 if (mv == 0) {
984 448 len = 1; /* ff_mpeg12_mbMotionVectorTable[0][1] */
985 } else {
986 int val, bit_size, code;
987
988 7340032 bit_size = f_code - 1;
989
990 7340032 val = mv;
991
2/2
✓ Branch 0 taken 3670016 times.
✓ Branch 1 taken 3670016 times.
7340032 if (val < 0)
992 3670016 val = -val;
993 7340032 val--;
994 7340032 code = (val >> bit_size) + 1;
995
2/2
✓ Branch 0 taken 260096 times.
✓ Branch 1 taken 7079936 times.
7340032 if (code < 17)
996 260096 len = ff_mpeg12_mbMotionVectorTable[code][1] +
997 1 + bit_size;
998 else
999 7079936 len = 10 /* ff_mpeg12_mbMotionVectorTable[16][1] */ +
1000 2 + bit_size;
1001 }
1002
1003 7340480 mv_penalty[f_code][mv + MAX_DMV] = len;
1004 }
1005
1006
1007
2/2
✓ Branch 0 taken 448 times.
✓ Branch 1 taken 64 times.
512 for (int f_code = MAX_FCODE; f_code > 0; f_code--)
1008
2/2
✓ Branch 0 taken 260096 times.
✓ Branch 1 taken 448 times.
260544 for (int mv = -(8 << f_code); mv < (8 << f_code); mv++)
1009 260096 fcode_tab[mv + MAX_MV] = f_code;
1010 64 }
1011
1012 64 static av_cold int find_frame_rate_index(AVCodecContext *avctx, MPEG12EncContext *mpeg12)
1013 {
1014 64 AVRational bestq = (AVRational) {0, 0};
1015 AVRational ext;
1016 64 AVRational target = av_inv_q(avctx->time_base);
1017
1018
2/2
✓ Branch 0 taken 580 times.
✓ Branch 1 taken 1 times.
581 for (int i = 1; i < 14; i++) {
1019
4/4
✓ Branch 0 taken 567 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 504 times.
580 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
1020 i >= 9)
1021 63 break;
1022
1023
2/2
✓ Branch 0 taken 2068 times.
✓ Branch 1 taken 517 times.
2585 for (ext.num = 1; ext.num <= 4; ext.num++) {
1024
2/2
✓ Branch 0 taken 66176 times.
✓ Branch 1 taken 2068 times.
68244 for (ext.den = 1; ext.den <= 32; ext.den++) {
1025 66176 AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]);
1026
1027
6/6
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 54912 times.
✓ Branch 2 taken 352 times.
✓ Branch 3 taken 10912 times.
✓ Branch 4 taken 264 times.
✓ Branch 5 taken 88 times.
66176 if (avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
1028 29194 continue;
1029
2/2
✓ Branch 0 taken 18018 times.
✓ Branch 1 taken 36982 times.
55000 if (av_gcd(ext.den, ext.num) != 1)
1030 18018 continue;
1031
1032
2/2
✓ Branch 0 taken 36918 times.
✓ Branch 1 taken 64 times.
36982 if ( bestq.num==0
1033
2/2
✓ Branch 1 taken 36634 times.
✓ Branch 2 taken 284 times.
36918 || av_nearer_q(target, bestq, q) < 0
1034
5/6
✓ Branch 0 taken 13478 times.
✓ Branch 1 taken 23156 times.
✓ Branch 2 taken 347 times.
✓ Branch 3 taken 13131 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 347 times.
36634 || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) {
1035 348 bestq = q;
1036 348 mpeg12->frame_rate_index = i;
1037 348 mpeg12->frame_rate_ext.num = ext.num;
1038 348 mpeg12->frame_rate_ext.den = ext.den;
1039 }
1040 }
1041 }
1042 }
1043
1044
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 63 times.
64 if (av_cmp_q(target, bestq))
1045 1 return -1;
1046 else
1047 63 return 0;
1048 }
1049
1050 64 static av_cold int encode_init(AVCodecContext *avctx)
1051 {
1052 static AVOnce init_static_once = AV_ONCE_INIT;
1053 64 MPEG12EncContext *const mpeg12 = avctx->priv_data;
1054 64 MPVMainEncContext *const m = &mpeg12->mpeg;
1055 64 MPVEncContext *const s = &m->s;
1056 int ret;
1057
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 11 times.
64 int max_size = avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 16383 : 4095;
1058
1059
2/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
64 if (avctx->width > max_size || avctx->height > max_size) {
1060 av_log(avctx, AV_LOG_ERROR, "%s does not support resolutions above %dx%d\n",
1061 CONFIG_SMALL ? avctx->codec->name : avctx->codec->long_name,
1062 max_size, max_size);
1063 return AVERROR(EINVAL);
1064 }
1065
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
64 if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
1066 av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
1067 return AVERROR(EINVAL);
1068 }
1069
1070
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 1 times.
64 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
1071
2/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 63 times.
63 if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) {
1072 av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiples of 4096\n"
1073 "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
1074 return AVERROR(EINVAL);
1075 }
1076 }
1077
1078
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 63 times.
64 if (s->c.q_scale_type == 1) {
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->qmax > 28) {
1080 av_log(avctx, AV_LOG_ERROR,
1081 "non linear quant only supports qmax <= 28 currently\n");
1082 return AVERROR_PATCHWELCOME;
1083 }
1084 }
1085
1086
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (avctx->profile == AV_PROFILE_UNKNOWN) {
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (avctx->level != AV_LEVEL_UNKNOWN) {
1088 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
1089 return AVERROR(EINVAL);
1090 }
1091 /* Main or 4:2:2 */
1092 64 avctx->profile = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? AV_PROFILE_MPEG2_MAIN
1093
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 5 times.
64 : AV_PROFILE_MPEG2_422;
1094 }
1095
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (avctx->level == AV_LEVEL_UNKNOWN) {
1096
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 59 times.
64 if (avctx->profile == AV_PROFILE_MPEG2_422) { /* 4:2:2 */
1097
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (avctx->width <= 720 && avctx->height <= 608)
1098 5 avctx->level = 5; /* Main */
1099 else
1100 avctx->level = 2; /* High */
1101 } else {
1102
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 if (avctx->profile != AV_PROFILE_MPEG2_HIGH &&
1103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
1104 av_log(avctx, AV_LOG_ERROR,
1105 "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
1106 return AVERROR(EINVAL);
1107 }
1108
3/4
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
59 if (avctx->width <= 720 && avctx->height <= 576)
1109 58 avctx->level = 8; /* Main */
1110
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (avctx->width <= 1440)
1111 1 avctx->level = 6; /* High 1440 */
1112 else
1113 avctx->level = 4; /* High */
1114 }
1115 }
1116
1117 64 m->encode_picture_header = mpeg1_encode_picture_header;
1118 64 s->encode_mb = mpeg12_encode_mb;
1119
1120 64 s->me.mv_penalty = mv_penalty;
1121 64 m->fcode_tab = fcode_tab + MAX_MV;
1122
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 53 times.
64 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1123 11 s->min_qcoeff = -255;
1124 11 s->max_qcoeff = 255;
1125 } else {
1126 53 s->min_qcoeff = -2047;
1127 53 s->max_qcoeff = 2047;
1128 53 s->mpeg_quant = 1;
1129 #if FF_API_INTRA_DC_PRECISION
1130
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 5 times.
53 if (s->c.intra_dc_precision < 0) {
1131 FF_DISABLE_DEPRECATION_WARNINGS
1132 48 s->c.intra_dc_precision = avctx->intra_dc_precision;
1133 FF_ENABLE_DEPRECATION_WARNINGS
1134 // workaround some differences between how applications specify dc precision
1135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (s->c.intra_dc_precision < 0) {
1136 s->c.intra_dc_precision += 8;
1137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 } else if (s->c.intra_dc_precision >= 8)
1138 s->c.intra_dc_precision -= 8;
1139
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (s->c.intra_dc_precision < 0) {
1141 av_log(avctx, AV_LOG_ERROR,
1142 "intra dc precision must be positive, note some applications use"
1143 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
1144 return AVERROR(EINVAL);
1145 }
1146
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (s->c.intra_dc_precision > 3) {
1148 av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
1149 return AVERROR(EINVAL);
1150 }
1151 }
1152 #endif
1153 }
1154 64 s->c.y_dc_scale_table =
1155 64 s->c.c_dc_scale_table = ff_mpeg12_dc_scale_table[s->c.intra_dc_precision];
1156
1157
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 51 times.
64 if (s->c.intra_vlc_format) {
1158 13 s->intra_ac_vlc_length =
1159 13 s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len;
1160 } else {
1161 51 s->intra_ac_vlc_length =
1162 51 s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1163 }
1164 64 s->inter_ac_vlc_length =
1165 64 s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1166
1167 64 ret = ff_mpv_encode_init(avctx);
1168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (ret < 0)
1169 return ret;
1170
1171
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 53 times.
64 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 s->c.thread_context[s->c.slice_context_count - 1]->start_mb_y >
1173 SLICE_MAX_START_CODE - SLICE_MIN_START_CODE) {
1174 // MPEG-1 slices must not start at a MB row number that would make
1175 // their start code > SLICE_MAX_START_CODE. So make the last slice
1176 // bigger if needed and evenly distribute the first 174 rows.
1177 static_assert(MAX_THREADS <= 1 + SLICE_MAX_START_CODE - SLICE_MIN_START_CODE,
1178 "With more than 175 slice contexts, we have to handle "
1179 "the case in which there is no work to do for some "
1180 "slice contexts.");
1181 const int mb_height = SLICE_MAX_START_CODE - SLICE_MIN_START_CODE;
1182 const int nb_slices = s->c.slice_context_count - 1;
1183
1184 s->c.thread_context[nb_slices]->start_mb_y = mb_height;
1185
1186 av_assert1(nb_slices >= 1);
1187 for (int i = 0; i < nb_slices; i++) {
1188 s->c.thread_context[i]->start_mb_y =
1189 (mb_height * (i ) + nb_slices / 2) / nb_slices;
1190 s->c.thread_context[i]->end_mb_y =
1191 (mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1192 }
1193 }
1194
1195
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 63 times.
64 if (find_frame_rate_index(avctx, mpeg12) < 0) {
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1197 av_log(avctx, AV_LOG_ERROR, "MPEG-1/2 does not support %d/%d fps\n",
1198 avctx->time_base.den, avctx->time_base.num);
1199 return AVERROR(EINVAL);
1200 } else {
1201 1 av_log(avctx, AV_LOG_INFO,
1202 "MPEG-1/2 does not support %d/%d fps, there may be AV sync issues\n",
1203 avctx->time_base.den, avctx->time_base.num);
1204 }
1205 }
1206
1207
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 62 times.
64 if (avctx->rc_max_rate &&
1208
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 avctx->rc_min_rate == avctx->rc_max_rate &&
1209 2 90000LL * (avctx->rc_buffer_size - 1) >
1210
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 avctx->rc_max_rate * 0xFFFFLL) {
1211 1 av_log(avctx, AV_LOG_INFO,
1212 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
1213 "specified vbv buffer is too large for the given bitrate!\n");
1214 }
1215
1216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (mpeg12->drop_frame_timecode)
1217 mpeg12->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME;
1218
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
64 if (mpeg12->drop_frame_timecode && mpeg12->frame_rate_index != 4) {
1219 av_log(avctx, AV_LOG_ERROR,
1220 "Drop frame time code only allowed with 1001/30000 fps\n");
1221 return AVERROR(EINVAL);
1222 }
1223
1224
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 58 times.
64 if (mpeg12->tc_opt_str) {
1225 6 AVRational rate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index];
1226 6 int ret = av_timecode_init_from_string(&mpeg12->tc, rate, mpeg12->tc_opt_str, avctx);
1227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1228 return ret;
1229 6 mpeg12->drop_frame_timecode = !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME);
1230 6 mpeg12->timecode_frame_start = mpeg12->tc.start;
1231 } else {
1232 58 mpeg12->timecode_frame_start = 0; // default is -1
1233 }
1234
1235 64 ff_thread_once(&init_static_once, mpeg12_encode_init_static);
1236
1237 64 return 0;
1238 }
1239
1240 #define OFFSET(x) offsetof(MPEG12EncContext, x)
1241 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1242 #define COMMON_OPTS \
1243 { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format. Overrides timecode_frame_start.", \
1244 OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE },\
1245 { "drop_frame_timecode", "Timecode is in drop frame format.", \
1246 OFFSET(drop_frame_timecode), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
1247 { "scan_offset", "Reserve space for SVCD scan offset user data.", \
1248 OFFSET(scan_offset), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
1249 { "timecode_frame_start", "GOP timecode frame start number, in non-drop-frame format", \
1250 OFFSET(timecode_frame_start), AV_OPT_TYPE_INT64, {.i64 = -1 }, -1, INT64_MAX, VE}, \
1251 FF_MPV_COMMON_BFRAME_OPTS
1252
1253 static const AVOption mpeg1_options[] = {
1254 COMMON_OPTS
1255 FF_MPV_COMMON_OPTS
1256 FF_MPV_COMMON_MOTION_EST_OPTS
1257 { NULL },
1258 };
1259
1260 static const AVOption mpeg2_options[] = {
1261 COMMON_OPTS
1262 #if FF_API_INTRA_DC_PRECISION
1263 { "intra_dc_precision", "Precision of the DC coefficient - 8", FF_MPV_OFFSET(c.intra_dc_precision), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 3, VE },
1264 #else
1265 { "intra_dc_precision", "Precision of the DC coefficient - 8", FF_MPV_OFFSET(c.intra_dc_precision), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, VE },
1266 #endif
1267 { "intra_vlc", "Use MPEG-2 intra VLC table.",
1268 FF_MPV_OFFSET(c.intra_vlc_format), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1269 { "non_linear_quant", "Use nonlinear quantizer.", FF_MPV_OFFSET(c.q_scale_type), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1270 { "alternate_scan", "Enable alternate scantable.", FF_MPV_OFFSET(c.alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1271 { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE },
1272 { "seq_disp_ext", "Write sequence_display_extension blocks.", OFFSET(seq_disp_ext), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, .unit = "seq_disp_ext" },
1273 { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, VE, .unit = "seq_disp_ext" },
1274 { "never", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, VE, .unit = "seq_disp_ext" },
1275 { "always", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, VE, .unit = "seq_disp_ext" },
1276 { "video_format", "Video_format in the sequence_display_extension indicating the source of the video.", OFFSET(video_format), AV_OPT_TYPE_INT, { .i64 = VIDEO_FORMAT_UNSPECIFIED }, 0, 7, VE, .unit = "video_format" },
1277 { "component", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_COMPONENT }, 0, 0, VE, .unit = "video_format" },
1278 { "pal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_PAL }, 0, 0, VE, .unit = "video_format" },
1279 { "ntsc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_NTSC }, 0, 0, VE, .unit = "video_format" },
1280 { "secam", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_SECAM }, 0, 0, VE, .unit = "video_format" },
1281 { "mac", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_MAC }, 0, 0, VE, .unit = "video_format" },
1282 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_UNSPECIFIED}, 0, 0, VE, .unit = "video_format" },
1283 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, { .i64 = value }, 0, 0, VE, .unit = "avctx.level"
1284 { LEVEL("high", 4) },
1285 { LEVEL("high1440", 6) },
1286 { LEVEL("main", 8) },
1287 { LEVEL("low", 10) },
1288 #undef LEVEL
1289 FF_MPV_COMMON_OPTS
1290 FF_MPV_COMMON_MOTION_EST_OPTS
1291 FF_MPEG2_PROFILE_OPTS
1292 { NULL },
1293 };
1294
1295 #define mpeg12_class(x) \
1296 static const AVClass mpeg ## x ## _class = { \
1297 .class_name = "mpeg" # x "video encoder", \
1298 .item_name = av_default_item_name, \
1299 .option = mpeg ## x ## _options, \
1300 .version = LIBAVUTIL_VERSION_INT, \
1301 };
1302
1303 mpeg12_class(1)
1304 mpeg12_class(2)
1305
1306 const FFCodec ff_mpeg1video_encoder = {
1307 .p.name = "mpeg1video",
1308 CODEC_LONG_NAME("MPEG-1 video"),
1309 .p.type = AVMEDIA_TYPE_VIDEO,
1310 .p.id = AV_CODEC_ID_MPEG1VIDEO,
1311 .priv_data_size = sizeof(MPEG12EncContext),
1312 .init = encode_init,
1313 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
1314 .close = ff_mpv_encode_end,
1315 CODEC_FRAMERATES_ARRAY(ff_mpeg12_frame_rate_tab + 1),
1316 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P),
1317 .color_ranges = AVCOL_RANGE_MPEG,
1318 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1319 AV_CODEC_CAP_SLICE_THREADS |
1320 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1321 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1322 .p.priv_class = &mpeg1_class,
1323 };
1324
1325 const FFCodec ff_mpeg2video_encoder = {
1326 .p.name = "mpeg2video",
1327 CODEC_LONG_NAME("MPEG-2 video"),
1328 .p.type = AVMEDIA_TYPE_VIDEO,
1329 .p.id = AV_CODEC_ID_MPEG2VIDEO,
1330 .priv_data_size = sizeof(MPEG12EncContext),
1331 .init = encode_init,
1332 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
1333 .close = ff_mpv_encode_end,
1334 CODEC_FRAMERATES_ARRAY(ff_mpeg2_frame_rate_tab),
1335 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P),
1336 .color_ranges = AVCOL_RANGE_MPEG,
1337 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1338 AV_CODEC_CAP_SLICE_THREADS |
1339 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1340 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1341 .p.priv_class = &mpeg2_class,
1342 };
1343 #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */
1344