FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12enc.c
Date: 2022-01-21 12:56:39
Exec Total Coverage
Lines: 599 688 87.1%
Branches: 320 411 77.9%

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 "libavutil/attributes.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/log.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/thread.h"
36 #include "libavutil/timecode.h"
37 #include "libavutil/stereo3d.h"
38
39 #include "avcodec.h"
40 #include "bytestream.h"
41 #include "mathops.h"
42 #include "mpeg12.h"
43 #include "mpeg12data.h"
44 #include "mpegutils.h"
45 #include "mpegvideo.h"
46 #include "profiles.h"
47
48 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
49 static const uint8_t svcd_scan_offset_placeholder[] = {
50 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
51 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
52 };
53
54 static uint8_t mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1];
55 static uint8_t fcode_tab[MAX_MV * 2 + 1];
56
57 static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2];
58 static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2];
59
60 /* simple include everything table for dc, first byte is bits
61 * number next 3 are code */
62 static uint32_t mpeg1_lum_dc_uni[512];
63 static uint32_t mpeg1_chr_dc_uni[512];
64
65 typedef struct MPEG12EncContext {
66 MpegEncContext mpeg;
67 AVRational frame_rate_ext;
68
69 int64_t timecode_frame_start; ///< GOP timecode frame start number, in non drop frame format
70 AVTimecode tc; ///< timecode context
71 char *tc_opt_str; ///< timecode option string
72 int drop_frame_timecode; ///< timecode is in drop frame format.
73 int scan_offset; ///< reserve space for SVCD scan offset user data.
74
75 int a53_cc;
76 int seq_disp_ext;
77 int video_format;
78 } MPEG12EncContext;
79
80 #define A53_MAX_CC_COUNT 0x1f
81 #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */
82
83 98 av_cold void ff_mpeg1_init_uni_ac_vlc(const RLTable *rl, uint8_t *uni_ac_vlc_len)
84 {
85 int i;
86
87
2/2
✓ Branch 0 taken 12544 times.
✓ Branch 1 taken 98 times.
12642 for (i = 0; i < 128; i++) {
88 12544 int level = i - 64;
89 int run;
90
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 12446 times.
12544 if (!level)
91 98 continue;
92
2/2
✓ Branch 0 taken 796544 times.
✓ Branch 1 taken 12446 times.
808990 for (run = 0; run < 64; run++) {
93 int len, code;
94 796544 int alevel = FFABS(level);
95
96
2/2
✓ Branch 0 taken 774788 times.
✓ Branch 1 taken 21756 times.
796544 if (alevel > rl->max_level[0][run])
97 774788 code = 111; /* rl->n */
98 else
99 21756 code = rl->index_run[0][run] + alevel - 1;
100
101
2/2
✓ Branch 0 taken 21756 times.
✓ Branch 1 taken 774788 times.
796544 if (code < 111) { /* rl->n */
102 /* length of VLC and sign */
103 21756 len = rl->table_vlc[code][1] + 1;
104 } else {
105 774788 len = rl->table_vlc[111 /* rl->n */][1] + 6;
106
107
1/2
✓ Branch 0 taken 774788 times.
✗ Branch 1 not taken.
774788 if (alevel < 128)
108 774788 len += 8;
109 else
110 len += 16;
111 }
112
113 796544 uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
114 }
115 }
116 98 }
117
118 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
119 49 static int find_frame_rate_index(MPEG12EncContext *mpeg12)
120 {
121 49 MpegEncContext *const s = &mpeg12->mpeg;
122 int i;
123 49 AVRational bestq = (AVRational) {0, 0};
124 AVRational ext;
125 49 AVRational target = av_inv_q(s->avctx->time_base);
126
127
1/2
✓ Branch 0 taken 441 times.
✗ Branch 1 not taken.
441 for (i = 1; i < 14; i++) {
128
3/4
✓ Branch 0 taken 441 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 392 times.
441 if (s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
129 i >= 9)
130 49 break;
131
132
2/2
✓ Branch 0 taken 1568 times.
✓ Branch 1 taken 392 times.
1960 for (ext.num=1; ext.num <= 4; ext.num++) {
133
2/2
✓ Branch 0 taken 50176 times.
✓ Branch 1 taken 1568 times.
51744 for (ext.den=1; ext.den <= 32; ext.den++) {
134 50176 AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]);
135
136
6/6
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 38912 times.
✓ Branch 2 taken 352 times.
✓ Branch 3 taken 10912 times.
✓ Branch 4 taken 264 times.
✓ Branch 5 taken 88 times.
50176 if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
137 23944 continue;
138
2/2
✓ Branch 0 taken 12768 times.
✓ Branch 1 taken 26232 times.
39000 if (av_gcd(ext.den, ext.num) != 1)
139 12768 continue;
140
141
2/2
✓ Branch 0 taken 26183 times.
✓ Branch 1 taken 49 times.
26232 if ( bestq.num==0
142
2/2
✓ Branch 1 taken 26084 times.
✓ Branch 2 taken 99 times.
26183 || av_nearer_q(target, bestq, q) < 0
143
5/6
✓ Branch 0 taken 9670 times.
✓ Branch 1 taken 16414 times.
✓ Branch 2 taken 246 times.
✓ Branch 3 taken 9424 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 246 times.
26084 || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) {
144 148 bestq = q;
145 148 s->frame_rate_index = i;
146 148 mpeg12->frame_rate_ext.num = ext.num;
147 148 mpeg12->frame_rate_ext.den = ext.den;
148 }
149 }
150 }
151 }
152
153
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if (av_cmp_q(target, bestq))
154 return -1;
155 else
156 49 return 0;
157 }
158
159 49 static av_cold int encode_init(AVCodecContext *avctx)
160 {
161 49 MPEG12EncContext *const mpeg12 = avctx->priv_data;
162 49 MpegEncContext *const s = &mpeg12->mpeg;
163 int ret;
164
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 11 times.
49 int max_size = avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 16383 : 4095;
165
166
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
49 if (avctx->width > max_size || avctx->height > max_size) {
167 av_log(avctx, AV_LOG_ERROR, "%s does not support resolutions above %dx%d\n",
168 CONFIG_SMALL ? avctx->codec->name : avctx->codec->long_name,
169 max_size, max_size);
170 return AVERROR(EINVAL);
171 }
172
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
49 if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
173 av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
174 return AVERROR(EINVAL);
175 }
176
177
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
178
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
49 if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) {
179 av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiples of 4096\n"
180 "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
181 return AVERROR(EINVAL);
182 }
183 }
184
185
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (avctx->profile == FF_PROFILE_UNKNOWN) {
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (avctx->level != FF_LEVEL_UNKNOWN) {
187 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
188 return AVERROR(EINVAL);
189 }
190 /* Main or 4:2:2 */
191 49 avctx->profile = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? FF_PROFILE_MPEG2_MAIN
192
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5 times.
49 : FF_PROFILE_MPEG2_422;
193 }
194
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (avctx->level == FF_LEVEL_UNKNOWN) {
195
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 44 times.
49 if (avctx->profile == FF_PROFILE_MPEG2_422) { /* 4:2:2 */
196
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)
197 5 avctx->level = 5; /* Main */
198 else
199 avctx->level = 2; /* High */
200 } else {
201
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (avctx->profile != FF_PROFILE_MPEG2_HIGH &&
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
203 av_log(avctx, AV_LOG_ERROR,
204 "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
205 return AVERROR(EINVAL);
206 }
207
3/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
44 if (avctx->width <= 720 && avctx->height <= 576)
208 43 avctx->level = 8; /* Main */
209
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (avctx->width <= 1440)
210 1 avctx->level = 6; /* High 1440 */
211 else
212 avctx->level = 4; /* High */
213 }
214 }
215
216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if ((ret = ff_mpv_encode_init(avctx)) < 0)
217 return ret;
218
219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if (find_frame_rate_index(mpeg12) < 0) {
220 if (s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
221 av_log(avctx, AV_LOG_ERROR, "MPEG-1/2 does not support %d/%d fps\n",
222 avctx->time_base.den, avctx->time_base.num);
223 return AVERROR(EINVAL);
224 } else {
225 av_log(avctx, AV_LOG_INFO,
226 "MPEG-1/2 does not support %d/%d fps, there may be AV sync issues\n",
227 avctx->time_base.den, avctx->time_base.num);
228 }
229 }
230
231
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
49 mpeg12->drop_frame_timecode = mpeg12->drop_frame_timecode || !!(avctx->flags2 & AV_CODEC_FLAG2_DROP_FRAME_TIMECODE);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (mpeg12->drop_frame_timecode)
233 mpeg12->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME;
234
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
49 if (mpeg12->drop_frame_timecode && s->frame_rate_index != 4) {
235 av_log(avctx, AV_LOG_ERROR,
236 "Drop frame time code only allowed with 1001/30000 fps\n");
237 return AVERROR(EINVAL);
238 }
239
240
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 43 times.
49 if (mpeg12->tc_opt_str) {
241 6 AVRational rate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
242 6 int ret = av_timecode_init_from_string(&mpeg12->tc, rate, mpeg12->tc_opt_str, s);
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
244 return ret;
245 6 mpeg12->drop_frame_timecode = !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME);
246 6 mpeg12->timecode_frame_start = mpeg12->tc.start;
247 } else {
248 43 mpeg12->timecode_frame_start = 0; // default is -1
249 }
250
251 49 return 0;
252 }
253
254 295237 static void put_header(MpegEncContext *s, int header)
255 {
256 295237 align_put_bits(&s->pb);
257 295237 put_bits(&s->pb, 16, header >> 16);
258 295237 put_sbits(&s->pb, 16, header);
259 295237 }
260
261 /* put sequence header if needed */
262 2265 static void mpeg1_encode_sequence_header(MpegEncContext *s)
263 {
264 2265 MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)s;
265 unsigned int vbv_buffer_size, fps, v;
266 int i, constraint_parameter_flag;
267 uint64_t time_code;
268 2265 int64_t best_aspect_error = INT64_MAX;
269 2265 AVRational aspect_ratio = s->avctx->sample_aspect_ratio;
270
271
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2240 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
2265 if (aspect_ratio.num == 0 || aspect_ratio.den == 0)
272 2240 aspect_ratio = (AVRational){1,1}; // pixel aspect 1.1 (VGA)
273
274
2/2
✓ Branch 0 taken 383 times.
✓ Branch 1 taken 1882 times.
2265 if (s->current_picture.f->key_frame) {
275 383 AVRational framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
276
277 /* MPEG-1 header repeated every GOP */
278 383 put_header(s, SEQ_START_CODE);
279
280 383 put_sbits(&s->pb, 12, s->width & 0xFFF);
281 383 put_sbits(&s->pb, 12, s->height & 0xFFF);
282
283
2/2
✓ Branch 0 taken 5362 times.
✓ Branch 1 taken 383 times.
5745 for (i = 1; i < 15; i++) {
284 5362 int64_t error = aspect_ratio.num * (1LL<<32) / aspect_ratio.den;
285
4/4
✓ Branch 0 taken 4620 times.
✓ Branch 1 taken 742 times.
✓ Branch 2 taken 330 times.
✓ Branch 3 taken 4290 times.
5362 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
286 1072 error -= (1LL<<32) / ff_mpeg1_aspect[i];
287 else
288 4290 error -= (1LL<<32)*ff_mpeg2_aspect[i].num * s->height / s->width / ff_mpeg2_aspect[i].den;
289
290 5362 error = FFABS(error);
291
292
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 4976 times.
5362 if (error - 2 <= best_aspect_error) {
293 386 best_aspect_error = error;
294 386 s->aspect_ratio_info = i;
295 }
296 }
297
298 383 put_bits(&s->pb, 4, s->aspect_ratio_info);
299 383 put_bits(&s->pb, 4, s->frame_rate_index);
300
301
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 230 times.
383 if (s->avctx->rc_max_rate) {
302 153 v = (s->avctx->rc_max_rate + 399) / 400;
303
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)
304 v = 0x3ffff;
305 } else {
306 230 v = 0x3FFFF;
307 }
308
309
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 230 times.
383 if (s->avctx->rc_buffer_size)
310 153 vbv_buffer_size = s->avctx->rc_buffer_size;
311 else
312 /* VBV calculation: Scaled so that a VCD has the proper
313 * VBV size of 40 kilobytes */
314 230 vbv_buffer_size = ((20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
315 383 vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
316
317 383 put_sbits(&s->pb, 18, v);
318 383 put_bits(&s->pb, 1, 1); // marker
319 383 put_sbits(&s->pb, 10, vbv_buffer_size);
320
321 383 constraint_parameter_flag =
322 763 s->width <= 768 &&
323
2/2
✓ Branch 0 taken 230 times.
✓ Branch 1 taken 150 times.
380 s->height <= 576 &&
324
2/2
✓ Branch 0 taken 221 times.
✓ Branch 1 taken 9 times.
230 s->mb_width * s->mb_height <= 396 &&
325
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 6 times.
221 s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den &&
326
1/2
✓ Branch 0 taken 215 times.
✗ Branch 1 not taken.
215 framerate.num <= framerate.den * 30 &&
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 s->avctx->me_range &&
328 s->avctx->me_range < 128 &&
329 vbv_buffer_size <= 20 &&
330
2/2
✓ Branch 0 taken 380 times.
✓ Branch 1 taken 3 times.
763 v <= 1856000 / 400 &&
331 s->codec_id == AV_CODEC_ID_MPEG1VIDEO;
332
333 383 put_bits(&s->pb, 1, constraint_parameter_flag);
334
335 383 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
336 383 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
337
338
2/2
✓ Branch 0 taken 330 times.
✓ Branch 1 taken 53 times.
383 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
339 AVFrameSideData *side_data;
340 330 int width = s->width;
341 330 int height = s->height;
342 int use_seq_disp_ext;
343
344 330 put_header(s, EXT_START_CODE);
345 330 put_bits(&s->pb, 4, 1); // seq ext
346
347 330 put_bits(&s->pb, 1, s->avctx->profile == FF_PROFILE_MPEG2_422); // escx 1 for 4:2:2 profile
348
349 330 put_bits(&s->pb, 3, s->avctx->profile); // profile
350 330 put_bits(&s->pb, 4, s->avctx->level); // level
351
352 330 put_bits(&s->pb, 1, s->progressive_sequence);
353 330 put_bits(&s->pb, 2, s->chroma_format);
354 330 put_bits(&s->pb, 2, s->width >> 12);
355 330 put_bits(&s->pb, 2, s->height >> 12);
356 330 put_bits(&s->pb, 12, v >> 18); // bitrate ext
357 330 put_bits(&s->pb, 1, 1); // marker
358 330 put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
359 330 put_bits(&s->pb, 1, s->low_delay);
360 330 put_bits(&s->pb, 2, mpeg12->frame_rate_ext.num-1); // frame_rate_ext_n
361 330 put_bits(&s->pb, 5, mpeg12->frame_rate_ext.den-1); // frame_rate_ext_d
362
363 330 side_data = av_frame_get_side_data(s->current_picture_ptr->f, AV_FRAME_DATA_PANSCAN);
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 330 times.
330 if (side_data) {
365 AVPanScan *pan_scan = (AVPanScan *)side_data->data;
366 if (pan_scan->width && pan_scan->height) {
367 width = pan_scan->width >> 4;
368 height = pan_scan->height >> 4;
369 }
370 }
371
372 990 use_seq_disp_ext = (width != s->width ||
373
1/2
✓ Branch 0 taken 330 times.
✗ Branch 1 not taken.
330 height != s->height ||
374
1/2
✓ Branch 0 taken 330 times.
✗ Branch 1 not taken.
330 s->avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
375
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 3 times.
330 s->avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
376
2/4
✓ Branch 0 taken 330 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 327 times.
✗ Branch 3 not taken.
987 s->avctx->colorspace != AVCOL_SPC_UNSPECIFIED ||
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 mpeg12->video_format != VIDEO_FORMAT_UNSPECIFIED);
378
379
1/2
✓ Branch 0 taken 330 times.
✗ Branch 1 not taken.
330 if (mpeg12->seq_disp_ext == 1 ||
380
3/4
✓ Branch 0 taken 330 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 327 times.
330 (mpeg12->seq_disp_ext == -1 && use_seq_disp_ext)) {
381 3 put_header(s, EXT_START_CODE);
382 3 put_bits(&s->pb, 4, 2); // sequence display extension
383 3 put_bits(&s->pb, 3, mpeg12->video_format); // video_format
384 3 put_bits(&s->pb, 1, 1); // colour_description
385 3 put_bits(&s->pb, 8, s->avctx->color_primaries); // colour_primaries
386 3 put_bits(&s->pb, 8, s->avctx->color_trc); // transfer_characteristics
387 3 put_bits(&s->pb, 8, s->avctx->colorspace); // matrix_coefficients
388 3 put_bits(&s->pb, 14, width); // display_horizontal_size
389 3 put_bits(&s->pb, 1, 1); // marker_bit
390 3 put_bits(&s->pb, 14, height); // display_vertical_size
391 3 put_bits(&s->pb, 3, 0); // remaining 3 bits are zero padding
392 }
393 }
394
395 383 put_header(s, GOP_START_CODE);
396 383 put_bits(&s->pb, 1, mpeg12->drop_frame_timecode); // drop frame flag
397 /* time code: we must convert from the real frame rate to a
398 * fake MPEG frame rate in case of low frame rate */
399 383 fps = (framerate.num + framerate.den / 2) / framerate.den;
400 383 time_code = s->current_picture_ptr->f->coded_picture_number +
401 383 mpeg12->timecode_frame_start;
402
403 383 s->gop_picture_number = s->current_picture_ptr->f->coded_picture_number;
404
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383 times.
383 av_assert0(mpeg12->drop_frame_timecode == !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
406
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 374 times.
383 if (mpeg12->drop_frame_timecode)
407 9 time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
408
409 383 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
410 383 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
411 383 put_bits(&s->pb, 1, 1);
412 383 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
413 383 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
414
5/6
✓ Branch 0 taken 383 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
✓ Branch 3 taken 150 times.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 185 times.
383 put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) || s->intra_only || !s->gop_picture_number);
415 383 put_bits(&s->pb, 1, 0); // broken link
416 }
417 2265 }
418
419 1570182 static inline void encode_mb_skip_run(MpegEncContext *s, int run)
420 {
421
2/2
✓ Branch 0 taken 69198 times.
✓ Branch 1 taken 1570182 times.
1639380 while (run >= 33) {
422 69198 put_bits(&s->pb, 11, 0x008);
423 69198 run -= 33;
424 }
425 1570182 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
426 1570182 ff_mpeg12_mbAddrIncrTable[run][0]);
427 1570182 }
428
429 475856 static av_always_inline void put_qscale(MpegEncContext *s)
430 {
431 475856 put_bits(&s->pb, 5, s->qscale);
432 475856 }
433
434 290088 void ff_mpeg1_encode_slice_header(MpegEncContext *s)
435 {
436
3/4
✓ Branch 0 taken 282815 times.
✓ Branch 1 taken 7273 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 282815 times.
290088 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) {
437 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
438 /* slice_vertical_position_extension */
439 put_bits(&s->pb, 3, s->mb_y >> 7);
440 } else {
441 290088 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
442 }
443 290088 put_qscale(s);
444 /* slice extra information */
445 290088 put_bits(&s->pb, 1, 0);
446 290088 }
447
448 2265 void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
449 {
450 2265 MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)s;
451 AVFrameSideData *side_data;
452 2265 mpeg1_encode_sequence_header(s);
453
454 /* MPEG-1 picture header */
455 2265 put_header(s, PICTURE_START_CODE);
456 /* temporal reference */
457
458 // RAL: s->picture_number instead of s->fake_picture_number
459 2265 put_bits(&s->pb, 10,
460 2265 (s->picture_number - s->gop_picture_number) & 0x3ff);
461 2265 put_bits(&s->pb, 3, s->pict_type);
462
463 2265 s->vbv_delay_ptr = s->pb.buf + put_bytes_count(&s->pb, 0);
464 2265 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
465
466 // RAL: Forward f_code also needed for B-frames
467
2/2
✓ Branch 0 taken 1090 times.
✓ Branch 1 taken 1175 times.
2265 if (s->pict_type == AV_PICTURE_TYPE_P ||
468
2/2
✓ Branch 0 taken 707 times.
✓ Branch 1 taken 383 times.
1090 s->pict_type == AV_PICTURE_TYPE_B) {
469 1882 put_bits(&s->pb, 1, 0); /* half pel coordinates */
470
2/2
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 1455 times.
1882 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
471 427 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
472 else
473 1455 put_bits(&s->pb, 3, 7); /* forward_f_code */
474 }
475
476 // RAL: Backward f_code necessary for B-frames
477
2/2
✓ Branch 0 taken 707 times.
✓ Branch 1 taken 1558 times.
2265 if (s->pict_type == AV_PICTURE_TYPE_B) {
478 707 put_bits(&s->pb, 1, 0); /* half pel coordinates */
479
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 563 times.
707 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
480 144 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
481 else
482 563 put_bits(&s->pb, 3, 7); /* backward_f_code */
483 }
484
485 2265 put_bits(&s->pb, 1, 0); /* extra bit picture */
486
487 2265 s->frame_pred_frame_dct = 1;
488
2/2
✓ Branch 0 taken 1785 times.
✓ Branch 1 taken 480 times.
2265 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
489 1785 put_header(s, EXT_START_CODE);
490 1785 put_bits(&s->pb, 4, 8); /* pic ext */
491
2/2
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 892 times.
1785 if (s->pict_type == AV_PICTURE_TYPE_P ||
492
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 330 times.
893 s->pict_type == AV_PICTURE_TYPE_B) {
493 1455 put_bits(&s->pb, 4, s->f_code);
494 1455 put_bits(&s->pb, 4, s->f_code);
495 } else {
496 330 put_bits(&s->pb, 8, 255);
497 }
498
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 1222 times.
1785 if (s->pict_type == AV_PICTURE_TYPE_B) {
499 563 put_bits(&s->pb, 4, s->b_code);
500 563 put_bits(&s->pb, 4, s->b_code);
501 } else {
502 1222 put_bits(&s->pb, 8, 255);
503 }
504 1785 put_bits(&s->pb, 2, s->intra_dc_precision);
505
506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1785 times.
1785 av_assert0(s->picture_structure == PICT_FRAME);
507 1785 put_bits(&s->pb, 2, s->picture_structure);
508
2/2
✓ Branch 0 taken 835 times.
✓ Branch 1 taken 950 times.
1785 if (s->progressive_sequence)
509 835 put_bits(&s->pb, 1, 0); /* no repeat */
510 else
511 950 put_bits(&s->pb, 1, s->current_picture_ptr->f->top_field_first);
512 /* XXX: optimize the generation of this flag with entropy measures */
513 1785 s->frame_pred_frame_dct = s->progressive_sequence;
514
515 1785 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
516 1785 put_bits(&s->pb, 1, s->concealment_motion_vectors);
517 1785 put_bits(&s->pb, 1, s->q_scale_type);
518 1785 put_bits(&s->pb, 1, s->intra_vlc_format);
519 1785 put_bits(&s->pb, 1, s->alternate_scan);
520 1785 put_bits(&s->pb, 1, s->repeat_first_field);
521 1785 s->progressive_frame = s->progressive_sequence;
522 /* chroma_420_type */
523
2/2
✓ Branch 0 taken 1435 times.
✓ Branch 1 taken 350 times.
3220 put_bits(&s->pb, 1, s->chroma_format ==
524 1435 CHROMA_420 ? s->progressive_frame : 0);
525 1785 put_bits(&s->pb, 1, s->progressive_frame);
526 1785 put_bits(&s->pb, 1, 0); /* composite_display_flag */
527 }
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2265 times.
2265 if (mpeg12->scan_offset) {
529 int i;
530
531 put_header(s, USER_START_CODE);
532 for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
533 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
534 }
535 2265 side_data = av_frame_get_side_data(s->current_picture_ptr->f,
536 AV_FRAME_DATA_STEREO3D);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2265 times.
2265 if (side_data) {
538 AVStereo3D *stereo = (AVStereo3D *)side_data->data;
539 uint8_t fpa_type;
540
541 switch (stereo->type) {
542 case AV_STEREO3D_SIDEBYSIDE:
543 fpa_type = 0x03;
544 break;
545 case AV_STEREO3D_TOPBOTTOM:
546 fpa_type = 0x04;
547 break;
548 case AV_STEREO3D_2D:
549 fpa_type = 0x08;
550 break;
551 case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
552 fpa_type = 0x23;
553 break;
554 default:
555 fpa_type = 0;
556 break;
557 }
558
559 if (fpa_type != 0) {
560 put_header(s, USER_START_CODE);
561 put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier
562 put_bits(&s->pb, 8, 'P');
563 put_bits(&s->pb, 8, '3');
564 put_bits(&s->pb, 8, 'D');
565 put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
566
567 put_bits(&s->pb, 1, 1); // reserved_bit
568 put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
569 put_bits(&s->pb, 8, 0x04); // reserved_data[0]
570 put_bits(&s->pb, 8, 0xFF); // reserved_data[1]
571 }
572 }
573
574
2/2
✓ Branch 0 taken 1785 times.
✓ Branch 1 taken 480 times.
2265 if (CONFIG_MPEG2VIDEO_ENCODER && mpeg12->a53_cc) {
575 1785 side_data = av_frame_get_side_data(s->current_picture_ptr->f,
576 AV_FRAME_DATA_A53_CC);
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1785 times.
1785 if (side_data) {
578 if (side_data->size <= A53_MAX_CC_COUNT * 3 && side_data->size % 3 == 0) {
579 int i = 0;
580
581 put_header (s, USER_START_CODE);
582
583 put_bits(&s->pb, 8, 'G'); // user_identifier
584 put_bits(&s->pb, 8, 'A');
585 put_bits(&s->pb, 8, '9');
586 put_bits(&s->pb, 8, '4');
587 put_bits(&s->pb, 8, 3); // user_data_type_code
588 put_bits(&s->pb, 8,
589 (side_data->size / 3 & A53_MAX_CC_COUNT) | 0x40); // flags, cc_count
590 put_bits(&s->pb, 8, 0xff); // em_data
591
592 for (i = 0; i < side_data->size; i++)
593 put_bits(&s->pb, 8, side_data->data[i]);
594
595 put_bits(&s->pb, 8, 0xff); // marker_bits
596 } else {
597 av_log(s->avctx, AV_LOG_WARNING,
598 "Closed Caption size (%"SIZE_SPECIFIER") can not exceed "
599 "93 bytes and must be a multiple of 3\n", side_data->size);
600 }
601 }
602 }
603
604 2265 s->mb_y = 0;
605 2265 ff_mpeg1_encode_slice_header(s);
606 2265 }
607
608 1309340 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
609 int has_mv, int field_motion)
610 {
611 1309340 put_bits(&s->pb, n, bits);
612
2/2
✓ Branch 0 taken 702603 times.
✓ Branch 1 taken 606737 times.
1309340 if (!s->frame_pred_frame_dct) {
613
2/2
✓ Branch 0 taken 365122 times.
✓ Branch 1 taken 337481 times.
702603 if (has_mv)
614 /* motion_type: frame/field */
615 365122 put_bits(&s->pb, 2, 2 - field_motion);
616 702603 put_bits(&s->pb, 1, s->interlaced_dct);
617 }
618 1309340 }
619
620 // RAL: Parameter added: f_or_b_code
621 2967158 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
622 {
623
2/2
✓ Branch 0 taken 1173275 times.
✓ Branch 1 taken 1793883 times.
2967158 if (val == 0) {
624 /* zero vector, corresponds to ff_mpeg12_mbMotionVectorTable[0] */
625 1173275 put_bits(&s->pb, 1, 0x01);
626 } else {
627 int code, sign, bits;
628 1793883 int bit_size = f_or_b_code - 1;
629 1793883 int range = 1 << bit_size;
630 /* modulo encoding */
631 1793883 val = sign_extend(val, 5 + bit_size);
632
633
2/2
✓ Branch 0 taken 874554 times.
✓ Branch 1 taken 919329 times.
1793883 if (val >= 0) {
634 874554 val--;
635 874554 code = (val >> bit_size) + 1;
636 874554 bits = val & (range - 1);
637 874554 sign = 0;
638 } else {
639 919329 val = -val;
640 919329 val--;
641 919329 code = (val >> bit_size) + 1;
642 919329 bits = val & (range - 1);
643 919329 sign = 1;
644 }
645
646 av_assert2(code > 0 && code <= 16);
647
648 1793883 put_bits(&s->pb,
649 1793883 ff_mpeg12_mbMotionVectorTable[code][1],
650 1793883 ff_mpeg12_mbMotionVectorTable[code][0]);
651
652 1793883 put_bits(&s->pb, 1, sign);
653
2/2
✓ Branch 0 taken 1248476 times.
✓ Branch 1 taken 545407 times.
1793883 if (bit_size > 0)
654 1248476 put_bits(&s->pb, bit_size, bits);
655 }
656 2967158 }
657
658 3228530 static inline void encode_dc(MpegEncContext *s, int diff, int component)
659 {
660 3228530 unsigned int diff_u = diff + 255;
661
2/2
✓ Branch 0 taken 162210 times.
✓ Branch 1 taken 3066320 times.
3228530 if (diff_u >= 511) {
662 int index;
663
664
2/2
✓ Branch 0 taken 113286 times.
✓ Branch 1 taken 48924 times.
162210 if (diff < 0) {
665 113286 index = av_log2_16bit(-2 * diff);
666 113286 diff--;
667 } else {
668 48924 index = av_log2_16bit(2 * diff);
669 }
670
2/2
✓ Branch 0 taken 91380 times.
✓ Branch 1 taken 70830 times.
162210 if (component == 0)
671 91380 put_bits(&s->pb,
672 91380 ff_mpeg12_vlc_dc_lum_bits[index] + index,
673 91380 (ff_mpeg12_vlc_dc_lum_code[index] << index) +
674 91380 av_mod_uintp2(diff, index));
675 else
676 70830 put_bits(&s->pb,
677 70830 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
678 70830 (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
679 70830 av_mod_uintp2(diff, index));
680 } else {
681
2/2
✓ Branch 0 taken 1666960 times.
✓ Branch 1 taken 1399360 times.
3066320 if (component == 0)
682 1666960 put_bits(&s->pb,
683 1666960 mpeg1_lum_dc_uni[diff + 255] & 0xFF,
684 1666960 mpeg1_lum_dc_uni[diff + 255] >> 8);
685 else
686 1399360 put_bits(&s->pb,
687 1399360 mpeg1_chr_dc_uni[diff + 255] & 0xFF,
688 1399360 mpeg1_chr_dc_uni[diff + 255] >> 8);
689 }
690 3228530 }
691
692 6457398 static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
693 {
694 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
695 int code, component;
696 6457398 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
697
698 6457398 last_index = s->block_last_index[n];
699
700 /* DC coef */
701
2/2
✓ Branch 0 taken 3228530 times.
✓ Branch 1 taken 3228868 times.
6457398 if (s->mb_intra) {
702
2/2
✓ Branch 0 taken 1470190 times.
✓ Branch 1 taken 1758340 times.
3228530 component = (n <= 3 ? 0 : (n & 1) + 1);
703 3228530 dc = block[0]; /* overflow is impossible */
704 3228530 diff = dc - s->last_dc[component];
705 3228530 encode_dc(s, diff, component);
706 3228530 s->last_dc[component] = dc;
707 3228530 i = 1;
708
2/2
✓ Branch 0 taken 2643692 times.
✓ Branch 1 taken 584838 times.
3228530 if (s->intra_vlc_format)
709 2643692 table_vlc = ff_rl_mpeg2.table_vlc;
710 } else {
711 /* encode the first coefficient: needs to be done here because
712 * it is handled slightly differently */
713 3228868 level = block[0];
714
2/2
✓ Branch 0 taken 945317 times.
✓ Branch 1 taken 2283551 times.
3228868 if (abs(level) == 1) {
715 945317 code = ((uint32_t)level >> 31); /* the sign bit */
716 945317 put_bits(&s->pb, 2, code | 0x02);
717 945317 i = 1;
718 } else {
719 2283551 i = 0;
720 2283551 last_non_zero = -1;
721 2283551 goto next_coef;
722 }
723 }
724
725 /* now quantify & encode AC coefs */
726 4173847 last_non_zero = i - 1;
727
728
2/2
✓ Branch 0 taken 156669072 times.
✓ Branch 1 taken 6457398 times.
163126470 for (; i <= last_index; i++) {
729 156669072 j = s->intra_scantable.permutated[i];
730 156669072 level = block[j];
731
732 158952623 next_coef:
733 /* encode using VLC */
734
2/2
✓ Branch 0 taken 65535795 times.
✓ Branch 1 taken 93416828 times.
158952623 if (level != 0) {
735 65535795 run = i - last_non_zero - 1;
736
737 65535795 alevel = level;
738 65535795 MASK_ABS(sign, alevel);
739 65535795 sign &= 1;
740
741
2/2
✓ Branch 0 taken 63455458 times.
✓ Branch 1 taken 2080337 times.
65535795 if (alevel <= ff_rl_mpeg1.max_level[0][run]) {
742 63455458 code = ff_rl_mpeg1.index_run[0][run] + alevel - 1;
743 /* store the VLC & sign at once */
744 63455458 put_bits(&s->pb, table_vlc[code][1] + 1,
745 63455458 (table_vlc[code][0] << 1) + sign);
746 } else {
747 /* Escape seems to be pretty rare <5% so I do not optimize it;
748 * the following value is the common escape value for both
749 * possible tables (i.e. table_vlc[111]). */
750 2080337 put_bits(&s->pb, 6, 0x01);
751 /* escape: only clip in this case */
752 2080337 put_bits(&s->pb, 6, run);
753
2/2
✓ Branch 0 taken 82631 times.
✓ Branch 1 taken 1997706 times.
2080337 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
754
1/2
✓ Branch 0 taken 82631 times.
✗ Branch 1 not taken.
82631 if (alevel < 128) {
755 82631 put_sbits(&s->pb, 8, level);
756 } else {
757 if (level < 0)
758 put_bits(&s->pb, 16, 0x8001 + level + 255);
759 else
760 put_sbits(&s->pb, 16, level);
761 }
762 } else {
763 1997706 put_sbits(&s->pb, 12, level);
764 }
765 }
766 65535795 last_non_zero = i;
767 }
768 }
769 /* end of block */
770 6457398 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
771 6457398 }
772
773 1657917 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
774 int16_t block[8][64],
775 int motion_x, int motion_y,
776 int mb_block_count,
777 int chroma_y_shift)
778 {
779 /* MPEG-1 is always 420. */
780 #define IS_MPEG1(s) (chroma_y_shift == 1 && (s)->codec_id == AV_CODEC_ID_MPEG1VIDEO)
781 int i, cbp;
782 1657917 const int mb_x = s->mb_x;
783 1657917 const int mb_y = s->mb_y;
784
4/4
✓ Branch 0 taken 321836 times.
✓ Branch 1 taken 1336081 times.
✓ Branch 2 taken 317179 times.
✓ Branch 3 taken 4657 times.
1657917 const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
785
786 /* compute cbp */
787 1657917 cbp = 0;
788
2/2
✓ Branch 0 taken 11233732 times.
✓ Branch 1 taken 1657917 times.
12891649 for (i = 0; i < mb_block_count; i++)
789
2/2
✓ Branch 0 taken 6457398 times.
✓ Branch 1 taken 4776334 times.
11233732 if (s->block_last_index[i] >= 0)
790 6457398 cbp |= 1 << (mb_block_count - 1 - i);
791
792
6/6
✓ Branch 0 taken 348577 times.
✓ Branch 1 taken 1309340 times.
✓ Branch 2 taken 341892 times.
✓ Branch 3 taken 6685 times.
✓ Branch 4 taken 320336 times.
✓ Branch 5 taken 21556 times.
1657917 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
793
2/2
✓ Branch 0 taken 8338 times.
✓ Branch 1 taken 311998 times.
320336 (mb_x != s->mb_width - 1 ||
794
6/6
✓ Branch 0 taken 8062 times.
✓ Branch 1 taken 276 times.
✓ Branch 2 taken 6185 times.
✓ Branch 3 taken 1877 times.
✓ Branch 4 taken 542 times.
✓ Branch 5 taken 5643 times.
8338 (mb_y != s->end_mb_y - 1 && IS_MPEG1(s))) &&
795
4/4
✓ Branch 0 taken 146406 times.
✓ Branch 1 taken 166134 times.
✓ Branch 2 taken 62550 times.
✓ Branch 3 taken 83856 times.
312540 ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
796
4/4
✓ Branch 0 taken 166134 times.
✓ Branch 1 taken 62550 times.
✓ Branch 2 taken 21548 times.
✓ Branch 3 taken 144586 times.
228684 (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir &&
797 21548 (((s->mv_dir & MV_DIR_FORWARD)
798 20583 ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) |
799
4/4
✓ Branch 0 taken 20583 times.
✓ Branch 1 taken 965 times.
✓ Branch 2 taken 3879 times.
✓ Branch 3 taken 17669 times.
43096 (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
800 21548 ((s->mv_dir & MV_DIR_BACKWARD)
801 20991 ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) |
802
2/2
✓ Branch 0 taken 20991 times.
✓ Branch 1 taken 557 times.
21548 (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
803 87735 s->mb_skip_run++;
804 87735 s->qscale -= s->dquant;
805 87735 s->skip_count++;
806 87735 s->misc_bits++;
807 87735 s->last_bits++;
808
2/2
✓ Branch 0 taken 83856 times.
✓ Branch 1 taken 3879 times.
87735 if (s->pict_type == AV_PICTURE_TYPE_P) {
809 83856 s->last_mv[0][0][0] =
810 83856 s->last_mv[0][0][1] =
811 83856 s->last_mv[0][1][0] =
812 83856 s->last_mv[0][1][1] = 0;
813 }
814 } else {
815
2/2
✓ Branch 0 taken 317179 times.
✓ Branch 1 taken 1253003 times.
1570182 if (first_mb) {
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 317179 times.
317179 av_assert0(s->mb_skip_run == 0);
817 317179 encode_mb_skip_run(s, s->mb_x);
818 } else {
819 1253003 encode_mb_skip_run(s, s->mb_skip_run);
820 }
821
822
2/2
✓ Branch 0 taken 388614 times.
✓ Branch 1 taken 1181568 times.
1570182 if (s->pict_type == AV_PICTURE_TYPE_I) {
823
3/4
✓ Branch 0 taken 43950 times.
✓ Branch 1 taken 344664 times.
✓ Branch 2 taken 43950 times.
✗ Branch 3 not taken.
388614 if (s->dquant && cbp) {
824 /* macroblock_type: macroblock_quant = 1 */
825 43950 put_mb_modes(s, 2, 1, 0, 0);
826 43950 put_qscale(s);
827 } else {
828 /* macroblock_type: macroblock_quant = 0 */
829 344664 put_mb_modes(s, 1, 1, 0, 0);
830 344664 s->qscale -= s->dquant;
831 }
832 388614 s->misc_bits += get_bits_diff(s);
833 388614 s->i_count++;
834
2/2
✓ Branch 0 taken 50971 times.
✓ Branch 1 taken 1130597 times.
1181568 } else if (s->mb_intra) {
835
3/4
✓ Branch 0 taken 11248 times.
✓ Branch 1 taken 39723 times.
✓ Branch 2 taken 11248 times.
✗ Branch 3 not taken.
50971 if (s->dquant && cbp) {
836 11248 put_mb_modes(s, 6, 0x01, 0, 0);
837 11248 put_qscale(s);
838 } else {
839 39723 put_mb_modes(s, 5, 0x03, 0, 0);
840 39723 s->qscale -= s->dquant;
841 }
842 50971 s->misc_bits += get_bits_diff(s);
843 50971 s->i_count++;
844 50971 memset(s->last_mv, 0, sizeof(s->last_mv));
845
2/2
✓ Branch 0 taken 549253 times.
✓ Branch 1 taken 581344 times.
1130597 } else if (s->pict_type == AV_PICTURE_TYPE_P) {
846
2/2
✓ Branch 0 taken 526300 times.
✓ Branch 1 taken 22953 times.
549253 if (s->mv_type == MV_TYPE_16X16) {
847
2/2
✓ Branch 0 taken 459195 times.
✓ Branch 1 taken 67105 times.
526300 if (cbp != 0) {
848
2/2
✓ Branch 0 taken 47018 times.
✓ Branch 1 taken 412177 times.
459195 if ((motion_x | motion_y) == 0) {
849
2/2
✓ Branch 0 taken 4208 times.
✓ Branch 1 taken 42810 times.
47018 if (s->dquant) {
850 /* macroblock_pattern & quant */
851 4208 put_mb_modes(s, 5, 1, 0, 0);
852 4208 put_qscale(s);
853 } else {
854 /* macroblock_pattern only */
855 42810 put_mb_modes(s, 2, 1, 0, 0);
856 }
857 47018 s->misc_bits += get_bits_diff(s);
858 } else {
859
2/2
✓ Branch 0 taken 70756 times.
✓ Branch 1 taken 341421 times.
412177 if (s->dquant) {
860 70756 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
861 70756 put_qscale(s);
862 } else {
863 341421 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
864 }
865 412177 s->misc_bits += get_bits_diff(s);
866 // RAL: f_code parameter added
867 412177 mpeg1_encode_motion(s,
868 412177 motion_x - s->last_mv[0][0][0],
869 s->f_code);
870 // RAL: f_code parameter added
871 412177 mpeg1_encode_motion(s,
872 412177 motion_y - s->last_mv[0][0][1],
873 s->f_code);
874 412177 s->mv_bits += get_bits_diff(s);
875 }
876 } else {
877 67105 put_bits(&s->pb, 3, 1); /* motion only */
878
2/2
✓ Branch 0 taken 16441 times.
✓ Branch 1 taken 50664 times.
67105 if (!s->frame_pred_frame_dct)
879 16441 put_bits(&s->pb, 2, 2); /* motion_type: frame */
880 67105 s->misc_bits += get_bits_diff(s);
881 // RAL: f_code parameter added
882 67105 mpeg1_encode_motion(s,
883 67105 motion_x - s->last_mv[0][0][0],
884 s->f_code);
885 // RAL: f_code parameter added
886 67105 mpeg1_encode_motion(s,
887 67105 motion_y - s->last_mv[0][0][1],
888 s->f_code);
889 67105 s->qscale -= s->dquant;
890 67105 s->mv_bits += get_bits_diff(s);
891 }
892 526300 s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
893 526300 s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y;
894 } else {
895 av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
896
897
2/2
✓ Branch 0 taken 21459 times.
✓ Branch 1 taken 1494 times.
22953 if (cbp) {
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21459 times.
21459 if (s->dquant) {
899 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
900 put_qscale(s);
901 } else {
902 21459 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
903 }
904 } else {
905 1494 put_bits(&s->pb, 3, 1); /* motion only */
906 1494 put_bits(&s->pb, 2, 1); /* motion_type: field */
907 1494 s->qscale -= s->dquant;
908 }
909 22953 s->misc_bits += get_bits_diff(s);
910
2/2
✓ Branch 0 taken 45906 times.
✓ Branch 1 taken 22953 times.
68859 for (i = 0; i < 2; i++) {
911 45906 put_bits(&s->pb, 1, s->field_select[0][i]);
912 45906 mpeg1_encode_motion(s,
913 45906 s->mv[0][i][0] - s->last_mv[0][i][0],
914 s->f_code);
915 45906 mpeg1_encode_motion(s,
916 45906 s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
917 s->f_code);
918 45906 s->last_mv[0][i][0] = s->mv[0][i][0];
919 45906 s->last_mv[0][i][1] = 2 * s->mv[0][i][1];
920 }
921 22953 s->mv_bits += get_bits_diff(s);
922 }
923
2/2
✓ Branch 0 taken 480654 times.
✓ Branch 1 taken 68599 times.
549253 if (cbp) {
924
2/2
✓ Branch 0 taken 404731 times.
✓ Branch 1 taken 75923 times.
480654 if (chroma_y_shift) {
925 404731 put_bits(&s->pb,
926 404731 ff_mpeg12_mbPatTable[cbp][1],
927 404731 ff_mpeg12_mbPatTable[cbp][0]);
928 } else {
929 75923 put_bits(&s->pb,
930 75923 ff_mpeg12_mbPatTable[cbp >> 2][1],
931 75923 ff_mpeg12_mbPatTable[cbp >> 2][0]);
932 75923 put_sbits(&s->pb, 2, cbp);
933 }
934 }
935 549253 s->f_count++;
936 } else {
937
2/2
✓ Branch 0 taken 486295 times.
✓ Branch 1 taken 95049 times.
581344 if (s->mv_type == MV_TYPE_16X16) {
938
2/2
✓ Branch 0 taken 314569 times.
✓ Branch 1 taken 171726 times.
486295 if (cbp) { // With coded bloc pattern
939
2/2
✓ Branch 0 taken 55606 times.
✓ Branch 1 taken 258963 times.
314569 if (s->dquant) {
940
2/2
✓ Branch 0 taken 12773 times.
✓ Branch 1 taken 42833 times.
55606 if (s->mv_dir == MV_DIR_FORWARD)
941 12773 put_mb_modes(s, 6, 3, 1, 0);
942 else
943 42833 put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0);
944 55606 put_qscale(s);
945 } else {
946 258963 put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0);
947 }
948 } else { // No coded bloc pattern
949 171726 put_bits(&s->pb, 5 - s->mv_dir, 2);
950
2/2
✓ Branch 0 taken 78149 times.
✓ Branch 1 taken 93577 times.
171726 if (!s->frame_pred_frame_dct)
951 78149 put_bits(&s->pb, 2, 2); /* motion_type: frame */
952 171726 s->qscale -= s->dquant;
953 }
954 486295 s->misc_bits += get_bits_diff(s);
955
2/2
✓ Branch 0 taken 347991 times.
✓ Branch 1 taken 138304 times.
486295 if (s->mv_dir & MV_DIR_FORWARD) {
956 347991 mpeg1_encode_motion(s,
957 347991 s->mv[0][0][0] - s->last_mv[0][0][0],
958 s->f_code);
959 347991 mpeg1_encode_motion(s,
960 347991 s->mv[0][0][1] - s->last_mv[0][0][1],
961 s->f_code);
962 347991 s->last_mv[0][0][0] =
963 347991 s->last_mv[0][1][0] = s->mv[0][0][0];
964 347991 s->last_mv[0][0][1] =
965 347991 s->last_mv[0][1][1] = s->mv[0][0][1];
966 347991 s->f_count++;
967 }
968
2/2
✓ Branch 0 taken 366304 times.
✓ Branch 1 taken 119991 times.
486295 if (s->mv_dir & MV_DIR_BACKWARD) {
969 366304 mpeg1_encode_motion(s,
970 366304 s->mv[1][0][0] - s->last_mv[1][0][0],
971 s->b_code);
972 366304 mpeg1_encode_motion(s,
973 366304 s->mv[1][0][1] - s->last_mv[1][0][1],
974 s->b_code);
975 366304 s->last_mv[1][0][0] =
976 366304 s->last_mv[1][1][0] = s->mv[1][0][0];
977 366304 s->last_mv[1][0][1] =
978 366304 s->last_mv[1][1][1] = s->mv[1][0][1];
979 366304 s->b_count++;
980 }
981 } else {
982 av_assert2(s->mv_type == MV_TYPE_FIELD);
983 av_assert2(!s->frame_pred_frame_dct);
984
2/2
✓ Branch 0 taken 74532 times.
✓ Branch 1 taken 20517 times.
95049 if (cbp) { // With coded bloc pattern
985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74532 times.
74532 if (s->dquant) {
986 if (s->mv_dir == MV_DIR_FORWARD)
987 put_mb_modes(s, 6, 3, 1, 1);
988 else
989 put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1);
990 put_qscale(s);
991 } else {
992 74532 put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1);
993 }
994 } else { // No coded bloc pattern
995 20517 put_bits(&s->pb, 5 - s->mv_dir, 2);
996 20517 put_bits(&s->pb, 2, 1); /* motion_type: field */
997 20517 s->qscale -= s->dquant;
998 }
999 95049 s->misc_bits += get_bits_diff(s);
1000
2/2
✓ Branch 0 taken 60873 times.
✓ Branch 1 taken 34176 times.
95049 if (s->mv_dir & MV_DIR_FORWARD) {
1001
2/2
✓ Branch 0 taken 121746 times.
✓ Branch 1 taken 60873 times.
182619 for (i = 0; i < 2; i++) {
1002 121746 put_bits(&s->pb, 1, s->field_select[0][i]);
1003 121746 mpeg1_encode_motion(s,
1004 121746 s->mv[0][i][0] - s->last_mv[0][i][0],
1005 s->f_code);
1006 121746 mpeg1_encode_motion(s,
1007 121746 s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
1008 s->f_code);
1009 121746 s->last_mv[0][i][0] = s->mv[0][i][0];
1010 121746 s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
1011 }
1012 60873 s->f_count++;
1013 }
1014
2/2
✓ Branch 0 taken 61175 times.
✓ Branch 1 taken 33874 times.
95049 if (s->mv_dir & MV_DIR_BACKWARD) {
1015
2/2
✓ Branch 0 taken 122350 times.
✓ Branch 1 taken 61175 times.
183525 for (i = 0; i < 2; i++) {
1016 122350 put_bits(&s->pb, 1, s->field_select[1][i]);
1017 122350 mpeg1_encode_motion(s,
1018 122350 s->mv[1][i][0] - s->last_mv[1][i][0],
1019 s->b_code);
1020 122350 mpeg1_encode_motion(s,
1021 122350 s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1),
1022 s->b_code);
1023 122350 s->last_mv[1][i][0] = s->mv[1][i][0];
1024 122350 s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
1025 }
1026 61175 s->b_count++;
1027 }
1028 }
1029 581344 s->mv_bits += get_bits_diff(s);
1030
2/2
✓ Branch 0 taken 389101 times.
✓ Branch 1 taken 192243 times.
581344 if (cbp) {
1031
2/2
✓ Branch 0 taken 206508 times.
✓ Branch 1 taken 182593 times.
389101 if (chroma_y_shift) {
1032 206508 put_bits(&s->pb,
1033 206508 ff_mpeg12_mbPatTable[cbp][1],
1034 206508 ff_mpeg12_mbPatTable[cbp][0]);
1035 } else {
1036 182593 put_bits(&s->pb,
1037 182593 ff_mpeg12_mbPatTable[cbp >> 2][1],
1038 182593 ff_mpeg12_mbPatTable[cbp >> 2][0]);
1039 182593 put_sbits(&s->pb, 2, cbp);
1040 }
1041 }
1042 }
1043
2/2
✓ Branch 0 taken 10706096 times.
✓ Branch 1 taken 1570182 times.
12276278 for (i = 0; i < mb_block_count; i++)
1044
2/2
✓ Branch 0 taken 6457398 times.
✓ Branch 1 taken 4248698 times.
10706096 if (cbp & (1 << (mb_block_count - 1 - i)))
1045 6457398 mpeg1_encode_block(s, block[i], i);
1046 1570182 s->mb_skip_run = 0;
1047
2/2
✓ Branch 0 taken 439585 times.
✓ Branch 1 taken 1130597 times.
1570182 if (s->mb_intra)
1048 439585 s->i_tex_bits += get_bits_diff(s);
1049 else
1050 1130597 s->p_tex_bits += get_bits_diff(s);
1051 }
1052 1657917 }
1053
1054 1657917 void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64],
1055 int motion_x, int motion_y)
1056 {
1057
2/2
✓ Branch 0 taken 1014802 times.
✓ Branch 1 taken 643115 times.
1657917 if (s->chroma_format == CHROMA_420)
1058 1014802 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6, 1);
1059 else
1060 643115 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8, 0);
1061 1657917 }
1062
1063 49 static av_cold void mpeg12_encode_init_static(void)
1064 {
1065 static uint8_t mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
1066
1067 49 ff_rl_init(&ff_rl_mpeg1, mpeg12_static_rl_table_store[0]);
1068 49 ff_rl_init(&ff_rl_mpeg2, mpeg12_static_rl_table_store[1]);
1069
1070 49 ff_mpeg1_init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
1071 49 ff_mpeg1_init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
1072
1073 /* build unified dc encoding tables */
1074
2/2
✓ Branch 0 taken 25039 times.
✓ Branch 1 taken 49 times.
25088 for (int i = -255; i < 256; i++) {
1075 int adiff, index;
1076 int bits, code;
1077 25039 int diff = i;
1078
1079 25039 adiff = FFABS(diff);
1080
2/2
✓ Branch 0 taken 12495 times.
✓ Branch 1 taken 12544 times.
25039 if (diff < 0)
1081 12495 diff--;
1082 25039 index = av_log2(2 * adiff);
1083
1084 25039 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
1085 25039 code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
1086 25039 av_mod_uintp2(diff, index);
1087 25039 mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
1088
1089 25039 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
1090 25039 code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
1091 25039 av_mod_uintp2(diff, index);
1092 25039 mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
1093 }
1094
1095
2/2
✓ Branch 0 taken 343 times.
✓ Branch 1 taken 49 times.
392 for (int f_code = 1; f_code <= MAX_FCODE; f_code++)
1096
2/2
✓ Branch 0 taken 5620055 times.
✓ Branch 1 taken 343 times.
5620398 for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) {
1097 int len;
1098
1099
2/2
✓ Branch 0 taken 343 times.
✓ Branch 1 taken 5619712 times.
5620055 if (mv == 0) {
1100 343 len = 1; /* ff_mpeg12_mbMotionVectorTable[0][1] */
1101 } else {
1102 int val, bit_size, code;
1103
1104 5619712 bit_size = f_code - 1;
1105
1106 5619712 val = mv;
1107
2/2
✓ Branch 0 taken 2809856 times.
✓ Branch 1 taken 2809856 times.
5619712 if (val < 0)
1108 2809856 val = -val;
1109 5619712 val--;
1110 5619712 code = (val >> bit_size) + 1;
1111
2/2
✓ Branch 0 taken 199136 times.
✓ Branch 1 taken 5420576 times.
5619712 if (code < 17)
1112 199136 len = ff_mpeg12_mbMotionVectorTable[code][1] +
1113 1 + bit_size;
1114 else
1115 5420576 len = 10 /* ff_mpeg12_mbMotionVectorTable[16][1] */ +
1116 2 + bit_size;
1117 }
1118
1119 5620055 mv_penalty[f_code][mv + MAX_DMV] = len;
1120 }
1121
1122
1123
2/2
✓ Branch 0 taken 343 times.
✓ Branch 1 taken 49 times.
392 for (int f_code = MAX_FCODE; f_code > 0; f_code--)
1124
2/2
✓ Branch 0 taken 199136 times.
✓ Branch 1 taken 343 times.
199479 for (int mv = -(8 << f_code); mv < (8 << f_code); mv++)
1125 199136 fcode_tab[mv + MAX_MV] = f_code;
1126 49 }
1127
1128 49 av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
1129 {
1130 static AVOnce init_static_once = AV_ONCE_INIT;
1131
1132 49 ff_mpeg12_common_init(s);
1133
1134 49 s->me.mv_penalty = mv_penalty;
1135 49 s->fcode_tab = fcode_tab;
1136
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 38 times.
49 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1137 11 s->min_qcoeff = -255;
1138 11 s->max_qcoeff = 255;
1139 } else {
1140 38 s->min_qcoeff = -2047;
1141 38 s->max_qcoeff = 2047;
1142 }
1143
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 36 times.
49 if (s->intra_vlc_format) {
1144 13 s->intra_ac_vlc_length =
1145 13 s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len;
1146 } else {
1147 36 s->intra_ac_vlc_length =
1148 36 s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1149 }
1150 49 s->inter_ac_vlc_length =
1151 49 s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1152
1153 49 ff_thread_once(&init_static_once, mpeg12_encode_init_static);
1154 49 }
1155
1156 #define OFFSET(x) offsetof(MPEG12EncContext, x)
1157 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1158 #define COMMON_OPTS \
1159 { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format. Overrides timecode_frame_start.", \
1160 OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE },\
1161 { "drop_frame_timecode", "Timecode is in drop frame format.", \
1162 OFFSET(drop_frame_timecode), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
1163 { "scan_offset", "Reserve space for SVCD scan offset user data.", \
1164 OFFSET(scan_offset), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
1165 { "timecode_frame_start", "GOP timecode frame start number, in non-drop-frame format", \
1166 OFFSET(timecode_frame_start), AV_OPT_TYPE_INT64, {.i64 = -1 }, -1, INT64_MAX, VE}, \
1167 FF_MPV_COMMON_BFRAME_OPTS
1168
1169 static const AVOption mpeg1_options[] = {
1170 COMMON_OPTS
1171 FF_MPV_COMMON_OPTS
1172 #if FF_API_MPEGVIDEO_OPTS
1173 FF_MPV_DEPRECATED_MPEG_QUANT_OPT
1174 FF_MPV_DEPRECATED_A53_CC_OPT
1175 FF_MPV_DEPRECATED_MATRIX_OPT
1176 #endif
1177 { NULL },
1178 };
1179
1180 static const AVOption mpeg2_options[] = {
1181 COMMON_OPTS
1182 { "intra_vlc", "Use MPEG-2 intra VLC table.",
1183 FF_MPV_OFFSET(intra_vlc_format), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1184 { "non_linear_quant", "Use nonlinear quantizer.", FF_MPV_OFFSET(q_scale_type), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1185 { "alternate_scan", "Enable alternate scantable.", FF_MPV_OFFSET(alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1186 { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE },
1187 { "seq_disp_ext", "Write sequence_display_extension blocks.", OFFSET(seq_disp_ext), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, "seq_disp_ext" },
1188 { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, VE, "seq_disp_ext" },
1189 { "never", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, VE, "seq_disp_ext" },
1190 { "always", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, VE, "seq_disp_ext" },
1191 { "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, "video_format" },
1192 { "component", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_COMPONENT }, 0, 0, VE, "video_format" },
1193 { "pal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_PAL }, 0, 0, VE, "video_format" },
1194 { "ntsc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_NTSC }, 0, 0, VE, "video_format" },
1195 { "secam", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_SECAM }, 0, 0, VE, "video_format" },
1196 { "mac", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_MAC }, 0, 0, VE, "video_format" },
1197 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_UNSPECIFIED}, 0, 0, VE, "video_format" },
1198 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, { .i64 = value }, 0, 0, VE, "avctx.level"
1199 { LEVEL("high", 4) },
1200 { LEVEL("high1440", 6) },
1201 { LEVEL("main", 8) },
1202 { LEVEL("low", 10) },
1203 #undef LEVEL
1204 FF_MPV_COMMON_OPTS
1205 #if FF_API_MPEGVIDEO_OPTS
1206 { "mpeg_quant", "Deprecated, does nothing", FF_MPV_OFFSET(mpeg_quant),
1207 AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, VE | AV_OPT_FLAG_DEPRECATED },
1208 FF_MPV_DEPRECATED_MATRIX_OPT
1209 #endif
1210 FF_MPEG2_PROFILE_OPTS
1211 { NULL },
1212 };
1213
1214 #define mpeg12_class(x) \
1215 static const AVClass mpeg ## x ## _class = { \
1216 .class_name = "mpeg" # x "video encoder", \
1217 .item_name = av_default_item_name, \
1218 .option = mpeg ## x ## _options, \
1219 .version = LIBAVUTIL_VERSION_INT, \
1220 };
1221
1222 mpeg12_class(1)
1223 mpeg12_class(2)
1224
1225 const AVCodec ff_mpeg1video_encoder = {
1226 .name = "mpeg1video",
1227 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
1228 .type = AVMEDIA_TYPE_VIDEO,
1229 .id = AV_CODEC_ID_MPEG1VIDEO,
1230 .priv_data_size = sizeof(MPEG12EncContext),
1231 .init = encode_init,
1232 .encode2 = ff_mpv_encode_picture,
1233 .close = ff_mpv_encode_end,
1234 .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
1235 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1236 AV_PIX_FMT_NONE },
1237 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
1238 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1239 .priv_class = &mpeg1_class,
1240 };
1241
1242 const AVCodec ff_mpeg2video_encoder = {
1243 .name = "mpeg2video",
1244 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
1245 .type = AVMEDIA_TYPE_VIDEO,
1246 .id = AV_CODEC_ID_MPEG2VIDEO,
1247 .priv_data_size = sizeof(MPEG12EncContext),
1248 .init = encode_init,
1249 .encode2 = ff_mpv_encode_picture,
1250 .close = ff_mpv_encode_end,
1251 .supported_framerates = ff_mpeg2_frame_rate_tab,
1252 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1253 AV_PIX_FMT_YUV422P,
1254 AV_PIX_FMT_NONE },
1255 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
1256 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1257 .priv_class = &mpeg2_class,
1258 };
1259 #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */
1260