FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12enc.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 618 684 90.4%
Functions: 17 17 100.0%
Branches: 342 417 82.0%

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