FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12enc.c
Date: 2025-06-01 09:29:47
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 // RAL: s->c.picture_number instead of s->fake_picture_number
354 3576 put_bits(&s->pb, 10,
355 3576 (s->c.picture_number - mpeg12->gop_picture_number) & 0x3ff);
356 3576 put_bits(&s->pb, 3, s->c.pict_type);
357
358 3576 m->vbv_delay_pos = put_bytes_count(&s->pb, 0);
359 3576 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
360
361 // RAL: Forward f_code also needed for B-frames
362
2/2
✓ Branch 0 taken 1120 times.
✓ Branch 1 taken 2456 times.
3576 if (s->c.pict_type == AV_PICTURE_TYPE_P ||
363
2/2
✓ Branch 0 taken 707 times.
✓ Branch 1 taken 413 times.
1120 s->c.pict_type == AV_PICTURE_TYPE_B) {
364 3163 put_bits(&s->pb, 1, 0); /* half pel coordinates */
365
2/2
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 2736 times.
3163 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
366 427 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
367 else
368 2736 put_bits(&s->pb, 3, 7); /* forward_f_code */
369 }
370
371 // RAL: Backward f_code necessary for B-frames
372
2/2
✓ Branch 0 taken 707 times.
✓ Branch 1 taken 2869 times.
3576 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
373 707 put_bits(&s->pb, 1, 0); /* half pel coordinates */
374
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 563 times.
707 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
375 144 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
376 else
377 563 put_bits(&s->pb, 3, 7); /* backward_f_code */
378 }
379
380 3576 put_bits(&s->pb, 1, 0); /* extra bit picture */
381
382 3576 s->c.frame_pred_frame_dct = 1;
383
2/2
✓ Branch 0 taken 3096 times.
✓ Branch 1 taken 480 times.
3576 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
384 3096 put_header(s, EXT_START_CODE);
385 3096 put_bits(&s->pb, 4, 8); /* pic ext */
386
2/2
✓ Branch 0 taken 923 times.
✓ Branch 1 taken 2173 times.
3096 if (s->c.pict_type == AV_PICTURE_TYPE_P ||
387
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 360 times.
923 s->c.pict_type == AV_PICTURE_TYPE_B) {
388 2736 put_bits(&s->pb, 4, s->f_code);
389 2736 put_bits(&s->pb, 4, s->f_code);
390 } else {
391 360 put_bits(&s->pb, 8, 255);
392 }
393
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 2533 times.
3096 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
394 563 put_bits(&s->pb, 4, s->b_code);
395 563 put_bits(&s->pb, 4, s->b_code);
396 } else {
397 2533 put_bits(&s->pb, 8, 255);
398 }
399 3096 put_bits(&s->pb, 2, s->c.intra_dc_precision);
400
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3096 times.
3096 av_assert0(s->c.picture_structure == PICT_FRAME);
402 3096 put_bits(&s->pb, 2, s->c.picture_structure);
403
2/2
✓ Branch 0 taken 1946 times.
✓ Branch 1 taken 1150 times.
3096 if (s->c.progressive_sequence)
404 1946 put_bits(&s->pb, 1, 0); /* no repeat */
405 else
406 1150 put_bits(&s->pb, 1, !!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
407 /* XXX: optimize the generation of this flag with entropy measures */
408 3096 s->c.frame_pred_frame_dct = s->c.progressive_sequence;
409
410 3096 put_bits(&s->pb, 1, s->c.frame_pred_frame_dct);
411 3096 put_bits(&s->pb, 1, s->c.concealment_motion_vectors);
412 3096 put_bits(&s->pb, 1, s->c.q_scale_type);
413 3096 put_bits(&s->pb, 1, s->c.intra_vlc_format);
414 3096 put_bits(&s->pb, 1, s->c.alternate_scan);
415 3096 put_bits(&s->pb, 1, s->c.repeat_first_field);
416 3096 s->c.progressive_frame = s->c.progressive_sequence;
417 /* chroma_420_type */
418
2/2
✓ Branch 0 taken 2746 times.
✓ Branch 1 taken 350 times.
5842 put_bits(&s->pb, 1, s->c.chroma_format ==
419 2746 CHROMA_420 ? s->c.progressive_frame : 0);
420 3096 put_bits(&s->pb, 1, s->c.progressive_frame);
421 3096 put_bits(&s->pb, 1, 0); /* composite_display_flag */
422 }
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3576 times.
3576 if (mpeg12->scan_offset) {
424 int i;
425
426 put_header(s, USER_START_CODE);
427 for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
428 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
429 }
430 3576 side_data = av_frame_get_side_data(s->c.cur_pic.ptr->f,
431 AV_FRAME_DATA_STEREO3D);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3576 times.
3576 if (side_data) {
433 const AVStereo3D *stereo = (AVStereo3D *)side_data->data;
434 uint8_t fpa_type;
435
436 switch (stereo->type) {
437 case AV_STEREO3D_SIDEBYSIDE:
438 fpa_type = 0x03;
439 break;
440 case AV_STEREO3D_TOPBOTTOM:
441 fpa_type = 0x04;
442 break;
443 case AV_STEREO3D_2D:
444 fpa_type = 0x08;
445 break;
446 case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
447 fpa_type = 0x23;
448 break;
449 default:
450 fpa_type = 0;
451 break;
452 }
453
454 if (fpa_type != 0) {
455 put_header(s, USER_START_CODE);
456 // S3D_video_format_signaling_identifier
457 put_bits32(&s->pb, MKBETAG('J','P','3','D'));
458 put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
459
460 put_bits(&s->pb, 1, 1); // reserved_bit
461 put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
462 put_bits(&s->pb, 16, 0x04FF); // reserved_data
463 }
464 }
465
466
2/2
✓ Branch 0 taken 3096 times.
✓ Branch 1 taken 480 times.
3576 if (CONFIG_MPEG2VIDEO_ENCODER && mpeg12->a53_cc) {
467 3096 side_data = av_frame_get_side_data(s->c.cur_pic.ptr->f,
468 AV_FRAME_DATA_A53_CC);
469
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 2961 times.
3096 if (side_data) {
470
3/4
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
135 if (side_data->size <= A53_MAX_CC_COUNT * 3 && side_data->size % 3 == 0) {
471 134 put_header (s, USER_START_CODE);
472
473 134 put_bits32(&s->pb, MKBETAG('G','A','9','4')); // user_identifier
474 134 put_bits(&s->pb, 8, 3); // user_data_type_code
475 134 put_bits(&s->pb, 8,
476 134 (side_data->size / 3 & A53_MAX_CC_COUNT) | 0x40); // flags, cc_count
477 134 put_bits(&s->pb, 8, 0xff); // em_data
478
479
2/2
✓ Branch 0 taken 8040 times.
✓ Branch 1 taken 134 times.
8174 for (int i = 0; i < side_data->size; i++)
480 8040 put_bits(&s->pb, 8, side_data->data[i]);
481
482 134 put_bits(&s->pb, 8, 0xff); // marker_bits
483 } else {
484 1 av_log(s->c.avctx, AV_LOG_WARNING,
485 "Closed Caption size (%"SIZE_SPECIFIER") can not exceed "
486 1 "93 bytes and must be a multiple of 3\n", side_data->size);
487 }
488 }
489 }
490
491 3576 s->c.mb_y = 0;
492 3576 ff_mpeg1_encode_slice_header(s);
493
494 3576 return 0;
495 }
496
497 1455808 static inline void put_mb_modes(MPVEncContext *const s, int n, int bits,
498 int has_mv, int field_motion)
499 {
500 1455808 put_bits(&s->pb, n, bits);
501
2/2
✓ Branch 0 taken 751049 times.
✓ Branch 1 taken 704759 times.
1455808 if (!s->c.frame_pred_frame_dct) {
502
2/2
✓ Branch 0 taken 407012 times.
✓ Branch 1 taken 344037 times.
751049 if (has_mv)
503 /* motion_type: frame/field */
504 407012 put_bits(&s->pb, 2, 2 - field_motion);
505 751049 put_bits(&s->pb, 1, s->c.interlaced_dct);
506 }
507 1455808 }
508
509 // RAL: Parameter added: f_or_b_code
510 3283796 static void mpeg1_encode_motion(MPVEncContext *const s, int val, int f_or_b_code)
511 {
512
2/2
✓ Branch 0 taken 1313162 times.
✓ Branch 1 taken 1970634 times.
3283796 if (val == 0) {
513 /* zero vector, corresponds to ff_mpeg12_mbMotionVectorTable[0] */
514 1313162 put_bits(&s->pb, 1, 0x01);
515 } else {
516 int code, sign, bits;
517 1970634 int bit_size = f_or_b_code - 1;
518 1970634 int range = 1 << bit_size;
519 /* modulo encoding */
520 1970634 val = sign_extend(val, 5 + bit_size);
521
522
2/2
✓ Branch 0 taken 950678 times.
✓ Branch 1 taken 1019956 times.
1970634 if (val >= 0) {
523 950678 val--;
524 950678 code = (val >> bit_size) + 1;
525 950678 bits = val & (range - 1);
526 950678 sign = 0;
527 } else {
528 1019956 val = -val;
529 1019956 val--;
530 1019956 code = (val >> bit_size) + 1;
531 1019956 bits = val & (range - 1);
532 1019956 sign = 1;
533 }
534
535 av_assert2(code > 0 && code <= 16);
536
537 1970634 put_bits(&s->pb,
538 1970634 ff_mpeg12_mbMotionVectorTable[code][1],
539 1970634 ff_mpeg12_mbMotionVectorTable[code][0]);
540
541 1970634 put_bits(&s->pb, 1, sign);
542
2/2
✓ Branch 0 taken 1418976 times.
✓ Branch 1 taken 551658 times.
1970634 if (bit_size > 0)
543 1418976 put_bits(&s->pb, bit_size, bits);
544 }
545 3283796 }
546
547 3438674 static inline void encode_dc(MPVEncContext *const s, int diff, int component)
548 {
549 3438674 unsigned int diff_u = diff + 255;
550
2/2
✓ Branch 0 taken 162210 times.
✓ Branch 1 taken 3276464 times.
3438674 if (diff_u >= 511) {
551 int index;
552
553
2/2
✓ Branch 0 taken 113286 times.
✓ Branch 1 taken 48924 times.
162210 if (diff < 0) {
554 113286 index = av_log2_16bit(-2 * diff);
555 113286 diff--;
556 } else {
557 48924 index = av_log2_16bit(2 * diff);
558 }
559
2/2
✓ Branch 0 taken 91380 times.
✓ Branch 1 taken 70830 times.
162210 if (component == 0)
560 91380 put_bits(&s->pb,
561 91380 ff_mpeg12_vlc_dc_lum_bits[index] + index,
562 91380 (ff_mpeg12_vlc_dc_lum_code[index] << index) +
563 91380 av_zero_extend(diff, index));
564 else
565 70830 put_bits(&s->pb,
566 70830 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
567 70830 (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
568 70830 av_zero_extend(diff, index));
569 } else {
570
2/2
✓ Branch 0 taken 1807388 times.
✓ Branch 1 taken 1469076 times.
3276464 if (component == 0)
571 1807388 put_bits(&s->pb,
572 1807388 mpeg1_lum_dc_uni[diff + 255] & 0xFF,
573 1807388 mpeg1_lum_dc_uni[diff + 255] >> 8);
574 else
575 1469076 put_bits(&s->pb,
576 1469076 mpeg1_chr_dc_uni[diff + 255] & 0xFF,
577 1469076 mpeg1_chr_dc_uni[diff + 255] >> 8);
578 }
579 3438674 }
580
581 7011737 static void mpeg1_encode_block(MPVEncContext *const s, const int16_t block[], int n)
582 {
583 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
584 int code, component;
585 7011737 const uint16_t (*table_vlc)[2] = ff_mpeg1_vlc_table;
586
587 7011737 last_index = s->c.block_last_index[n];
588
589 /* DC coef */
590
2/2
✓ Branch 0 taken 3438674 times.
✓ Branch 1 taken 3573063 times.
7011737 if (s->c.mb_intra) {
591
2/2
✓ Branch 0 taken 1539906 times.
✓ Branch 1 taken 1898768 times.
3438674 component = (n <= 3 ? 0 : (n & 1) + 1);
592 3438674 dc = block[0]; /* overflow is impossible */
593 3438674 diff = dc - s->c.last_dc[component];
594 3438674 encode_dc(s, diff, component);
595 3438674 s->c.last_dc[component] = dc;
596 3438674 i = 1;
597
2/2
✓ Branch 0 taken 2641700 times.
✓ Branch 1 taken 796974 times.
3438674 if (s->c.intra_vlc_format)
598 2641700 table_vlc = ff_mpeg2_vlc_table;
599 } else {
600 /* encode the first coefficient: needs to be done here because
601 * it is handled slightly differently */
602 3573063 level = block[0];
603
2/2
✓ Branch 0 taken 1068227 times.
✓ Branch 1 taken 2504836 times.
3573063 if (abs(level) == 1) {
604 1068227 code = ((uint32_t)level >> 31); /* the sign bit */
605 1068227 put_bits(&s->pb, 2, code | 0x02);
606 1068227 i = 1;
607 } else {
608 2504836 i = 0;
609 2504836 last_non_zero = -1;
610 2504836 goto next_coef;
611 }
612 }
613
614 /* now quantify & encode AC coefs */
615 4506901 last_non_zero = i - 1;
616
617
2/2
✓ Branch 0 taken 163845356 times.
✓ Branch 1 taken 7011737 times.
170857093 for (; i <= last_index; i++) {
618 163845356 j = s->c.intra_scantable.permutated[i];
619 163845356 level = block[j];
620
621 166350192 next_coef:
622 /* encode using VLC */
623
2/2
✓ Branch 0 taken 68884026 times.
✓ Branch 1 taken 97466166 times.
166350192 if (level != 0) {
624 68884026 run = i - last_non_zero - 1;
625
626 68884026 alevel = level;
627 68884026 MASK_ABS(sign, alevel);
628 68884026 sign &= 1;
629
630
2/2
✓ Branch 0 taken 66711344 times.
✓ Branch 1 taken 2172682 times.
68884026 if (alevel <= mpeg12_max_level[run]) {
631 66711344 code = mpeg12_index_run[run] + alevel - 1;
632 /* store the VLC & sign at once */
633 66711344 put_bits(&s->pb, table_vlc[code][1] + 1,
634 66711344 (table_vlc[code][0] << 1) + sign);
635 } else {
636 /* Escape seems to be pretty rare <5% so I do not optimize it.
637 * The following encodes run together with the common escape
638 * value of both tables 000001b. */
639 2172682 put_bits(&s->pb, 6 + 6, 0x01 << 6 | run);
640 /* escape: only clip in this case */
641
2/2
✓ Branch 0 taken 82680 times.
✓ Branch 1 taken 2090002 times.
2172682 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO) {
642
1/2
✓ Branch 0 taken 82680 times.
✗ Branch 1 not taken.
82680 if (alevel < 128) {
643 82680 put_sbits(&s->pb, 8, level);
644 } else {
645 if (level < 0)
646 put_bits(&s->pb, 16, 0x8001 + level + 255);
647 else
648 put_sbits(&s->pb, 16, level);
649 }
650 } else {
651 2090002 put_sbits(&s->pb, 12, level);
652 }
653 }
654 68884026 last_non_zero = i;
655 }
656 }
657 /* end of block */
658 7011737 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
659 7011737 }
660
661 1910478 static av_always_inline void mpeg1_encode_mb_internal(MPVEncContext *const s,
662 const int16_t block[8][64],
663 int motion_x, int motion_y,
664 int mb_block_count,
665 int chroma_y_shift)
666 {
667 /* MPEG-1 is always 420. */
668 #define IS_MPEG1(s) (chroma_y_shift == 1 && (s)->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
669 int i, cbp;
670 1910478 const int mb_x = s->c.mb_x;
671 1910478 const int mb_y = s->c.mb_y;
672
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;
673
674 /* compute cbp */
675 1910478 cbp = 0;
676
2/2
✓ Branch 0 taken 12748778 times.
✓ Branch 1 taken 1910478 times.
14659256 for (i = 0; i < mb_block_count; i++)
677
2/2
✓ Branch 0 taken 7011737 times.
✓ Branch 1 taken 5737041 times.
12748778 if (s->c.block_last_index[i] >= 0)
678 7011737 cbp |= 1 << (mb_block_count - 1 - i);
679
680
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 &&
681
2/2
✓ Branch 0 taken 12180 times.
✓ Branch 1 taken 410934 times.
423114 (mb_x != s->c.mb_width - 1 ||
682
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))) &&
683
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) ||
684
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 &&
685 22008 (((s->c.mv_dir & MV_DIR_FORWARD)
686 20311 ? ((s->c.mv[0][0][0] - s->c.last_mv[0][0][0]) |
687
4/4
✓ Branch 0 taken 20311 times.
✓ Branch 1 taken 1697 times.
✓ Branch 2 taken 4342 times.
✓ Branch 3 taken 17666 times.
44016 (s->c.mv[0][0][1] - s->c.last_mv[0][0][1])) : 0) |
688 22008 ((s->c.mv_dir & MV_DIR_BACKWARD)
689 21679 ? ((s->c.mv[1][0][0] - s->c.last_mv[1][0][0]) |
690
2/2
✓ Branch 0 taken 21679 times.
✓ Branch 1 taken 329 times.
22008 (s->c.mv[1][0][1] - s->c.last_mv[1][0][1])) : 0)) == 0))) {
691 133525 s->c.mb_skip_run++;
692 133525 s->c.qscale -= s->dquant;
693 133525 s->misc_bits++;
694 133525 s->last_bits++;
695
2/2
✓ Branch 0 taken 129183 times.
✓ Branch 1 taken 4342 times.
133525 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
696 129183 s->c.last_mv[0][0][0] =
697 129183 s->c.last_mv[0][0][1] =
698 129183 s->c.last_mv[0][1][0] =
699 129183 s->c.last_mv[0][1][1] = 0;
700 }
701 } else {
702
2/2
✓ Branch 0 taken 326442 times.
✓ Branch 1 taken 1450511 times.
1776953 if (first_mb) {
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 326442 times.
326442 av_assert0(s->c.mb_skip_run == 0);
704 326442 encode_mb_skip_run(s, s->c.mb_x);
705 } else {
706 1450511 encode_mb_skip_run(s, s->c.mb_skip_run);
707 }
708
709
2/2
✓ Branch 0 taken 400954 times.
✓ Branch 1 taken 1375999 times.
1776953 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
710
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) {
711 /* macroblock_type: macroblock_quant = 1 */
712 43090 put_mb_modes(s, 2, 1, 0, 0);
713 43090 put_qscale(s);
714 } else {
715 /* macroblock_type: macroblock_quant = 0 */
716 357864 put_mb_modes(s, 1, 1, 0, 0);
717 357864 s->c.qscale -= s->dquant;
718 }
719 400954 s->misc_bits += get_bits_diff(s);
720 400954 s->i_count++;
721
2/2
✓ Branch 0 taken 73738 times.
✓ Branch 1 taken 1302261 times.
1375999 } else if (s->c.mb_intra) {
722
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) {
723 11835 put_mb_modes(s, 6, 0x01, 0, 0);
724 11835 put_qscale(s);
725 } else {
726 61903 put_mb_modes(s, 5, 0x03, 0, 0);
727 61903 s->c.qscale -= s->dquant;
728 }
729 73738 s->misc_bits += get_bits_diff(s);
730 73738 s->i_count++;
731 73738 memset(s->c.last_mv, 0, sizeof(s->c.last_mv));
732
2/2
✓ Branch 0 taken 721318 times.
✓ Branch 1 taken 580943 times.
1302261 } else if (s->c.pict_type == AV_PICTURE_TYPE_P) {
733
2/2
✓ Branch 0 taken 698469 times.
✓ Branch 1 taken 22849 times.
721318 if (s->c.mv_type == MV_TYPE_16X16) {
734
2/2
✓ Branch 0 taken 573480 times.
✓ Branch 1 taken 124989 times.
698469 if (cbp != 0) {
735
2/2
✓ Branch 0 taken 61233 times.
✓ Branch 1 taken 512247 times.
573480 if ((motion_x | motion_y) == 0) {
736
2/2
✓ Branch 0 taken 1554 times.
✓ Branch 1 taken 59679 times.
61233 if (s->dquant) {
737 /* macroblock_pattern & quant */
738 1554 put_mb_modes(s, 5, 1, 0, 0);
739 1554 put_qscale(s);
740 } else {
741 /* macroblock_pattern only */
742 59679 put_mb_modes(s, 2, 1, 0, 0);
743 }
744 61233 s->misc_bits += get_bits_diff(s);
745 } else {
746
2/2
✓ Branch 0 taken 72389 times.
✓ Branch 1 taken 439858 times.
512247 if (s->dquant) {
747 72389 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
748 72389 put_qscale(s);
749 } else {
750 439858 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
751 }
752 512247 s->misc_bits += get_bits_diff(s);
753 // RAL: f_code parameter added
754 512247 mpeg1_encode_motion(s,
755 512247 motion_x - s->c.last_mv[0][0][0],
756 s->f_code);
757 // RAL: f_code parameter added
758 512247 mpeg1_encode_motion(s,
759 512247 motion_y - s->c.last_mv[0][0][1],
760 s->f_code);
761 512247 s->mv_bits += get_bits_diff(s);
762 }
763 } else {
764 124989 put_bits(&s->pb, 3, 1); /* motion only */
765
2/2
✓ Branch 0 taken 24679 times.
✓ Branch 1 taken 100310 times.
124989 if (!s->c.frame_pred_frame_dct)
766 24679 put_bits(&s->pb, 2, 2); /* motion_type: frame */
767 124989 s->misc_bits += get_bits_diff(s);
768 // RAL: f_code parameter added
769 124989 mpeg1_encode_motion(s,
770 124989 motion_x - s->c.last_mv[0][0][0],
771 s->f_code);
772 // RAL: f_code parameter added
773 124989 mpeg1_encode_motion(s,
774 124989 motion_y - s->c.last_mv[0][0][1],
775 s->f_code);
776 124989 s->c.qscale -= s->dquant;
777 124989 s->mv_bits += get_bits_diff(s);
778 }
779 698469 s->c.last_mv[0][1][0] = s->c.last_mv[0][0][0] = motion_x;
780 698469 s->c.last_mv[0][1][1] = s->c.last_mv[0][0][1] = motion_y;
781 } else {
782 av_assert2(!s->c.frame_pred_frame_dct && s->c.mv_type == MV_TYPE_FIELD);
783
784
2/2
✓ Branch 0 taken 21344 times.
✓ Branch 1 taken 1505 times.
22849 if (cbp) {
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21344 times.
21344 if (s->dquant) {
786 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
787 put_qscale(s);
788 } else {
789 21344 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
790 }
791 } else {
792 1505 put_bits(&s->pb, 3, 1); /* motion only */
793 1505 put_bits(&s->pb, 2, 1); /* motion_type: field */
794 1505 s->c.qscale -= s->dquant;
795 }
796 22849 s->misc_bits += get_bits_diff(s);
797
2/2
✓ Branch 0 taken 45698 times.
✓ Branch 1 taken 22849 times.
68547 for (i = 0; i < 2; i++) {
798 45698 put_bits(&s->pb, 1, s->c.field_select[0][i]);
799 45698 mpeg1_encode_motion(s,
800 45698 s->c.mv[0][i][0] - s->c.last_mv[0][i][0],
801 s->f_code);
802 45698 mpeg1_encode_motion(s,
803 45698 s->c.mv[0][i][1] - (s->c.last_mv[0][i][1] >> 1),
804 s->f_code);
805 45698 s->c.last_mv[0][i][0] = s->c.mv[0][i][0];
806 45698 s->c.last_mv[0][i][1] = 2 * s->c.mv[0][i][1];
807 }
808 22849 s->mv_bits += get_bits_diff(s);
809 }
810
2/2
✓ Branch 0 taken 594824 times.
✓ Branch 1 taken 126494 times.
721318 if (cbp) {
811
2/2
✓ Branch 0 taken 520012 times.
✓ Branch 1 taken 74812 times.
594824 if (chroma_y_shift) {
812 520012 put_bits(&s->pb,
813 520012 ff_mpeg12_mbPatTable[cbp][1],
814 520012 ff_mpeg12_mbPatTable[cbp][0]);
815 } else {
816 74812 put_bits(&s->pb,
817 74812 ff_mpeg12_mbPatTable[cbp >> 2][1],
818 74812 ff_mpeg12_mbPatTable[cbp >> 2][0]);
819 74812 put_sbits(&s->pb, 2, cbp);
820 }
821 }
822 } else {
823
2/2
✓ Branch 0 taken 488110 times.
✓ Branch 1 taken 92833 times.
580943 if (s->c.mv_type == MV_TYPE_16X16) {
824
2/2
✓ Branch 0 taken 312597 times.
✓ Branch 1 taken 175513 times.
488110 if (cbp) { // With coded bloc pattern
825
2/2
✓ Branch 0 taken 53290 times.
✓ Branch 1 taken 259307 times.
312597 if (s->dquant) {
826
2/2
✓ Branch 0 taken 11218 times.
✓ Branch 1 taken 42072 times.
53290 if (s->c.mv_dir == MV_DIR_FORWARD)
827 11218 put_mb_modes(s, 6, 3, 1, 0);
828 else
829 42072 put_mb_modes(s, 8 - s->c.mv_dir, 2, 1, 0);
830 53290 put_qscale(s);
831 } else {
832 259307 put_mb_modes(s, 5 - s->c.mv_dir, 3, 1, 0);
833 }
834 } else { // No coded bloc pattern
835 175513 put_bits(&s->pb, 5 - s->c.mv_dir, 2);
836
2/2
✓ Branch 0 taken 82021 times.
✓ Branch 1 taken 93492 times.
175513 if (!s->c.frame_pred_frame_dct)
837 82021 put_bits(&s->pb, 2, 2); /* motion_type: frame */
838 175513 s->c.qscale -= s->dquant;
839 }
840 488110 s->misc_bits += get_bits_diff(s);
841
2/2
✓ Branch 0 taken 351301 times.
✓ Branch 1 taken 136809 times.
488110 if (s->c.mv_dir & MV_DIR_FORWARD) {
842 351301 mpeg1_encode_motion(s,
843 351301 s->c.mv[0][0][0] - s->c.last_mv[0][0][0],
844 s->f_code);
845 351301 mpeg1_encode_motion(s,
846 351301 s->c.mv[0][0][1] - s->c.last_mv[0][0][1],
847 s->f_code);
848 351301 s->c.last_mv[0][0][0] =
849 351301 s->c.last_mv[0][1][0] = s->c.mv[0][0][0];
850 351301 s->c.last_mv[0][0][1] =
851 351301 s->c.last_mv[0][1][1] = s->c.mv[0][0][1];
852 }
853
2/2
✓ Branch 0 taken 369943 times.
✓ Branch 1 taken 118167 times.
488110 if (s->c.mv_dir & MV_DIR_BACKWARD) {
854 369943 mpeg1_encode_motion(s,
855 369943 s->c.mv[1][0][0] - s->c.last_mv[1][0][0],
856 s->b_code);
857 369943 mpeg1_encode_motion(s,
858 369943 s->c.mv[1][0][1] - s->c.last_mv[1][0][1],
859 s->b_code);
860 369943 s->c.last_mv[1][0][0] =
861 369943 s->c.last_mv[1][1][0] = s->c.mv[1][0][0];
862 369943 s->c.last_mv[1][0][1] =
863 369943 s->c.last_mv[1][1][1] = s->c.mv[1][0][1];
864 }
865 } else {
866 av_assert2(s->c.mv_type == MV_TYPE_FIELD);
867 av_assert2(!s->c.frame_pred_frame_dct);
868
2/2
✓ Branch 0 taken 73695 times.
✓ Branch 1 taken 19138 times.
92833 if (cbp) { // With coded bloc pattern
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73695 times.
73695 if (s->dquant) {
870 if (s->c.mv_dir == MV_DIR_FORWARD)
871 put_mb_modes(s, 6, 3, 1, 1);
872 else
873 put_mb_modes(s, 8 - s->c.mv_dir, 2, 1, 1);
874 put_qscale(s);
875 } else {
876 73695 put_mb_modes(s, 5 - s->c.mv_dir, 3, 1, 1);
877 }
878 } else { // No coded bloc pattern
879 19138 put_bits(&s->pb, 5 - s->c.mv_dir, 2);
880 19138 put_bits(&s->pb, 2, 1); /* motion_type: field */
881 19138 s->c.qscale -= s->dquant;
882 }
883 92833 s->misc_bits += get_bits_diff(s);
884
2/2
✓ Branch 0 taken 59191 times.
✓ Branch 1 taken 33642 times.
92833 if (s->c.mv_dir & MV_DIR_FORWARD) {
885
2/2
✓ Branch 0 taken 118382 times.
✓ Branch 1 taken 59191 times.
177573 for (i = 0; i < 2; i++) {
886 118382 put_bits(&s->pb, 1, s->c.field_select[0][i]);
887 118382 mpeg1_encode_motion(s,
888 118382 s->c.mv[0][i][0] - s->c.last_mv[0][i][0],
889 s->f_code);
890 118382 mpeg1_encode_motion(s,
891 118382 s->c.mv[0][i][1] - (s->c.last_mv[0][i][1] >> 1),
892 s->f_code);
893 118382 s->c.last_mv[0][i][0] = s->c.mv[0][i][0];
894 118382 s->c.last_mv[0][i][1] = s->c.mv[0][i][1] * 2;
895 }
896 }
897
2/2
✓ Branch 0 taken 59669 times.
✓ Branch 1 taken 33164 times.
92833 if (s->c.mv_dir & MV_DIR_BACKWARD) {
898
2/2
✓ Branch 0 taken 119338 times.
✓ Branch 1 taken 59669 times.
179007 for (i = 0; i < 2; i++) {
899 119338 put_bits(&s->pb, 1, s->c.field_select[1][i]);
900 119338 mpeg1_encode_motion(s,
901 119338 s->c.mv[1][i][0] - s->c.last_mv[1][i][0],
902 s->b_code);
903 119338 mpeg1_encode_motion(s,
904 119338 s->c.mv[1][i][1] - (s->c.last_mv[1][i][1] >> 1),
905 s->b_code);
906 119338 s->c.last_mv[1][i][0] = s->c.mv[1][i][0];
907 119338 s->c.last_mv[1][i][1] = s->c.mv[1][i][1] * 2;
908 }
909 }
910 }
911 580943 s->mv_bits += get_bits_diff(s);
912
2/2
✓ Branch 0 taken 386292 times.
✓ Branch 1 taken 194651 times.
580943 if (cbp) {
913
2/2
✓ Branch 0 taken 206130 times.
✓ Branch 1 taken 180162 times.
386292 if (chroma_y_shift) {
914 206130 put_bits(&s->pb,
915 206130 ff_mpeg12_mbPatTable[cbp][1],
916 206130 ff_mpeg12_mbPatTable[cbp][0]);
917 } else {
918 180162 put_bits(&s->pb,
919 180162 ff_mpeg12_mbPatTable[cbp >> 2][1],
920 180162 ff_mpeg12_mbPatTable[cbp >> 2][0]);
921 180162 put_sbits(&s->pb, 2, cbp);
922 }
923 }
924 }
925
2/2
✓ Branch 0 taken 11946332 times.
✓ Branch 1 taken 1776953 times.
13723285 for (i = 0; i < mb_block_count; i++)
926
2/2
✓ Branch 0 taken 7011737 times.
✓ Branch 1 taken 4934595 times.
11946332 if (cbp & (1 << (mb_block_count - 1 - i)))
927 7011737 mpeg1_encode_block(s, block[i], i);
928 1776953 s->c.mb_skip_run = 0;
929
2/2
✓ Branch 0 taken 474692 times.
✓ Branch 1 taken 1302261 times.
1776953 if (s->c.mb_intra)
930 474692 s->i_tex_bits += get_bits_diff(s);
931 else
932 1302261 s->p_tex_bits += get_bits_diff(s);
933 }
934 1910478 }
935
936 1910478 static void mpeg12_encode_mb(MPVEncContext *const s, int16_t block[][64],
937 int motion_x, int motion_y)
938 {
939
2/2
✓ Branch 0 taken 1435786 times.
✓ Branch 1 taken 474692 times.
1910478 if (!s->c.mb_intra)
940 1435786 s->c.last_dc[0] = s->c.last_dc[1] = s->c.last_dc[2] = 128 << s->c.intra_dc_precision;
941
2/2
✓ Branch 0 taken 1267523 times.
✓ Branch 1 taken 642955 times.
1910478 if (s->c.chroma_format == CHROMA_420)
942 1267523 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6, 1);
943 else
944 642955 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8, 0);
945 1910478 }
946
947 55 static av_cold void mpeg12_encode_init_static(void)
948 {
949 55 ff_rl_init_level_run(mpeg12_max_level, mpeg12_index_run,
950 ff_mpeg12_run, ff_mpeg12_level, MPEG12_RL_NB_ELEMS);
951
952 55 ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run,
953 ff_mpeg1_vlc_table, uni_mpeg1_ac_vlc_len);
954 55 ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run,
955 ff_mpeg2_vlc_table, uni_mpeg2_ac_vlc_len);
956
957 /* build unified dc encoding tables */
958
2/2
✓ Branch 0 taken 28105 times.
✓ Branch 1 taken 55 times.
28160 for (int i = -255; i < 256; i++) {
959 int adiff, index;
960 int bits, code;
961 28105 int diff = i;
962
963 28105 adiff = FFABS(diff);
964
2/2
✓ Branch 0 taken 14025 times.
✓ Branch 1 taken 14080 times.
28105 if (diff < 0)
965 14025 diff--;
966 28105 index = av_log2(2 * adiff);
967
968 28105 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
969 28105 code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
970 28105 av_zero_extend(diff, index);
971 28105 mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
972
973 28105 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
974 28105 code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
975 28105 av_zero_extend(diff, index);
976 28105 mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
977 }
978
979
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 55 times.
440 for (int f_code = 1; f_code <= MAX_FCODE; f_code++)
980
2/2
✓ Branch 0 taken 6308225 times.
✓ Branch 1 taken 385 times.
6308610 for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) {
981 int len;
982
983
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 6307840 times.
6308225 if (mv == 0) {
984 385 len = 1; /* ff_mpeg12_mbMotionVectorTable[0][1] */
985 } else {
986 int val, bit_size, code;
987
988 6307840 bit_size = f_code - 1;
989
990 6307840 val = mv;
991
2/2
✓ Branch 0 taken 3153920 times.
✓ Branch 1 taken 3153920 times.
6307840 if (val < 0)
992 3153920 val = -val;
993 6307840 val--;
994 6307840 code = (val >> bit_size) + 1;
995
2/2
✓ Branch 0 taken 223520 times.
✓ Branch 1 taken 6084320 times.
6307840 if (code < 17)
996 223520 len = ff_mpeg12_mbMotionVectorTable[code][1] +
997 1 + bit_size;
998 else
999 6084320 len = 10 /* ff_mpeg12_mbMotionVectorTable[16][1] */ +
1000 2 + bit_size;
1001 }
1002
1003 6308225 mv_penalty[f_code][mv + MAX_DMV] = len;
1004 }
1005
1006
1007
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 55 times.
440 for (int f_code = MAX_FCODE; f_code > 0; f_code--)
1008
2/2
✓ Branch 0 taken 223520 times.
✓ Branch 1 taken 385 times.
223905 for (int mv = -(8 << f_code); mv < (8 << f_code); mv++)
1009 223520 fcode_tab[mv + MAX_MV] = f_code;
1010 55 }
1011
1012 55 static av_cold int find_frame_rate_index(AVCodecContext *avctx, MPEG12EncContext *mpeg12)
1013 {
1014 55 AVRational bestq = (AVRational) {0, 0};
1015 AVRational ext;
1016 55 AVRational target = av_inv_q(avctx->time_base);
1017
1018
2/2
✓ Branch 0 taken 499 times.
✓ Branch 1 taken 1 times.
500 for (int i = 1; i < 14; i++) {
1019
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 &&
1020 i >= 9)
1021 54 break;
1022
1023
2/2
✓ Branch 0 taken 1780 times.
✓ Branch 1 taken 445 times.
2225 for (ext.num = 1; ext.num <= 4; ext.num++) {
1024
2/2
✓ Branch 0 taken 56960 times.
✓ Branch 1 taken 1780 times.
58740 for (ext.den = 1; ext.den <= 32; ext.den++) {
1025 56960 AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]);
1026
1027
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))
1028 26170 continue;
1029
2/2
✓ Branch 0 taken 14994 times.
✓ Branch 1 taken 30790 times.
45784 if (av_gcd(ext.den, ext.num) != 1)
1030 14994 continue;
1031
1032
2/2
✓ Branch 0 taken 30735 times.
✓ Branch 1 taken 55 times.
30790 if ( bestq.num==0
1033
2/2
✓ Branch 1 taken 30597 times.
✓ Branch 2 taken 138 times.
30735 || av_nearer_q(target, bestq, q) < 0
1034
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) {
1035 193 bestq = q;
1036 193 mpeg12->frame_rate_index = i;
1037 193 mpeg12->frame_rate_ext.num = ext.num;
1038 193 mpeg12->frame_rate_ext.den = ext.den;
1039 }
1040 }
1041 }
1042 }
1043
1044
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 54 times.
55 if (av_cmp_q(target, bestq))
1045 1 return -1;
1046 else
1047 54 return 0;
1048 }
1049
1050 55 static av_cold int encode_init(AVCodecContext *avctx)
1051 {
1052 static AVOnce init_static_once = AV_ONCE_INIT;
1053 55 MPEG12EncContext *const mpeg12 = avctx->priv_data;
1054 55 MPVMainEncContext *const m = &mpeg12->mpeg;
1055 55 MPVEncContext *const s = &m->s;
1056 int ret;
1057
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;
1058
1059
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) {
1060 av_log(avctx, AV_LOG_ERROR, "%s does not support resolutions above %dx%d\n",
1061 CONFIG_SMALL ? avctx->codec->name : avctx->codec->long_name,
1062 max_size, max_size);
1063 return AVERROR(EINVAL);
1064 }
1065
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
55 if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
1066 av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
1067 return AVERROR(EINVAL);
1068 }
1069
1070
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1 times.
55 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
1071
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) {
1072 av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiples of 4096\n"
1073 "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
1074 return AVERROR(EINVAL);
1075 }
1076 }
1077
1078
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 54 times.
55 if (s->c.q_scale_type == 1) {
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->qmax > 28) {
1080 av_log(avctx, AV_LOG_ERROR,
1081 "non linear quant only supports qmax <= 28 currently\n");
1082 return AVERROR_PATCHWELCOME;
1083 }
1084 }
1085
1086
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 if (avctx->profile == AV_PROFILE_UNKNOWN) {
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (avctx->level != AV_LEVEL_UNKNOWN) {
1088 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
1089 return AVERROR(EINVAL);
1090 }
1091 /* Main or 4:2:2 */
1092 55 avctx->profile = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? AV_PROFILE_MPEG2_MAIN
1093
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5 times.
55 : AV_PROFILE_MPEG2_422;
1094 }
1095
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 if (avctx->level == AV_LEVEL_UNKNOWN) {
1096
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 50 times.
55 if (avctx->profile == AV_PROFILE_MPEG2_422) { /* 4:2:2 */
1097
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (avctx->width <= 720 && avctx->height <= 608)
1098 5 avctx->level = 5; /* Main */
1099 else
1100 avctx->level = 2; /* High */
1101 } else {
1102
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (avctx->profile != AV_PROFILE_MPEG2_HIGH &&
1103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
1104 av_log(avctx, AV_LOG_ERROR,
1105 "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
1106 return AVERROR(EINVAL);
1107 }
1108
3/4
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
50 if (avctx->width <= 720 && avctx->height <= 576)
1109 49 avctx->level = 8; /* Main */
1110
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (avctx->width <= 1440)
1111 1 avctx->level = 6; /* High 1440 */
1112 else
1113 avctx->level = 4; /* High */
1114 }
1115 }
1116
1117 55 m->encode_picture_header = mpeg1_encode_picture_header;
1118 55 s->encode_mb = mpeg12_encode_mb;
1119
1120 55 s->me.mv_penalty = mv_penalty;
1121 55 m->fcode_tab = fcode_tab + MAX_MV;
1122
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 44 times.
55 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1123 11 s->min_qcoeff = -255;
1124 11 s->max_qcoeff = 255;
1125 } else {
1126 44 s->min_qcoeff = -2047;
1127 44 s->max_qcoeff = 2047;
1128 44 s->mpeg_quant = 1;
1129 }
1130
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 42 times.
55 if (s->c.intra_vlc_format) {
1131 13 s->intra_ac_vlc_length =
1132 13 s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len;
1133 } else {
1134 42 s->intra_ac_vlc_length =
1135 42 s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1136 }
1137 55 s->inter_ac_vlc_length =
1138 55 s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1139
1140 55 ret = ff_mpv_encode_init(avctx);
1141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (ret < 0)
1142 return ret;
1143
1144
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 44 times.
55 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
1145
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 >
1146 SLICE_MAX_START_CODE - SLICE_MIN_START_CODE) {
1147 // MPEG-1 slices must not start at a MB row number that would make
1148 // their start code > SLICE_MAX_START_CODE. So make the last slice
1149 // bigger if needed and evenly distribute the first 174 rows.
1150 static_assert(MAX_THREADS <= 1 + SLICE_MAX_START_CODE - SLICE_MIN_START_CODE,
1151 "With more than 175 slice contexts, we have to handle "
1152 "the case in which there is no work to do for some "
1153 "slice contexts.");
1154 const int mb_height = SLICE_MAX_START_CODE - SLICE_MIN_START_CODE;
1155 const int nb_slices = s->c.slice_context_count - 1;
1156
1157 s->c.thread_context[nb_slices]->start_mb_y = mb_height;
1158
1159 av_assert1(nb_slices >= 1);
1160 for (int i = 0; i < nb_slices; i++) {
1161 s->c.thread_context[i]->start_mb_y =
1162 (mb_height * (i ) + nb_slices / 2) / nb_slices;
1163 s->c.thread_context[i]->end_mb_y =
1164 (mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1165 }
1166 }
1167
1168
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 54 times.
55 if (find_frame_rate_index(avctx, mpeg12) < 0) {
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1170 av_log(avctx, AV_LOG_ERROR, "MPEG-1/2 does not support %d/%d fps\n",
1171 avctx->time_base.den, avctx->time_base.num);
1172 return AVERROR(EINVAL);
1173 } else {
1174 1 av_log(avctx, AV_LOG_INFO,
1175 "MPEG-1/2 does not support %d/%d fps, there may be AV sync issues\n",
1176 avctx->time_base.den, avctx->time_base.num);
1177 }
1178 }
1179
1180
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 53 times.
55 if (avctx->rc_max_rate &&
1181
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 avctx->rc_min_rate == avctx->rc_max_rate &&
1182 2 90000LL * (avctx->rc_buffer_size - 1) >
1183
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 avctx->rc_max_rate * 0xFFFFLL) {
1184 1 av_log(avctx, AV_LOG_INFO,
1185 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
1186 "specified vbv buffer is too large for the given bitrate!\n");
1187 }
1188
1189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (mpeg12->drop_frame_timecode)
1190 mpeg12->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME;
1191
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) {
1192 av_log(avctx, AV_LOG_ERROR,
1193 "Drop frame time code only allowed with 1001/30000 fps\n");
1194 return AVERROR(EINVAL);
1195 }
1196
1197
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 49 times.
55 if (mpeg12->tc_opt_str) {
1198 6 AVRational rate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index];
1199 6 int ret = av_timecode_init_from_string(&mpeg12->tc, rate, mpeg12->tc_opt_str, avctx);
1200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1201 return ret;
1202 6 mpeg12->drop_frame_timecode = !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME);
1203 6 mpeg12->timecode_frame_start = mpeg12->tc.start;
1204 } else {
1205 49 mpeg12->timecode_frame_start = 0; // default is -1
1206 }
1207
1208 55 ff_thread_once(&init_static_once, mpeg12_encode_init_static);
1209
1210 55 return 0;
1211 }
1212
1213 #define OFFSET(x) offsetof(MPEG12EncContext, x)
1214 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1215 #define COMMON_OPTS \
1216 { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format. Overrides timecode_frame_start.", \
1217 OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE },\
1218 { "drop_frame_timecode", "Timecode is in drop frame format.", \
1219 OFFSET(drop_frame_timecode), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
1220 { "scan_offset", "Reserve space for SVCD scan offset user data.", \
1221 OFFSET(scan_offset), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
1222 { "timecode_frame_start", "GOP timecode frame start number, in non-drop-frame format", \
1223 OFFSET(timecode_frame_start), AV_OPT_TYPE_INT64, {.i64 = -1 }, -1, INT64_MAX, VE}, \
1224 FF_MPV_COMMON_BFRAME_OPTS
1225
1226 static const AVOption mpeg1_options[] = {
1227 COMMON_OPTS
1228 FF_MPV_COMMON_OPTS
1229 FF_MPV_COMMON_MOTION_EST_OPTS
1230 { NULL },
1231 };
1232
1233 static const AVOption mpeg2_options[] = {
1234 COMMON_OPTS
1235 { "intra_vlc", "Use MPEG-2 intra VLC table.",
1236 FF_MPV_OFFSET(c.intra_vlc_format), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1237 { "non_linear_quant", "Use nonlinear quantizer.", FF_MPV_OFFSET(c.q_scale_type), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1238 { "alternate_scan", "Enable alternate scantable.", FF_MPV_OFFSET(c.alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1239 { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE },
1240 { "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" },
1241 { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, VE, .unit = "seq_disp_ext" },
1242 { "never", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, VE, .unit = "seq_disp_ext" },
1243 { "always", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, VE, .unit = "seq_disp_ext" },
1244 { "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" },
1245 { "component", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_COMPONENT }, 0, 0, VE, .unit = "video_format" },
1246 { "pal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_PAL }, 0, 0, VE, .unit = "video_format" },
1247 { "ntsc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_NTSC }, 0, 0, VE, .unit = "video_format" },
1248 { "secam", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_SECAM }, 0, 0, VE, .unit = "video_format" },
1249 { "mac", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_MAC }, 0, 0, VE, .unit = "video_format" },
1250 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_UNSPECIFIED}, 0, 0, VE, .unit = "video_format" },
1251 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, { .i64 = value }, 0, 0, VE, .unit = "avctx.level"
1252 { LEVEL("high", 4) },
1253 { LEVEL("high1440", 6) },
1254 { LEVEL("main", 8) },
1255 { LEVEL("low", 10) },
1256 #undef LEVEL
1257 FF_MPV_COMMON_OPTS
1258 FF_MPV_COMMON_MOTION_EST_OPTS
1259 FF_MPEG2_PROFILE_OPTS
1260 { NULL },
1261 };
1262
1263 #define mpeg12_class(x) \
1264 static const AVClass mpeg ## x ## _class = { \
1265 .class_name = "mpeg" # x "video encoder", \
1266 .item_name = av_default_item_name, \
1267 .option = mpeg ## x ## _options, \
1268 .version = LIBAVUTIL_VERSION_INT, \
1269 };
1270
1271 mpeg12_class(1)
1272 mpeg12_class(2)
1273
1274 const FFCodec ff_mpeg1video_encoder = {
1275 .p.name = "mpeg1video",
1276 CODEC_LONG_NAME("MPEG-1 video"),
1277 .p.type = AVMEDIA_TYPE_VIDEO,
1278 .p.id = AV_CODEC_ID_MPEG1VIDEO,
1279 .priv_data_size = sizeof(MPEG12EncContext),
1280 .init = encode_init,
1281 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
1282 .close = ff_mpv_encode_end,
1283 CODEC_FRAMERATES_ARRAY(ff_mpeg12_frame_rate_tab + 1),
1284 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P),
1285 .color_ranges = AVCOL_RANGE_MPEG,
1286 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1287 AV_CODEC_CAP_SLICE_THREADS |
1288 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1289 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1290 .p.priv_class = &mpeg1_class,
1291 };
1292
1293 const FFCodec ff_mpeg2video_encoder = {
1294 .p.name = "mpeg2video",
1295 CODEC_LONG_NAME("MPEG-2 video"),
1296 .p.type = AVMEDIA_TYPE_VIDEO,
1297 .p.id = AV_CODEC_ID_MPEG2VIDEO,
1298 .priv_data_size = sizeof(MPEG12EncContext),
1299 .init = encode_init,
1300 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
1301 .close = ff_mpv_encode_end,
1302 CODEC_FRAMERATES_ARRAY(ff_mpeg2_frame_rate_tab),
1303 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P),
1304 .color_ranges = AVCOL_RANGE_MPEG,
1305 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1306 AV_CODEC_CAP_SLICE_THREADS |
1307 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1308 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1309 .p.priv_class = &mpeg2_class,
1310 };
1311 #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */
1312