FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12enc.c
Date: 2024-03-29 01:21:52
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 1776710 static inline void encode_mb_skip_run(MpegEncContext *s, int run)
442 {
443
2/2
✓ Branch 0 taken 69199 times.
✓ Branch 1 taken 1776710 times.
1845909 while (run >= 33) {
444 69199 put_bits(&s->pb, 11, 0x008);
445 69199 run -= 33;
446 }
447 1776710 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
448 1776710 ff_mpeg12_mbAddrIncrTable[run][0]);
449 1776710 }
450
451 481366 static av_always_inline void put_qscale(MpegEncContext *s)
452 {
453 481366 put_bits(&s->pb, 5, s->qscale);
454 481366 }
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 1449870 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
631 int has_mv, int field_motion)
632 {
633 1449870 put_bits(&s->pb, n, bits);
634
2/2
✓ Branch 0 taken 698812 times.
✓ Branch 1 taken 751058 times.
1449870 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 1449870 }
641
642 // RAL: Parameter added: f_or_b_code
643 3284962 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
644 {
645
2/2
✓ Branch 0 taken 1313690 times.
✓ Branch 1 taken 1971272 times.
3284962 if (val == 0) {
646 /* zero vector, corresponds to ff_mpeg12_mbMotionVectorTable[0] */
647 1313690 put_bits(&s->pb, 1, 0x01);
648 } else {
649 int code, sign, bits;
650 1971272 int bit_size = f_or_b_code - 1;
651 1971272 int range = 1 << bit_size;
652 /* modulo encoding */
653 1971272 val = sign_extend(val, 5 + bit_size);
654
655
2/2
✓ Branch 0 taken 949817 times.
✓ Branch 1 taken 1021455 times.
1971272 if (val >= 0) {
656 949817 val--;
657 949817 code = (val >> bit_size) + 1;
658 949817 bits = val & (range - 1);
659 949817 sign = 0;
660 } else {
661 1021455 val = -val;
662 1021455 val--;
663 1021455 code = (val >> bit_size) + 1;
664 1021455 bits = val & (range - 1);
665 1021455 sign = 1;
666 }
667
668 av_assert2(code > 0 && code <= 16);
669
670 1971272 put_bits(&s->pb,
671 1971272 ff_mpeg12_mbMotionVectorTable[code][1],
672 1971272 ff_mpeg12_mbMotionVectorTable[code][0]);
673
674 1971272 put_bits(&s->pb, 1, sign);
675
2/2
✓ Branch 0 taken 1407241 times.
✓ Branch 1 taken 564031 times.
1971272 if (bit_size > 0)
676 1407241 put_bits(&s->pb, bit_size, bits);
677 }
678 3284962 }
679
680 3433352 static inline void encode_dc(MpegEncContext *s, int diff, int component)
681 {
682 3433352 unsigned int diff_u = diff + 255;
683
2/2
✓ Branch 0 taken 162210 times.
✓ Branch 1 taken 3271142 times.
3433352 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 1803840 times.
✓ Branch 1 taken 1467302 times.
3271142 if (component == 0)
704 1803840 put_bits(&s->pb,
705 1803840 mpeg1_lum_dc_uni[diff + 255] & 0xFF,
706 1803840 mpeg1_lum_dc_uni[diff + 255] >> 8);
707 else
708 1467302 put_bits(&s->pb,
709 1467302 mpeg1_chr_dc_uni[diff + 255] & 0xFF,
710 1467302 mpeg1_chr_dc_uni[diff + 255] >> 8);
711 }
712 3433352 }
713
714 6985022 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 6985022 const uint16_t (*table_vlc)[2] = ff_mpeg1_vlc_table;
719
720 6985022 last_index = s->block_last_index[n];
721
722 /* DC coef */
723
2/2
✓ Branch 0 taken 3433352 times.
✓ Branch 1 taken 3551670 times.
6985022 if (s->mb_intra) {
724
2/2
✓ Branch 0 taken 1538132 times.
✓ Branch 1 taken 1895220 times.
3433352 component = (n <= 3 ? 0 : (n & 1) + 1);
725 3433352 dc = block[0]; /* overflow is impossible */
726 3433352 diff = dc - s->last_dc[component];
727 3433352 encode_dc(s, diff, component);
728 3433352 s->last_dc[component] = dc;
729 3433352 i = 1;
730
2/2
✓ Branch 0 taken 2641700 times.
✓ Branch 1 taken 791652 times.
3433352 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 3551670 level = block[0];
736
2/2
✓ Branch 0 taken 1061350 times.
✓ Branch 1 taken 2490320 times.
3551670 if (abs(level) == 1) {
737 1061350 code = ((uint32_t)level >> 31); /* the sign bit */
738 1061350 put_bits(&s->pb, 2, code | 0x02);
739 1061350 i = 1;
740 } else {
741 2490320 i = 0;
742 2490320 last_non_zero = -1;
743 2490320 goto next_coef;
744 }
745 }
746
747 /* now quantify & encode AC coefs */
748 4494702 last_non_zero = i - 1;
749
750
2/2
✓ Branch 0 taken 162517204 times.
✓ Branch 1 taken 6985022 times.
169502226 for (; i <= last_index; i++) {
751 162517204 j = s->intra_scantable.permutated[i];
752 162517204 level = block[j];
753
754 165007524 next_coef:
755 /* encode using VLC */
756
2/2
✓ Branch 0 taken 68555111 times.
✓ Branch 1 taken 96452413 times.
165007524 if (level != 0) {
757 68555111 run = i - last_non_zero - 1;
758
759 68555111 alevel = level;
760 68555111 MASK_ABS(sign, alevel);
761 68555111 sign &= 1;
762
763
2/2
✓ Branch 0 taken 66394966 times.
✓ Branch 1 taken 2160145 times.
68555111 if (alevel <= mpeg12_max_level[run]) {
764 66394966 code = mpeg12_index_run[run] + alevel - 1;
765 /* store the VLC & sign at once */
766 66394966 put_bits(&s->pb, table_vlc[code][1] + 1,
767 66394966 (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 2160145 put_bits(&s->pb, 6, 0x01);
773 /* escape: only clip in this case */
774 2160145 put_bits(&s->pb, 6, run);
775
2/2
✓ Branch 0 taken 82680 times.
✓ Branch 1 taken 2077465 times.
2160145 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 2077465 put_sbits(&s->pb, 12, level);
786 }
787 }
788 68555111 last_non_zero = i;
789 }
790 }
791 /* end of block */
792 6985022 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
793 6985022 }
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 6985022 times.
✓ Branch 1 taken 5762856 times.
12747878 if (s->block_last_index[i] >= 0)
812 6985022 cbp |= 1 << (mb_block_count - 1 - i);
813
814
6/6
✓ Branch 0 taken 460458 times.
✓ Branch 1 taken 1449870 times.
✓ Branch 2 taken 448979 times.
✓ Branch 3 taken 11479 times.
✓ Branch 4 taken 428756 times.
✓ Branch 5 taken 20223 times.
1910328 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
815
2/2
✓ Branch 0 taken 12313 times.
✓ Branch 1 taken 416443 times.
428756 (mb_x != s->mb_width - 1 ||
816
6/6
✓ Branch 0 taken 11551 times.
✓ Branch 1 taken 762 times.
✓ Branch 2 taken 9591 times.
✓ Branch 3 taken 1960 times.
✓ Branch 4 taken 552 times.
✓ Branch 5 taken 9039 times.
12313 (mb_y != s->end_mb_y - 1 && IS_MPEG1(s))) &&
817
4/4
✓ Branch 0 taken 246759 times.
✓ Branch 1 taken 170236 times.
✓ Branch 2 taken 117483 times.
✓ Branch 3 taken 129276 times.
416995 ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
818
4/4
✓ Branch 0 taken 170236 times.
✓ Branch 1 taken 117483 times.
✓ Branch 2 taken 22008 times.
✓ Branch 3 taken 148228 times.
287719 (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 133618 s->mb_skip_run++;
826 133618 s->qscale -= s->dquant;
827 133618 s->skip_count++;
828 133618 s->misc_bits++;
829 133618 s->last_bits++;
830
2/2
✓ Branch 0 taken 129276 times.
✓ Branch 1 taken 4342 times.
133618 if (s->pict_type == AV_PICTURE_TYPE_P) {
831 129276 s->last_mv[0][0][0] =
832 129276 s->last_mv[0][0][1] =
833 129276 s->last_mv[0][1][0] =
834 129276 s->last_mv[0][1][1] = 0;
835 }
836 } else {
837
2/2
✓ Branch 0 taken 326392 times.
✓ Branch 1 taken 1450318 times.
1776710 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 1450318 encode_mb_skip_run(s, s->mb_skip_run);
842 }
843
844
2/2
✓ Branch 0 taken 400936 times.
✓ Branch 1 taken 1375774 times.
1776710 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 72869 times.
✓ Branch 1 taken 1302905 times.
1375774 } else if (s->mb_intra) {
857
3/4
✓ Branch 0 taken 11835 times.
✓ Branch 1 taken 61034 times.
✓ Branch 2 taken 11835 times.
✗ Branch 3 not taken.
72869 if (s->dquant && cbp) {
858 11835 put_mb_modes(s, 6, 0x01, 0, 0);
859 11835 put_qscale(s);
860 } else {
861 61034 put_mb_modes(s, 5, 0x03, 0, 0);
862 61034 s->qscale -= s->dquant;
863 }
864 72869 s->misc_bits += get_bits_diff(s);
865 72869 s->i_count++;
866 72869 memset(s->last_mv, 0, sizeof(s->last_mv));
867
2/2
✓ Branch 0 taken 721962 times.
✓ Branch 1 taken 580943 times.
1302905 } else if (s->pict_type == AV_PICTURE_TYPE_P) {
868
2/2
✓ Branch 0 taken 699113 times.
✓ Branch 1 taken 22849 times.
721962 if (s->mv_type == MV_TYPE_16X16) {
869
2/2
✓ Branch 0 taken 568429 times.
✓ Branch 1 taken 130684 times.
699113 if (cbp != 0) {
870
2/2
✓ Branch 0 taken 61294 times.
✓ Branch 1 taken 507135 times.
568429 if ((motion_x | motion_y) == 0) {
871
2/2
✓ Branch 0 taken 1554 times.
✓ Branch 1 taken 59740 times.
61294 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 59740 put_mb_modes(s, 2, 1, 0, 0);
878 }
879 61294 s->misc_bits += get_bits_diff(s);
880 } else {
881
2/2
✓ Branch 0 taken 72388 times.
✓ Branch 1 taken 434747 times.
507135 if (s->dquant) {
882 72388 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
883 72388 put_qscale(s);
884 } else {
885 434747 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
886 }
887 507135 s->misc_bits += get_bits_diff(s);
888 // RAL: f_code parameter added
889 507135 mpeg1_encode_motion(s,
890 507135 motion_x - s->last_mv[0][0][0],
891 s->f_code);
892 // RAL: f_code parameter added
893 507135 mpeg1_encode_motion(s,
894 507135 motion_y - s->last_mv[0][0][1],
895 s->f_code);
896 507135 s->mv_bits += get_bits_diff(s);
897 }
898 } else {
899 130684 put_bits(&s->pb, 3, 1); /* motion only */
900
2/2
✓ Branch 0 taken 17533 times.
✓ Branch 1 taken 113151 times.
130684 if (!s->frame_pred_frame_dct)
901 17533 put_bits(&s->pb, 2, 2); /* motion_type: frame */
902 130684 s->misc_bits += get_bits_diff(s);
903 // RAL: f_code parameter added
904 130684 mpeg1_encode_motion(s,
905 130684 motion_x - s->last_mv[0][0][0],
906 s->f_code);
907 // RAL: f_code parameter added
908 130684 mpeg1_encode_motion(s,
909 130684 motion_y - s->last_mv[0][0][1],
910 s->f_code);
911 130684 s->qscale -= s->dquant;
912 130684 s->mv_bits += get_bits_diff(s);
913 }
914 699113 s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
915 699113 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 589773 times.
✓ Branch 1 taken 132189 times.
721962 if (cbp) {
946
2/2
✓ Branch 0 taken 514961 times.
✓ Branch 1 taken 74812 times.
589773 if (chroma_y_shift) {
947 514961 put_bits(&s->pb,
948 514961 ff_mpeg12_mbPatTable[cbp][1],
949 514961 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 351307 times.
✓ Branch 1 taken 136803 times.
488110 if (s->mv_dir & MV_DIR_FORWARD) {
977 351307 mpeg1_encode_motion(s,
978 351307 s->mv[0][0][0] - s->last_mv[0][0][0],
979 s->f_code);
980 351307 mpeg1_encode_motion(s,
981 351307 s->mv[0][0][1] - s->last_mv[0][0][1],
982 s->f_code);
983 351307 s->last_mv[0][0][0] =
984 351307 s->last_mv[0][1][0] = s->mv[0][0][0];
985 351307 s->last_mv[0][0][1] =
986 351307 s->last_mv[0][1][1] = s->mv[0][0][1];
987 }
988
2/2
✓ Branch 0 taken 369937 times.
✓ Branch 1 taken 118173 times.
488110 if (s->mv_dir & MV_DIR_BACKWARD) {
989 369937 mpeg1_encode_motion(s,
990 369937 s->mv[1][0][0] - s->last_mv[1][0][0],
991 s->b_code);
992 369937 mpeg1_encode_motion(s,
993 369937 s->mv[1][0][1] - s->last_mv[1][0][1],
994 s->b_code);
995 369937 s->last_mv[1][0][0] =
996 369937 s->last_mv[1][1][0] = s->mv[1][0][0];
997 369937 s->last_mv[1][0][1] =
998 369937 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 11944874 times.
✓ Branch 1 taken 1776710 times.
13721584 for (i = 0; i < mb_block_count; i++)
1061
2/2
✓ Branch 0 taken 6985022 times.
✓ Branch 1 taken 4959852 times.
11944874 if (cbp & (1 << (mb_block_count - 1 - i)))
1062 6985022 mpeg1_encode_block(s, block[i], i);
1063 1776710 s->mb_skip_run = 0;
1064
2/2
✓ Branch 0 taken 473805 times.
✓ Branch 1 taken 1302905 times.
1776710 if (s->mb_intra)
1065 473805 s->i_tex_bits += get_bits_diff(s);
1066 else
1067 1302905 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