FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12enc.c
Date: 2024-05-26 22:00:00
Exec Total Coverage
Lines: 617 684 90.2%
Functions: 17 17 100.0%
Branches: 334 407 82.1%

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