FFmpeg coverage


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