FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12enc.c
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 624 697 89.5%
Functions: 16 16 100.0%
Branches: 347 425 81.6%

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 119 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 15232 times.
✓ Branch 1 taken 119 times.
15351 for (i = 0; i < 128; i++) {
110 15232 int level = i - 64;
111 int run;
112
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 15113 times.
15232 if (!level)
113 119 continue;
114
2/2
✓ Branch 0 taken 967232 times.
✓ Branch 1 taken 15113 times.
982345 for (run = 0; run < 64; run++) {
115 int len, code;
116 967232 int alevel = FFABS(level);
117
118
2/2
✓ Branch 0 taken 940634 times.
✓ Branch 1 taken 26598 times.
967232 if (alevel > max_level[run])
119 940634 code = 111; /* rl->n */
120 else
121 26598 code = index_run[run] + alevel - 1;
122
123
2/2
✓ Branch 0 taken 26418 times.
✓ Branch 1 taken 940814 times.
967232 if (code < 111) { /* rl->n */
124 /* length of VLC and sign */
125 26418 len = table_vlc[code][1] + 1;
126 } else {
127 940814 len = table_vlc[MPEG12_RL_NB_ELEMS][1] + 6;
128
129
1/2
✓ Branch 0 taken 940814 times.
✗ Branch 1 not taken.
940814 if (alevel < 128)
130 940814 len += 8;
131 else
132 len += 16;
133 }
134
135 967232 uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
136 }
137 }
138 119 }
139
140 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
141 307263 static void put_header(MPVEncContext *const s, uint32_t header)
142 {
143 307263 align_put_bits(&s->pb);
144 307263 put_bits32(&s->pb, header);
145 307263 }
146
147 /* put sequence header if needed */
148 3576 static void mpeg1_encode_sequence_header(MPEG12EncContext *mpeg12)
149 {
150 3576 MPVEncContext *const s = &mpeg12->mpeg.s;
151 unsigned int vbv_buffer_size, fps, v;
152 int constraint_parameter_flag;
153 3576 AVRational framerate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index];
154 uint64_t time_code;
155 3576 int64_t best_aspect_error = INT64_MAX;
156 3576 AVRational aspect_ratio = s->c.avctx->sample_aspect_ratio;
157 int aspect_ratio_info;
158
159 3576 put_bits_assume_flushed(&s->pb);
160
161
2/2
✓ Branch 0 taken 3163 times.
✓ Branch 1 taken 413 times.
3576 if (!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY))
162 3163 return;
163
164
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 405 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
413 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 413 put_header(s, SEQ_START_CODE);
169
170 413 put_sbits(&s->pb, 12, s->c.width & 0xFFF);
171 413 put_sbits(&s->pb, 12, s->c.height & 0xFFF);
172
173
2/2
✓ Branch 0 taken 5782 times.
✓ Branch 1 taken 413 times.
6195 for (int i = 1; i < 15; i++) {
174 5782 int64_t error = aspect_ratio.num * (1LL<<32) / aspect_ratio.den;
175
4/4
✓ Branch 0 taken 5040 times.
✓ Branch 1 taken 742 times.
✓ Branch 2 taken 360 times.
✓ Branch 3 taken 4680 times.
5782 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
176 1102 error -= (1LL<<32) / ff_mpeg1_aspect[i];
177 else
178 4680 error -= (1LL<<32)*ff_mpeg2_aspect[i].num * s->c.height / s->c.width / ff_mpeg2_aspect[i].den;
179
180 5782 error = FFABS(error);
181
182
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 5366 times.
5782 if (error - 2 <= best_aspect_error) {
183 416 best_aspect_error = error;
184 416 aspect_ratio_info = i;
185 }
186 }
187
188 413 put_bits(&s->pb, 4, aspect_ratio_info);
189 413 put_bits(&s->pb, 4, mpeg12->frame_rate_index);
190
191
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 260 times.
413 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 260 v = 0x3FFFF;
197 }
198
199
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 260 times.
413 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 260 vbv_buffer_size = av_rescale_rnd(mpeg12->mpeg.bit_rate, 20, 1151929 / 2, AV_ROUND_ZERO) * 8 * 1024;
205 413 vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
206
207 413 put_sbits(&s->pb, 18, v);
208 413 put_bits(&s->pb, 1, 1); // marker
209 413 put_sbits(&s->pb, 10, vbv_buffer_size);
210
211 413 constraint_parameter_flag =
212 823 s->c.width <= 768 &&
213
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 150 times.
410 s->c.height <= 576 &&
214
2/2
✓ Branch 0 taken 237 times.
✓ Branch 1 taken 23 times.
260 s->c.mb_width * s->c.mb_height <= 396 &&
215
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 6 times.
237 s->c.mb_width * s->c.mb_height * framerate.num <= 396 * 25 * framerate.den &&
216
1/2
✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
231 framerate.num <= framerate.den * 30 &&
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 231 times.
231 s->c.avctx->me_range &&
218 s->c.avctx->me_range < 128 &&
219 vbv_buffer_size <= 20 &&
220
2/2
✓ Branch 0 taken 410 times.
✓ Branch 1 taken 3 times.
823 v <= 1856000 / 400 &&
221 s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO;
222
223 413 put_bits(&s->pb, 1, constraint_parameter_flag);
224
225 413 ff_write_quant_matrix(&s->pb, s->c.avctx->intra_matrix);
226 413 ff_write_quant_matrix(&s->pb, s->c.avctx->inter_matrix);
227
228
2/2
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 53 times.
413 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
229 const AVFrameSideData *side_data;
230 360 int width = s->c.width;
231 360 int height = s->c.height;
232 int use_seq_disp_ext;
233
234 360 put_header(s, EXT_START_CODE);
235 360 put_bits(&s->pb, 4, 1); // seq ext
236
237 360 put_bits(&s->pb, 1, s->c.avctx->profile == AV_PROFILE_MPEG2_422); // escx 1 for 4:2:2 profile
238
239 360 put_bits(&s->pb, 3, s->c.avctx->profile); // profile
240 360 put_bits(&s->pb, 4, s->c.avctx->level); // level
241
242 360 put_bits(&s->pb, 1, s->c.progressive_sequence);
243 360 put_bits(&s->pb, 2, s->c.chroma_format);
244 360 put_bits(&s->pb, 2, s->c.width >> 12);
245 360 put_bits(&s->pb, 2, s->c.height >> 12);
246 360 put_bits(&s->pb, 12, v >> 18); // bitrate ext
247 360 put_bits(&s->pb, 1, 1); // marker
248 360 put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
249 360 put_bits(&s->pb, 1, s->c.low_delay);
250 360 put_bits(&s->pb, 2, mpeg12->frame_rate_ext.num-1); // frame_rate_ext_n
251 360 put_bits(&s->pb, 5, mpeg12->frame_rate_ext.den-1); // frame_rate_ext_d
252
253 360 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 355 times.
360 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 1080 use_seq_disp_ext = (width != s->c.width ||
263
1/2
✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
360 height != s->c.height ||
264
2/2
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 9 times.
360 s->c.avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
265
2/2
✓ Branch 0 taken 348 times.
✓ Branch 1 taken 3 times.
351 s->c.avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
266
2/4
✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 348 times.
✗ Branch 3 not taken.
1068 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 360 times.
✗ Branch 1 not taken.
360 if (mpeg12->seq_disp_ext == 1 ||
270
3/4
✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 348 times.
360 (mpeg12->seq_disp_ext == -1 && use_seq_disp_ext)) {
271 12 put_header(s, EXT_START_CODE);
272 12 put_bits(&s->pb, 4, 2); // sequence display extension
273 12 put_bits(&s->pb, 3, mpeg12->video_format); // video_format
274 12 put_bits(&s->pb, 1, 1); // colour_description
275 12 put_bits(&s->pb, 8, s->c.avctx->color_primaries); // colour_primaries
276 12 put_bits(&s->pb, 8, s->c.avctx->color_trc); // transfer_characteristics
277 12 put_bits(&s->pb, 8, s->c.avctx->colorspace); // matrix_coefficients
278 12 put_bits(&s->pb, 14, width); // display_horizontal_size
279 12 put_bits(&s->pb, 1, 1); // marker_bit
280 12 put_bits(&s->pb, 14, height); // display_vertical_size
281 12 put_bits(&s->pb, 3, 0); // remaining 3 bits are zero padding
282 }
283 }
284
285 413 put_header(s, GOP_START_CODE);
286 413 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 413 fps = (framerate.num + framerate.den / 2) / framerate.den;
290 413 time_code = s->c.cur_pic.ptr->coded_picture_number +
291 413 mpeg12->timecode_frame_start;
292
293 413 mpeg12->gop_picture_number = s->c.cur_pic.ptr->coded_picture_number;
294
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 413 times.
413 av_assert0(mpeg12->drop_frame_timecode == !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
296
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 404 times.
413 if (mpeg12->drop_frame_timecode)
297 9 time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
298
299 413 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
300 413 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
301 413 put_bits(&s->pb, 1, 1);
302 413 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
303 413 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
304 413 put_bits(&s->pb, 1, !!(s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) ||
305
5/6
✓ Branch 0 taken 413 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 263 times.
✓ Branch 3 taken 150 times.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 209 times.
413 mpeg12->mpeg.intra_only || !mpeg12->gop_picture_number);
306 413 put_bits(&s->pb, 1, 0); // broken link
307 }
308
309 1776953 static inline void encode_mb_skip_run(MPVEncContext *const s, int run)
310 {
311
2/2
✓ Branch 0 taken 69199 times.
✓ Branch 1 taken 1776953 times.
1846152 while (run >= 33) {
312 69199 put_bits(&s->pb, 11, 0x008);
313 69199 run -= 33;
314 }
315 1776953 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
316 1776953 ff_mpeg12_mbAddrIncrTable[run][0]);
317 1776953 }
318
319 481417 static av_always_inline void put_qscale(MPVEncContext *const s)
320 {
321 481417 put_bits(&s->pb, 5, s->c.qscale);
322 481417 }
323
324 299259 void ff_mpeg1_encode_slice_header(MPVEncContext *const s)
325 {
326
3/4
✓ Branch 0 taken 291986 times.
✓ Branch 1 taken 7273 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 291986 times.
299259 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 299259 put_header(s, SLICE_MIN_START_CODE + s->c.mb_y);
333 }
334 299259 put_qscale(s);
335 /* slice extra information */
336 299259 put_bits(&s->pb, 1, 0);
337 299259 }
338
339 3576 static int mpeg1_encode_picture_header(MPVMainEncContext *const m)
340 {
341 3576 MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)m;
342 3576 MPVEncContext *const s = &m->s;
343 const AVFrameSideData *side_data;
344
345 3576 put_bits_assume_flushed(&s->pb);
346
347 3576 mpeg1_encode_sequence_header(mpeg12);
348
349 /* MPEG-1 picture header */
350 3576 put_header(s, PICTURE_START_CODE);
351 /* temporal reference */
352
353 3576 put_bits(&s->pb, 10,
354 3576 (s->picture_number - mpeg12->gop_picture_number) & 0x3ff);
355 3576 put_bits(&s->pb, 3, s->c.pict_type);
356
357 3576 m->vbv_delay_pos = put_bytes_count(&s->pb, 0);
358 3576 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 1120 times.
✓ Branch 1 taken 2456 times.
3576 if (s->c.pict_type == AV_PICTURE_TYPE_P ||
362
2/2
✓ Branch 0 taken 707 times.
✓ Branch 1 taken 413 times.
1120 s->c.pict_type == AV_PICTURE_TYPE_B) {
363 3163 put_bits(&s->pb, 1, 0); /* half pel coordinates */
364
2/2
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 2736 times.
3163 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 2736 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 2869 times.
3576 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 3576 put_bits(&s->pb, 1, 0); /* extra bit picture */
380
381 3576 s->c.frame_pred_frame_dct = 1;
382
2/2
✓ Branch 0 taken 3096 times.
✓ Branch 1 taken 480 times.
3576 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
383 3096 put_header(s, EXT_START_CODE);
384 3096 put_bits(&s->pb, 4, 8); /* pic ext */
385
2/2
✓ Branch 0 taken 923 times.
✓ Branch 1 taken 2173 times.
3096 if (s->c.pict_type == AV_PICTURE_TYPE_P ||
386
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 360 times.
923 s->c.pict_type == AV_PICTURE_TYPE_B) {
387 2736 put_bits(&s->pb, 4, s->f_code);
388 2736 put_bits(&s->pb, 4, s->f_code);
389 } else {
390 360 put_bits(&s->pb, 8, 255);
391 }
392
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 2533 times.
3096 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 2533 put_bits(&s->pb, 8, 255);
397 }
398 3096 put_bits(&s->pb, 2, s->c.intra_dc_precision);
399
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3096 times.
3096 av_assert0(s->c.picture_structure == PICT_FRAME);
401 3096 put_bits(&s->pb, 2, s->c.picture_structure);
402
2/2
✓ Branch 0 taken 1946 times.
✓ Branch 1 taken 1150 times.
3096 if (s->c.progressive_sequence)
403 1946 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 3096 s->c.frame_pred_frame_dct = s->c.progressive_sequence;
408
409 3096 put_bits(&s->pb, 1, s->c.frame_pred_frame_dct);
410 3096 put_bits(&s->pb, 1, s->c.concealment_motion_vectors);
411 3096 put_bits(&s->pb, 1, s->c.q_scale_type);
412 3096 put_bits(&s->pb, 1, s->c.intra_vlc_format);
413 3096 put_bits(&s->pb, 1, s->c.alternate_scan);
414 3096 put_bits(&s->pb, 1, s->c.repeat_first_field);
415 3096 s->c.progressive_frame = s->c.progressive_sequence;
416 /* chroma_420_type */
417
2/2
✓ Branch 0 taken 2746 times.
✓ Branch 1 taken 350 times.
5842 put_bits(&s->pb, 1, s->c.chroma_format ==
418 2746 CHROMA_420 ? s->c.progressive_frame : 0);
419 3096 put_bits(&s->pb, 1, s->c.progressive_frame);
420 3096 put_bits(&s->pb, 1, 0); /* composite_display_flag */
421 }
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3576 times.
3576 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 3576 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 3576 times.
3576 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 3096 times.
✓ Branch 1 taken 480 times.
3576 if (CONFIG_MPEG2VIDEO_ENCODER && mpeg12->a53_cc) {
466 3096 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 2961 times.
3096 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 3576 s->c.mb_y = 0;
491 3576 ff_mpeg1_encode_slice_header(s);
492
493 3576 return 0;
494 }
495
496 1455808 static inline void put_mb_modes(MPVEncContext *const s, int n, int bits,
497 int has_mv, int field_motion)
498 {
499 1455808 put_bits(&s->pb, n, bits);
500
2/2
✓ Branch 0 taken 751049 times.
✓ Branch 1 taken 704759 times.
1455808 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 1455808 }
507
508 // RAL: Parameter added: f_or_b_code
509 3283796 static void mpeg1_encode_motion(MPVEncContext *const s, int val, int f_or_b_code)
510 {
511
2/2
✓ Branch 0 taken 1313162 times.
✓ Branch 1 taken 1970634 times.
3283796 if (val == 0) {
512 /* zero vector, corresponds to ff_mpeg12_mbMotionVectorTable[0] */
513 1313162 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 3283796 }
545
546 3438674 static inline void encode_dc(MPVEncContext *const s, int diff, int component)
547 {
548 3438674 unsigned int diff_u = diff + 255;
549
2/2
✓ Branch 0 taken 162210 times.
✓ Branch 1 taken 3276464 times.
3438674 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 1807388 times.
✓ Branch 1 taken 1469076 times.
3276464 if (component == 0)
570 1807388 put_bits(&s->pb,
571 1807388 mpeg1_lum_dc_uni[diff + 255] & 0xFF,
572 1807388 mpeg1_lum_dc_uni[diff + 255] >> 8);
573 else
574 1469076 put_bits(&s->pb,
575 1469076 mpeg1_chr_dc_uni[diff + 255] & 0xFF,
576 1469076 mpeg1_chr_dc_uni[diff + 255] >> 8);
577 }
578 3438674 }
579
580 7011737 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 7011737 const uint16_t (*table_vlc)[2] = ff_mpeg1_vlc_table;
585
586 7011737 last_index = s->c.block_last_index[n];
587
588 /* DC coef */
589
2/2
✓ Branch 0 taken 3438674 times.
✓ Branch 1 taken 3573063 times.
7011737 if (s->c.mb_intra) {
590
2/2
✓ Branch 0 taken 1539906 times.
✓ Branch 1 taken 1898768 times.
3438674 component = (n <= 3 ? 0 : (n & 1) + 1);
591 3438674 dc = block[0]; /* overflow is impossible */
592 3438674 diff = dc - s->c.last_dc[component];
593 3438674 encode_dc(s, diff, component);
594 3438674 s->c.last_dc[component] = dc;
595 3438674 i = 1;
596
2/2
✓ Branch 0 taken 2641700 times.
✓ Branch 1 taken 796974 times.
3438674 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 3573063 level = block[0];
602
2/2
✓ Branch 0 taken 1068227 times.
✓ Branch 1 taken 2504836 times.
3573063 if (abs(level) == 1) {
603 1068227 code = ((uint32_t)level >> 31); /* the sign bit */
604 1068227 put_bits(&s->pb, 2, code | 0x02);
605 1068227 i = 1;
606 } else {
607 2504836 i = 0;
608 2504836 last_non_zero = -1;
609 2504836 goto next_coef;
610 }
611 }
612
613 /* now quantify & encode AC coefs */
614 4506901 last_non_zero = i - 1;
615
616
2/2
✓ Branch 0 taken 163845356 times.
✓ Branch 1 taken 7011737 times.
170857093 for (; i <= last_index; i++) {
617 163845356 j = s->c.intra_scantable.permutated[i];
618 163845356 level = block[j];
619
620 166350192 next_coef:
621 /* encode using VLC */
622
2/2
✓ Branch 0 taken 68884026 times.
✓ Branch 1 taken 97466166 times.
166350192 if (level != 0) {
623 68884026 run = i - last_non_zero - 1;
624
625 68884026 alevel = level;
626 68884026 MASK_ABS(sign, alevel);
627 68884026 sign &= 1;
628
629
2/2
✓ Branch 0 taken 66711344 times.
✓ Branch 1 taken 2172682 times.
68884026 if (alevel <= mpeg12_max_level[run]) {
630 66711344 code = mpeg12_index_run[run] + alevel - 1;
631 /* store the VLC & sign at once */
632 66711344 put_bits(&s->pb, table_vlc[code][1] + 1,
633 66711344 (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 2172682 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 2090002 times.
2172682 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 2090002 put_sbits(&s->pb, 12, level);
651 }
652 }
653 68884026 last_non_zero = i;
654 }
655 }
656 /* end of block */
657 7011737 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
658 7011737 }
659
660 1910478 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 1910478 const int mb_x = s->c.mb_x;
670 1910478 const int mb_y = s->c.mb_y;
671
4/4
✓ Branch 0 taken 331099 times.
✓ Branch 1 taken 1579379 times.
✓ Branch 2 taken 326442 times.
✓ Branch 3 taken 4657 times.
1910478 const int first_mb = mb_x == s->c.resync_mb_x && mb_y == s->c.resync_mb_y;
672
673 /* compute cbp */
674 1910478 cbp = 0;
675
2/2
✓ Branch 0 taken 12748778 times.
✓ Branch 1 taken 1910478 times.
14659256 for (i = 0; i < mb_block_count; i++)
676
2/2
✓ Branch 0 taken 7011737 times.
✓ Branch 1 taken 5737041 times.
12748778 if (s->c.block_last_index[i] >= 0)
677 7011737 cbp |= 1 << (mb_block_count - 1 - i);
678
679
6/6
✓ Branch 0 taken 454670 times.
✓ Branch 1 taken 1455808 times.
✓ Branch 2 taken 443337 times.
✓ Branch 3 taken 11333 times.
✓ Branch 4 taken 423114 times.
✓ Branch 5 taken 20223 times.
1910478 if (cbp == 0 && !first_mb && s->c.mv_type == MV_TYPE_16X16 &&
680
2/2
✓ Branch 0 taken 12180 times.
✓ Branch 1 taken 410934 times.
423114 (mb_x != s->c.mb_width - 1 ||
681
6/6
✓ Branch 0 taken 11420 times.
✓ Branch 1 taken 760 times.
✓ Branch 2 taken 9460 times.
✓ Branch 3 taken 1960 times.
✓ Branch 4 taken 552 times.
✓ Branch 5 taken 8908 times.
12180 (mb_y != s->c.end_mb_y - 1 && IS_MPEG1(s))) &&
682
4/4
✓ Branch 0 taken 241250 times.
✓ Branch 1 taken 170236 times.
✓ Branch 2 taken 112067 times.
✓ Branch 3 taken 129183 times.
411486 ((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 133525 s->mb_skip_run++;
691 133525 s->c.qscale -= s->dquant;
692 133525 s->misc_bits++;
693 133525 s->last_bits++;
694
2/2
✓ Branch 0 taken 129183 times.
✓ Branch 1 taken 4342 times.
133525 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
695 129183 s->c.last_mv[0][0][0] =
696 129183 s->c.last_mv[0][0][1] =
697 129183 s->c.last_mv[0][1][0] =
698 129183 s->c.last_mv[0][1][1] = 0;
699 }
700 } else {
701
2/2
✓ Branch 0 taken 326442 times.
✓ Branch 1 taken 1450511 times.
1776953 if (first_mb) {
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 326442 times.
326442 av_assert0(s->mb_skip_run == 0);
703 326442 encode_mb_skip_run(s, s->c.mb_x);
704 } else {
705 1450511 encode_mb_skip_run(s, s->mb_skip_run);
706 }
707
708
2/2
✓ Branch 0 taken 400954 times.
✓ Branch 1 taken 1375999 times.
1776953 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
709
3/4
✓ Branch 0 taken 43090 times.
✓ Branch 1 taken 357864 times.
✓ Branch 2 taken 43090 times.
✗ Branch 3 not taken.
400954 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 357864 put_mb_modes(s, 1, 1, 0, 0);
716 357864 s->c.qscale -= s->dquant;
717 }
718 400954 s->misc_bits += get_bits_diff(s);
719 400954 s->i_count++;
720
2/2
✓ Branch 0 taken 73738 times.
✓ Branch 1 taken 1302261 times.
1375999 } 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 721318 times.
✓ Branch 1 taken 580943 times.
1302261 } else if (s->c.pict_type == AV_PICTURE_TYPE_P) {
732
2/2
✓ Branch 0 taken 698469 times.
✓ Branch 1 taken 22849 times.
721318 if (s->c.mv_type == MV_TYPE_16X16) {
733
2/2
✓ Branch 0 taken 573480 times.
✓ Branch 1 taken 124989 times.
698469 if (cbp != 0) {
734
2/2
✓ Branch 0 taken 61233 times.
✓ Branch 1 taken 512247 times.
573480 if ((motion_x | motion_y) == 0) {
735
2/2
✓ Branch 0 taken 1554 times.
✓ Branch 1 taken 59679 times.
61233 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 59679 put_mb_modes(s, 2, 1, 0, 0);
742 }
743 61233 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 124989 put_bits(&s->pb, 3, 1); /* motion only */
764
2/2
✓ Branch 0 taken 24679 times.
✓ Branch 1 taken 100310 times.
124989 if (!s->c.frame_pred_frame_dct)
765 24679 put_bits(&s->pb, 2, 2); /* motion_type: frame */
766 124989 s->misc_bits += get_bits_diff(s);
767 // RAL: f_code parameter added
768 124989 mpeg1_encode_motion(s,
769 124989 motion_x - s->c.last_mv[0][0][0],
770 s->f_code);
771 // RAL: f_code parameter added
772 124989 mpeg1_encode_motion(s,
773 124989 motion_y - s->c.last_mv[0][0][1],
774 s->f_code);
775 124989 s->c.qscale -= s->dquant;
776 124989 s->mv_bits += get_bits_diff(s);
777 }
778 698469 s->c.last_mv[0][1][0] = s->c.last_mv[0][0][0] = motion_x;
779 698469 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 594824 times.
✓ Branch 1 taken 126494 times.
721318 if (cbp) {
810
2/2
✓ Branch 0 taken 520012 times.
✓ Branch 1 taken 74812 times.
594824 if (chroma_y_shift) {
811 520012 put_bits(&s->pb,
812 520012 ff_mpeg12_mbPatTable[cbp][1],
813 520012 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 11946332 times.
✓ Branch 1 taken 1776953 times.
13723285 for (i = 0; i < mb_block_count; i++)
925
2/2
✓ Branch 0 taken 7011737 times.
✓ Branch 1 taken 4934595 times.
11946332 if (cbp & (1 << (mb_block_count - 1 - i)))
926 7011737 mpeg1_encode_block(s, block[i], i);
927 1776953 s->mb_skip_run = 0;
928
2/2
✓ Branch 0 taken 474692 times.
✓ Branch 1 taken 1302261 times.
1776953 if (s->c.mb_intra)
929 474692 s->i_tex_bits += get_bits_diff(s);
930 else
931 1302261 s->p_tex_bits += get_bits_diff(s);
932 }
933 1910478 }
934
935 1910478 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 1435786 times.
✓ Branch 1 taken 474692 times.
1910478 if (!s->c.mb_intra)
939 1435786 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 1267523 times.
✓ Branch 1 taken 642955 times.
1910478 if (s->c.chroma_format == CHROMA_420)
941 1267523 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 1910478 }
945
946 55 static av_cold void mpeg12_encode_init_static(void)
947 {
948 55 ff_rl_init_level_run(mpeg12_max_level, mpeg12_index_run,
949 ff_mpeg12_run, ff_mpeg12_level, MPEG12_RL_NB_ELEMS);
950
951 55 ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run,
952 ff_mpeg1_vlc_table, uni_mpeg1_ac_vlc_len);
953 55 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 28105 times.
✓ Branch 1 taken 55 times.
28160 for (int i = -255; i < 256; i++) {
958 int adiff, index;
959 int bits, code;
960 28105 int diff = i;
961
962 28105 adiff = FFABS(diff);
963
2/2
✓ Branch 0 taken 14025 times.
✓ Branch 1 taken 14080 times.
28105 if (diff < 0)
964 14025 diff--;
965 28105 index = av_log2(2 * adiff);
966
967 28105 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
968 28105 code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
969 28105 av_zero_extend(diff, index);
970 28105 mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
971
972 28105 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
973 28105 code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
974 28105 av_zero_extend(diff, index);
975 28105 mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
976 }
977
978
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 55 times.
440 for (int f_code = 1; f_code <= MAX_FCODE; f_code++)
979
2/2
✓ Branch 0 taken 6308225 times.
✓ Branch 1 taken 385 times.
6308610 for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) {
980 int len;
981
982
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 6307840 times.
6308225 if (mv == 0) {
983 385 len = 1; /* ff_mpeg12_mbMotionVectorTable[0][1] */
984 } else {
985 int val, bit_size, code;
986
987 6307840 bit_size = f_code - 1;
988
989 6307840 val = mv;
990
2/2
✓ Branch 0 taken 3153920 times.
✓ Branch 1 taken 3153920 times.
6307840 if (val < 0)
991 3153920 val = -val;
992 6307840 val--;
993 6307840 code = (val >> bit_size) + 1;
994
2/2
✓ Branch 0 taken 223520 times.
✓ Branch 1 taken 6084320 times.
6307840 if (code < 17)
995 223520 len = ff_mpeg12_mbMotionVectorTable[code][1] +
996 1 + bit_size;
997 else
998 6084320 len = 10 /* ff_mpeg12_mbMotionVectorTable[16][1] */ +
999 2 + bit_size;
1000 }
1001
1002 6308225 mv_penalty[f_code][mv + MAX_DMV] = len;
1003 }
1004
1005
1006
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 55 times.
440 for (int f_code = MAX_FCODE; f_code > 0; f_code--)
1007
2/2
✓ Branch 0 taken 223520 times.
✓ Branch 1 taken 385 times.
223905 for (int mv = -(8 << f_code); mv < (8 << f_code); mv++)
1008 223520 fcode_tab[mv + MAX_MV] = f_code;
1009 55 }
1010
1011 55 static av_cold int find_frame_rate_index(AVCodecContext *avctx, MPEG12EncContext *mpeg12)
1012 {
1013 55 AVRational bestq = (AVRational) {0, 0};
1014 AVRational ext;
1015 55 AVRational target = av_inv_q(avctx->time_base);
1016
1017
2/2
✓ Branch 0 taken 499 times.
✓ Branch 1 taken 1 times.
500 for (int i = 1; i < 14; i++) {
1018
4/4
✓ Branch 0 taken 486 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 432 times.
499 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
1019 i >= 9)
1020 54 break;
1021
1022
2/2
✓ Branch 0 taken 1780 times.
✓ Branch 1 taken 445 times.
2225 for (ext.num = 1; ext.num <= 4; ext.num++) {
1023
2/2
✓ Branch 0 taken 56960 times.
✓ Branch 1 taken 1780 times.
58740 for (ext.den = 1; ext.den <= 32; ext.den++) {
1024 56960 AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]);
1025
1026
6/6
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 45696 times.
✓ Branch 2 taken 352 times.
✓ Branch 3 taken 10912 times.
✓ Branch 4 taken 264 times.
✓ Branch 5 taken 88 times.
56960 if (avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
1027 26170 continue;
1028
2/2
✓ Branch 0 taken 14994 times.
✓ Branch 1 taken 30790 times.
45784 if (av_gcd(ext.den, ext.num) != 1)
1029 14994 continue;
1030
1031
2/2
✓ Branch 0 taken 30735 times.
✓ Branch 1 taken 55 times.
30790 if ( bestq.num==0
1032
2/2
✓ Branch 1 taken 30597 times.
✓ Branch 2 taken 138 times.
30735 || av_nearer_q(target, bestq, q) < 0
1033
5/6
✓ Branch 0 taken 11329 times.
✓ Branch 1 taken 19268 times.
✓ Branch 2 taken 286 times.
✓ Branch 3 taken 11043 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 286 times.
30597 || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) {
1034 193 bestq = q;
1035 193 mpeg12->frame_rate_index = i;
1036 193 mpeg12->frame_rate_ext.num = ext.num;
1037 193 mpeg12->frame_rate_ext.den = ext.den;
1038 }
1039 }
1040 }
1041 }
1042
1043
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 54 times.
55 if (av_cmp_q(target, bestq))
1044 1 return -1;
1045 else
1046 54 return 0;
1047 }
1048
1049 55 static av_cold int encode_init(AVCodecContext *avctx)
1050 {
1051 static AVOnce init_static_once = AV_ONCE_INIT;
1052 55 MPEG12EncContext *const mpeg12 = avctx->priv_data;
1053 55 MPVMainEncContext *const m = &mpeg12->mpeg;
1054 55 MPVEncContext *const s = &m->s;
1055 int ret;
1056
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 11 times.
55 int max_size = avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 16383 : 4095;
1057
1058
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 55 times.
55 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 55 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
55 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 54 times.
✓ Branch 1 taken 1 times.
55 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
1070
2/4
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
54 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 54 times.
55 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 55 times.
✗ Branch 1 not taken.
55 if (avctx->profile == AV_PROFILE_UNKNOWN) {
1086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 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 55 avctx->profile = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? AV_PROFILE_MPEG2_MAIN
1092
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5 times.
55 : AV_PROFILE_MPEG2_422;
1093 }
1094
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 if (avctx->level == AV_LEVEL_UNKNOWN) {
1095
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 50 times.
55 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 50 times.
✗ Branch 1 not taken.
50 if (avctx->profile != AV_PROFILE_MPEG2_HIGH &&
1102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 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 49 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
50 if (avctx->width <= 720 && avctx->height <= 576)
1108 49 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 55 m->encode_picture_header = mpeg1_encode_picture_header;
1117 55 s->encode_mb = mpeg12_encode_mb;
1118
1119 55 s->me.mv_penalty = mv_penalty;
1120 55 m->fcode_tab = fcode_tab + MAX_MV;
1121
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 44 times.
55 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1122 11 s->min_qcoeff = -255;
1123 11 s->max_qcoeff = 255;
1124 } else {
1125 44 s->min_qcoeff = -2047;
1126 44 s->max_qcoeff = 2047;
1127 44 s->mpeg_quant = 1;
1128 }
1129
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 42 times.
55 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 42 s->intra_ac_vlc_length =
1134 42 s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1135 }
1136 55 s->inter_ac_vlc_length =
1137 55 s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1138
1139 55 ret = ff_mpv_encode_init(avctx);
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (ret < 0)
1141 return ret;
1142
1143
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 44 times.
55 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 54 times.
55 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 53 times.
55 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 55 times.
55 if (mpeg12->drop_frame_timecode)
1189 mpeg12->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME;
1190
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
55 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 49 times.
55 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 49 mpeg12->timecode_frame_start = 0; // default is -1
1205 }
1206
1207 55 ff_thread_once(&init_static_once, mpeg12_encode_init_static);
1208
1209 55 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