FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12enc.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 624 697 89.5%
Functions: 16 16 100.0%
Branches: 348 425 81.9%

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