FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12enc.c
Date: 2026-10-06 00:26:59
Exec Total Coverage
Lines: 639 718 89.0%
Functions: 17 17 100.0%
Branches: 359 441 81.4%

Line Branch Exec Source
1 /*
2 * MPEG-1/2 encoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * MPEG-1/2 encoder
26 */
27
28 #include <assert.h>
29 #include <stdint.h>
30
31 #include "config.h"
32 #include "config_components.h"
33 #include "libavutil/attributes.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/log.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/thread.h"
38 #include "libavutil/timecode.h"
39 #include "libavutil/stereo3d.h"
40
41 #include "avcodec.h"
42 #include "codec_internal.h"
43 #include "mathops.h"
44 #include "mpeg12.h"
45 #include "mpeg12data.h"
46 #include "mpeg12enc.h"
47 #include "mpeg12vlc.h"
48 #include "mpegutils.h"
49 #include "mpegvideo.h"
50 #include "mpegvideodata.h"
51 #include "mpegvideoenc.h"
52 #include "profiles.h"
53 #include "put_bits.h"
54 #include "rl.h"
55
56 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
57 static const uint8_t svcd_scan_offset_placeholder[] = {
58 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
59 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 };
61
62 static uint8_t mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1];
63 static uint8_t fcode_tab[MAX_MV * 2 + 1];
64
65 static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2];
66 static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2];
67
68 static uint8_t mpeg12_max_level[MAX_LEVEL + 1];
69 static uint8_t mpeg12_index_run[MAX_RUN + 1];
70
71 /* simple include everything table for dc, first byte is bits
72 * number next 3 are code */
73 static uint32_t mpeg1_lum_dc_uni[512];
74 static uint32_t mpeg1_chr_dc_uni[512];
75
76 typedef struct MPEG12EncContext {
77 MPVMainEncContext mpeg;
78 AVRational frame_rate_ext;
79 unsigned frame_rate_index;
80
81 int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num
82
83 int64_t timecode_frame_start; ///< GOP timecode frame start number, in non drop frame format
84 AVTimecode tc; ///< timecode context
85 char *tc_opt_str; ///< timecode option string
86 int drop_frame_timecode; ///< timecode is in drop frame format.
87 int scan_offset; ///< reserve space for SVCD scan offset user data.
88
89 int a53_cc;
90 int seq_disp_ext;
91 int video_format;
92 #define VIDEO_FORMAT_COMPONENT 0
93 #define VIDEO_FORMAT_PAL 1
94 #define VIDEO_FORMAT_NTSC 2
95 #define VIDEO_FORMAT_SECAM 3
96 #define VIDEO_FORMAT_MAC 4
97 #define VIDEO_FORMAT_UNSPECIFIED 5
98 } MPEG12EncContext;
99
100 #define A53_MAX_CC_COUNT 0x1f
101 #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */
102
103 165 av_cold void ff_mpeg1_init_uni_ac_vlc(const int8_t max_level[],
104 const uint8_t index_run[],
105 const uint16_t table_vlc[][2],
106 uint8_t uni_ac_vlc_len[])
107 {
108 int i;
109
110
2/2
✓ Branch 0 taken 21120 times.
✓ Branch 1 taken 165 times.
21285 for (i = 0; i < 128; i++) {
111 21120 int level = i - 64;
112 int run;
113
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 20955 times.
21120 if (!level)
114 165 continue;
115
2/2
✓ Branch 0 taken 1341120 times.
✓ Branch 1 taken 20955 times.
1362075 for (run = 0; run < 64; run++) {
116 int len, code;
117 1341120 int alevel = FFABS(level);
118
119
2/2
✓ Branch 0 taken 1304310 times.
✓ Branch 1 taken 36810 times.
1341120 if (alevel > max_level[run])
120 1304310 code = 111; /* rl->n */
121 else
122 36810 code = index_run[run] + alevel - 1;
123
124
2/2
✓ Branch 0 taken 36630 times.
✓ Branch 1 taken 1304490 times.
1341120 if (code < 111) { /* rl->n */
125 /* length of VLC and sign */
126 36630 len = table_vlc[code][1] + 1;
127 } else {
128 1304490 len = table_vlc[MPEG12_RL_NB_ELEMS][1] + 6;
129
130
1/2
✓ Branch 0 taken 1304490 times.
✗ Branch 1 not taken.
1304490 if (alevel < 128)
131 1304490 len += 8;
132 else
133 ✗ len += 16;
134 }
135
136 1341120 uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
137 }
138 }
139 165 }
140
141 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
142 329449 static void put_header(MPVEncContext *const s, uint32_t header)
143 {
144 329449 align_put_bits(&s->pb);
145 329449 put_bits32(&s->pb, header);
146 329449 }
147
148 /* put sequence header if needed */
149 5329 static void mpeg1_encode_sequence_header(MPEG12EncContext *mpeg12)
150 {
151 5329 MPVEncContext *const s = &mpeg12->mpeg.s;
152 unsigned int vbv_buffer_size, fps, v;
153 int constraint_parameter_flag;
154 5329 AVRational framerate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index];
155 uint64_t time_code;
156 5329 int64_t best_aspect_error = INT64_MAX;
157 5329 AVRational aspect_ratio = s->c.avctx->sample_aspect_ratio;
158 int aspect_ratio_info;
159
160 5329 put_bits_assume_flushed(&s->pb);
161
162
2/2
✓ Branch 0 taken 4844 times.
✓ Branch 1 taken 485 times.
5329 if (!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY))
163 4844 return;
164
165
3/4
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 411 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 74 times.
485 if (aspect_ratio.num == 0 || aspect_ratio.den == 0)
166 411 aspect_ratio = (AVRational){1,1}; // pixel aspect 1.1 (VGA)
167
168 /* MPEG-1 header repeated every GOP */
169 485 put_header(s, SEQ_START_CODE);
170
171 485 put_sbits(&s->pb, 12, s->c.width & 0xFFF);
172 485 put_sbits(&s->pb, 12, s->c.height & 0xFFF);
173
174
2/2
✓ Branch 0 taken 6790 times.
✓ Branch 1 taken 485 times.
7275 for (int i = 1; i < 15; i++) {
175 6790 int64_t error = aspect_ratio.num * (1LL<<32) / aspect_ratio.den;
176
4/4
✓ Branch 0 taken 6048 times.
✓ Branch 1 taken 742 times.
✓ Branch 2 taken 432 times.
✓ Branch 3 taken 5616 times.
6790 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
177 1174 error -= (1LL<<32) / ff_mpeg1_aspect[i];
178 else
179 5616 error -= (1LL<<32)*ff_mpeg2_aspect[i].num * s->c.height / s->c.width / ff_mpeg2_aspect[i].den;
180
181 6790 error = FFABS(error);
182
183
2/2
✓ Branch 0 taken 543 times.
✓ Branch 1 taken 6247 times.
6790 if (error - 2 <= best_aspect_error) {
184 543 best_aspect_error = error;
185 543 aspect_ratio_info = i;
186 }
187 }
188
189 485 put_bits(&s->pb, 4, aspect_ratio_info);
190 485 put_bits(&s->pb, 4, mpeg12->frame_rate_index);
191
192
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 332 times.
485 if (s->c.avctx->rc_max_rate) {
193 153 v = (s->c.avctx->rc_max_rate + 399) / 400;
194
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
153 if (v > 0x3ffff && s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
195 ✗ v = 0x3ffff;
196 } else {
197 332 v = 0x3FFFF;
198 }
199
200
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 332 times.
485 if (s->c.avctx->rc_buffer_size)
201 153 vbv_buffer_size = s->c.avctx->rc_buffer_size;
202 else
203 /* VBV calculation: Scaled so that a VCD has the proper
204 * VBV size of 40 kilobytes */
205 332 vbv_buffer_size = av_rescale_rnd(mpeg12->mpeg.bit_rate, 20, 1151929 / 2, AV_ROUND_ZERO) * 8 * 1024;
206 485 vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
207
208 485 put_sbits(&s->pb, 18, v);
209 485 put_bits(&s->pb, 1, 1); // marker
210 485 put_sbits(&s->pb, 10, vbv_buffer_size);
211
212 485 constraint_parameter_flag =
213 967 s->c.width <= 768 &&
214
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 150 times.
482 s->c.height <= 576 &&
215
2/2
✓ Branch 0 taken 299 times.
✓ Branch 1 taken 33 times.
332 s->c.mb_width * s->c.mb_height <= 396 &&
216
2/2
✓ Branch 0 taken 293 times.
✓ Branch 1 taken 6 times.
299 s->c.mb_width * s->c.mb_height * framerate.num <= 396 * 25 * framerate.den &&
217
1/2
✓ Branch 0 taken 293 times.
✗ Branch 1 not taken.
293 framerate.num <= framerate.den * 30 &&
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293 times.
293 s->c.avctx->me_range &&
219 ✗ s->c.avctx->me_range < 128 &&
220 ✗ vbv_buffer_size <= 20 &&
221
2/2
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 3 times.
967 v <= 1856000 / 400 &&
222 ✗ s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO;
223
224 485 put_bits(&s->pb, 1, constraint_parameter_flag);
225
226 485 ff_write_quant_matrix(&s->pb, s->c.avctx->intra_matrix);
227 485 ff_write_quant_matrix(&s->pb, s->c.avctx->inter_matrix);
228
229
2/2
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 53 times.
485 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
230 const AVFrameSideData *side_data;
231 432 int width = s->c.width;
232 432 int height = s->c.height;
233 int use_seq_disp_ext;
234
235 432 put_header(s, EXT_START_CODE);
236 432 put_bits(&s->pb, 4, 1); // seq ext
237
238 432 put_bits(&s->pb, 1, s->c.avctx->profile == AV_PROFILE_MPEG2_422); // escx 1 for 4:2:2 profile
239
240 432 put_bits(&s->pb, 3, s->c.avctx->profile); // profile
241 432 put_bits(&s->pb, 4, s->c.avctx->level); // level
242
243 432 put_bits(&s->pb, 1, s->c.progressive_sequence);
244 432 put_bits(&s->pb, 2, s->c.chroma_format);
245 432 put_bits(&s->pb, 2, s->c.width >> 12);
246 432 put_bits(&s->pb, 2, s->c.height >> 12);
247 432 put_bits(&s->pb, 12, v >> 18); // bitrate ext
248 432 put_bits(&s->pb, 1, 1); // marker
249 432 put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
250 432 put_bits(&s->pb, 1, s->c.low_delay);
251 432 put_bits(&s->pb, 2, mpeg12->frame_rate_ext.num-1); // frame_rate_ext_n
252 432 put_bits(&s->pb, 5, mpeg12->frame_rate_ext.den-1); // frame_rate_ext_d
253
254 432 side_data = av_frame_get_side_data(s->c.cur_pic.ptr->f, AV_FRAME_DATA_PANSCAN);
255
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 427 times.
432 if (side_data) {
256 5 const AVPanScan *pan_scan = (AVPanScan *)side_data->data;
257
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) {
258 5 width = pan_scan->width >> 4;
259 5 height = pan_scan->height >> 4;
260 }
261 }
262
263 1296 use_seq_disp_ext = (width != s->c.width ||
264
1/2
✓ Branch 0 taken 432 times.
✗ Branch 1 not taken.
432 height != s->c.height ||
265
2/2
✓ Branch 0 taken 413 times.
✓ Branch 1 taken 19 times.
432 s->c.avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
266
2/2
✓ Branch 0 taken 410 times.
✓ Branch 1 taken 3 times.
413 s->c.avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
267
3/4
✓ Branch 0 taken 432 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 384 times.
✓ Branch 3 taken 26 times.
1248 s->c.avctx->colorspace != AVCOL_SPC_UNSPECIFIED ||
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384 times.
384 mpeg12->video_format != VIDEO_FORMAT_UNSPECIFIED);
269
270
1/2
✓ Branch 0 taken 432 times.
✗ Branch 1 not taken.
432 if (mpeg12->seq_disp_ext == 1 ||
271
3/4
✓ Branch 0 taken 432 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 384 times.
432 (mpeg12->seq_disp_ext == -1 && use_seq_disp_ext)) {
272 48 put_header(s, EXT_START_CODE);
273 48 put_bits(&s->pb, 4, 2); // sequence display extension
274 48 put_bits(&s->pb, 3, mpeg12->video_format); // video_format
275 48 put_bits(&s->pb, 1, 1); // colour_description
276 48 put_bits(&s->pb, 8, s->c.avctx->color_primaries); // colour_primaries
277 48 put_bits(&s->pb, 8, s->c.avctx->color_trc); // transfer_characteristics
278 48 put_bits(&s->pb, 8, s->c.avctx->colorspace); // matrix_coefficients
279 48 put_bits(&s->pb, 14, width); // display_horizontal_size
280 48 put_bits(&s->pb, 1, 1); // marker_bit
281 48 put_bits(&s->pb, 14, height); // display_vertical_size
282 48 put_bits(&s->pb, 3, 0); // remaining 3 bits are zero padding
283 }
284 }
285
286 485 put_header(s, GOP_START_CODE);
287 485 put_bits(&s->pb, 1, mpeg12->drop_frame_timecode); // drop frame flag
288 /* time code: we must convert from the real frame rate to a
289 * fake MPEG frame rate in case of low frame rate */
290 485 fps = (framerate.num + framerate.den / 2) / framerate.den;
291 485 time_code = s->c.cur_pic.ptr->coded_picture_number +
292 485 mpeg12->timecode_frame_start;
293
294 485 mpeg12->gop_picture_number = s->c.cur_pic.ptr->coded_picture_number;
295
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 485 times.
485 av_assert0(mpeg12->drop_frame_timecode == !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
297
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 476 times.
485 if (mpeg12->drop_frame_timecode)
298 9 time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
299
300 485 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
301 485 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
302 485 put_bits(&s->pb, 1, 1);
303 485 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
304 485 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
305 485 put_bits(&s->pb, 1, !!(s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) ||
306
5/6
✓ Branch 0 taken 485 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 311 times.
✓ Branch 3 taken 174 times.
✓ Branch 4 taken 69 times.
✓ Branch 5 taken 242 times.
485 mpeg12->mpeg.intra_only || !mpeg12->gop_picture_number);
307 485 put_bits(&s->pb, 1, 0); // broken link
308 }
309
310 1850691 static inline void encode_mb_skip_run(MPVEncContext *const s, int run)
311 {
312
2/2
✓ Branch 0 taken 69137 times.
✓ Branch 1 taken 1850691 times.
1919828 while (run >= 33) {
313 69137 put_bits(&s->pb, 11, 0x008);
314 69137 run -= 33;
315 }
316 1850691 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
317 1850691 ff_mpeg12_mbAddrIncrTable[run][0]);
318 1850691 }
319
320 501040 static av_always_inline void put_qscale(MPVEncContext *const s)
321 {
322 501040 put_bits(&s->pb, 5, s->c.qscale);
323 501040 }
324
325 317687 void ff_mpeg1_encode_slice_header(MPVEncContext *const s)
326 {
327
3/4
✓ Branch 0 taken 310414 times.
✓ Branch 1 taken 7273 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 310414 times.
317687 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO && s->c.height > 2800) {
328 ✗ put_header(s, SLICE_MIN_START_CODE + (s->c.mb_y & 127));
329 /* slice_vertical_position_extension */
330 ✗ put_bits(&s->pb, 3, s->c.mb_y >> 7);
331 } else {
332 av_assert1(s->c.mb_y <= SLICE_MAX_START_CODE - SLICE_MIN_START_CODE);
333 317687 put_header(s, SLICE_MIN_START_CODE + s->c.mb_y);
334 }
335 317687 put_qscale(s);
336 /* slice extra information */
337 317687 put_bits(&s->pb, 1, 0);
338 317687 }
339
340 5329 static int mpeg1_encode_picture_header(MPVMainEncContext *const m)
341 {
342 5329 MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)m;
343 5329 MPVEncContext *const s = &m->s;
344 const AVFrameSideData *side_data;
345
346 5329 put_bits_assume_flushed(&s->pb);
347
348 5329 mpeg1_encode_sequence_header(mpeg12);
349
350 /* MPEG-1 picture header */
351 5329 put_header(s, PICTURE_START_CODE);
352 /* temporal reference */
353
354 5329 put_bits(&s->pb, 10,
355 5329 (s->picture_number - mpeg12->gop_picture_number) & 0x3ff);
356 5329 put_bits(&s->pb, 3, s->c.pict_type);
357
358 5329 m->vbv_delay_pos = put_bytes_count(&s->pb, 0);
359 5329 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
360
361 // RAL: Forward f_code also needed for B-frames
362
2/2
✓ Branch 0 taken 1231 times.
✓ Branch 1 taken 4098 times.
5329 if (s->c.pict_type == AV_PICTURE_TYPE_P ||
363
2/2
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 485 times.
1231 s->c.pict_type == AV_PICTURE_TYPE_B) {
364 4844 put_bits(&s->pb, 1, 0); /* half pel coordinates */
365
2/2
✓ Branch 0 taken 427 times.
✓ Branch 1 taken 4417 times.
4844 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
366 427 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
367 else
368 4417 put_bits(&s->pb, 3, 7); /* forward_f_code */
369 }
370
371 // RAL: Backward f_code necessary for B-frames
372
2/2
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 4583 times.
5329 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
373 746 put_bits(&s->pb, 1, 0); /* half pel coordinates */
374
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 602 times.
746 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
375 144 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
376 else
377 602 put_bits(&s->pb, 3, 7); /* backward_f_code */
378 }
379
380 5329 put_bits(&s->pb, 1, 0); /* extra bit picture */
381
382 5329 s->c.frame_pred_frame_dct = 1;
383
2/2
✓ Branch 0 taken 4849 times.
✓ Branch 1 taken 480 times.
5329 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
384 4849 put_header(s, EXT_START_CODE);
385 4849 put_bits(&s->pb, 4, 8); /* pic ext */
386
2/2
✓ Branch 0 taken 1034 times.
✓ Branch 1 taken 3815 times.
4849 if (s->c.pict_type == AV_PICTURE_TYPE_P ||
387
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 432 times.
1034 s->c.pict_type == AV_PICTURE_TYPE_B) {
388 4417 put_bits(&s->pb, 4, s->f_code);
389 4417 put_bits(&s->pb, 4, s->f_code);
390 } else {
391 432 put_bits(&s->pb, 8, 255);
392 }
393
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 4247 times.
4849 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
394 602 put_bits(&s->pb, 4, s->b_code);
395 602 put_bits(&s->pb, 4, s->b_code);
396 } else {
397 4247 put_bits(&s->pb, 8, 255);
398 }
399 4849 put_bits(&s->pb, 2, s->c.intra_dc_precision);
400
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4849 times.
4849 av_assert0(s->c.picture_structure == PICT_FRAME);
402 4849 put_bits(&s->pb, 2, s->c.picture_structure);
403
2/2
✓ Branch 0 taken 3699 times.
✓ Branch 1 taken 1150 times.
4849 if (s->c.progressive_sequence)
404 3699 put_bits(&s->pb, 1, 0); /* no repeat */
405 else
406 1150 put_bits(&s->pb, 1, !!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST));
407 /* XXX: optimize the generation of this flag with entropy measures */
408 4849 s->c.frame_pred_frame_dct = s->c.progressive_sequence;
409
410 4849 put_bits(&s->pb, 1, s->c.frame_pred_frame_dct);
411 4849 put_bits(&s->pb, 1, s->c.concealment_motion_vectors);
412 4849 put_bits(&s->pb, 1, s->c.q_scale_type);
413 4849 put_bits(&s->pb, 1, s->c.intra_vlc_format);
414 4849 put_bits(&s->pb, 1, s->c.alternate_scan);
415 4849 put_bits(&s->pb, 1, s->c.repeat_first_field);
416 4849 s->c.progressive_frame = s->c.progressive_sequence;
417 /* chroma_420_type */
418
2/2
✓ Branch 0 taken 4499 times.
✓ Branch 1 taken 350 times.
9348 put_bits(&s->pb, 1, s->c.chroma_format ==
419 4499 CHROMA_420 ? s->c.progressive_frame : 0);
420 4849 put_bits(&s->pb, 1, s->c.progressive_frame);
421 4849 put_bits(&s->pb, 1, 0); /* composite_display_flag */
422 }
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5329 times.
5329 if (mpeg12->scan_offset) {
424 int i;
425
426 ✗ put_header(s, USER_START_CODE);
427 ✗ for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
428 ✗ put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
429 }
430 5329 side_data = av_frame_get_side_data(s->c.cur_pic.ptr->f,
431 AV_FRAME_DATA_STEREO3D);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5329 times.
5329 if (side_data) {
433 ✗ const AVStereo3D *stereo = (AVStereo3D *)side_data->data;
434 uint8_t fpa_type;
435
436 ✗ switch (stereo->type) {
437 ✗ case AV_STEREO3D_SIDEBYSIDE:
438 ✗ fpa_type = 0x03;
439 ✗ break;
440 ✗ case AV_STEREO3D_TOPBOTTOM:
441 ✗ fpa_type = 0x04;
442 ✗ break;
443 ✗ case AV_STEREO3D_2D:
444 ✗ fpa_type = 0x08;
445 ✗ break;
446 ✗ case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
447 ✗ fpa_type = 0x23;
448 ✗ break;
449 ✗ default:
450 ✗ fpa_type = 0;
451 ✗ break;
452 }
453
454 ✗ if (fpa_type != 0) {
455 ✗ put_header(s, USER_START_CODE);
456 // S3D_video_format_signaling_identifier
457 ✗ put_bits32(&s->pb, MKBETAG('J','P','3','D'));
458 ✗ put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
459
460 ✗ put_bits(&s->pb, 1, 1); // reserved_bit
461 ✗ put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
462 ✗ put_bits(&s->pb, 16, 0x04FF); // reserved_data
463 }
464 }
465
466
2/2
✓ Branch 0 taken 4849 times.
✓ Branch 1 taken 480 times.
5329 if (CONFIG_MPEG2VIDEO_ENCODER && mpeg12->a53_cc) {
467 4849 side_data = av_frame_get_side_data(s->c.cur_pic.ptr->f,
468 AV_FRAME_DATA_A53_CC);
469
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 4714 times.
4849 if (side_data) {
470
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) {
471 134 put_header (s, USER_START_CODE);
472
473 134 put_bits32(&s->pb, MKBETAG('G','A','9','4')); // user_identifier
474 134 put_bits(&s->pb, 8, 3); // user_data_type_code
475 134 put_bits(&s->pb, 8,
476 134 (side_data->size / 3 & A53_MAX_CC_COUNT) | 0x40); // flags, cc_count
477 134 put_bits(&s->pb, 8, 0xff); // em_data
478
479
2/2
✓ Branch 0 taken 8040 times.
✓ Branch 1 taken 134 times.
8174 for (int i = 0; i < side_data->size; i++)
480 8040 put_bits(&s->pb, 8, side_data->data[i]);
481
482 134 put_bits(&s->pb, 8, 0xff); // marker_bits
483 } else {
484 1 av_log(s->c.avctx, AV_LOG_WARNING,
485 "Closed Caption size (%zu) can not exceed "
486 1 "93 bytes and must be a multiple of 3\n", side_data->size);
487 }
488 }
489 }
490
491 5329 s->c.mb_y = 0;
492 5329 ff_mpeg1_encode_slice_header(s);
493
494 5329 return 0;
495 }
496
497 1494303 static inline void put_mb_modes(MPVEncContext *const s, int n, int bits,
498 int has_mv, int field_motion)
499 {
500 1494303 put_bits(&s->pb, n, bits);
501
2/2
✓ Branch 0 taken 751644 times.
✓ Branch 1 taken 742659 times.
1494303 if (!s->c.frame_pred_frame_dct) {
502
2/2
✓ Branch 0 taken 407748 times.
✓ Branch 1 taken 343896 times.
751644 if (has_mv)
503 /* motion_type: frame/field */
504 407748 put_bits(&s->pb, 2, 2 - field_motion);
505 751644 put_bits(&s->pb, 1, s->c.interlaced_dct);
506 }
507 1494303 }
508
509 // RAL: Parameter added: f_or_b_code
510 3367282 static void mpeg1_encode_motion(MPVEncContext *const s, int val, int f_or_b_code)
511 {
512
2/2
✓ Branch 0 taken 1387781 times.
✓ Branch 1 taken 1979501 times.
3367282 if (val == 0) {
513 /* zero vector, corresponds to ff_mpeg12_mbMotionVectorTable[0] */
514 1387781 put_bits(&s->pb, 1, 0x01);
515 } else {
516 int code, sign, bits;
517 1979501 int bit_size = f_or_b_code - 1;
518 1979501 int range = 1 << bit_size;
519 /* modulo encoding */
520 1979501 val = sign_extend(val, 5 + bit_size);
521
522
2/2
✓ Branch 0 taken 954552 times.
✓ Branch 1 taken 1024949 times.
1979501 if (val >= 0) {
523 954552 val--;
524 954552 code = (val >> bit_size) + 1;
525 954552 bits = val & (range - 1);
526 954552 sign = 0;
527 } else {
528 1024949 val = -val;
529 1024949 val--;
530 1024949 code = (val >> bit_size) + 1;
531 1024949 bits = val & (range - 1);
532 1024949 sign = 1;
533 }
534
535 av_assert2(code > 0 && code <= 16);
536
537 1979501 put_bits(&s->pb,
538 1979501 ff_mpeg12_mbMotionVectorTable[code][1],
539 1979501 ff_mpeg12_mbMotionVectorTable[code][0]);
540
541 1979501 put_bits(&s->pb, 1, sign);
542
2/2
✓ Branch 0 taken 1417381 times.
✓ Branch 1 taken 562120 times.
1979501 if (bit_size > 0)
543 1417381 put_bits(&s->pb, bit_size, bits);
544 }
545 3367282 }
546
547 3523690 static inline void encode_dc(MPVEncContext *const s, int diff, int component)
548 {
549 3523690 unsigned int diff_u = diff + 255;
550
2/2
✓ Branch 0 taken 163407 times.
✓ Branch 1 taken 3360283 times.
3523690 if (diff_u >= 511) {
551 int index;
552
553
2/2
✓ Branch 0 taken 113309 times.
✓ Branch 1 taken 50098 times.
163407 if (diff < 0) {
554 113309 index = av_log2_16bit(-2 * diff);
555 113309 diff--;
556 } else {
557 50098 index = av_log2_16bit(2 * diff);
558 }
559
2/2
✓ Branch 0 taken 93845 times.
✓ Branch 1 taken 69562 times.
163407 if (component == 0)
560 93845 put_bits(&s->pb,
561 93845 ff_mpeg12_vlc_dc_lum_bits[index] + index,
562 93845 (ff_mpeg12_vlc_dc_lum_code[index] << index) +
563 93845 av_zero_extend(diff, index));
564 else
565 69562 put_bits(&s->pb,
566 69562 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
567 69562 (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
568 69562 av_zero_extend(diff, index));
569 } else {
570
2/2
✓ Branch 0 taken 1861719 times.
✓ Branch 1 taken 1498564 times.
3360283 if (component == 0)
571 1861719 put_bits(&s->pb,
572 1861719 mpeg1_lum_dc_uni[diff + 255] & 0xFF,
573 1861719 mpeg1_lum_dc_uni[diff + 255] >> 8);
574 else
575 1498564 put_bits(&s->pb,
576 1498564 mpeg1_chr_dc_uni[diff + 255] & 0xFF,
577 1498564 mpeg1_chr_dc_uni[diff + 255] >> 8);
578 }
579 3523690 }
580
581 7156099 static void mpeg1_encode_block(MPVEncContext *const s, const int16_t block[], int n)
582 {
583 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
584 int code, component;
585 7156099 const uint16_t (*table_vlc)[2] = ff_mpeg1_vlc_table;
586
587 7156099 last_index = s->c.block_last_index[n];
588
589 /* DC coef */
590
2/2
✓ Branch 0 taken 3523690 times.
✓ Branch 1 taken 3632409 times.
7156099 if (s->c.mb_intra) {
591
2/2
✓ Branch 0 taken 1568126 times.
✓ Branch 1 taken 1955564 times.
3523690 component = (n <= 3 ? 0 : (n & 1) + 1);
592 3523690 dc = block[0]; /* overflow is impossible */
593 3523690 diff = dc - s->last_dc[component];
594 3523690 encode_dc(s, diff, component);
595 3523690 s->last_dc[component] = dc;
596 3523690 i = 1;
597
2/2
✓ Branch 0 taken 2640268 times.
✓ Branch 1 taken 883422 times.
3523690 if (s->c.intra_vlc_format)
598 2640268 table_vlc = ff_mpeg2_vlc_table;
599 } else {
600 /* encode the first coefficient: needs to be done here because
601 * it is handled slightly differently */
602 3632409 level = block[0];
603
2/2
✓ Branch 0 taken 1085563 times.
✓ Branch 1 taken 2546846 times.
3632409 if (abs(level) == 1) {
604 1085563 code = ((uint32_t)level >> 31); /* the sign bit */
605 1085563 put_bits(&s->pb, 2, code | 0x02);
606 1085563 i = 1;
607 } else {
608 2546846 i = 0;
609 2546846 last_non_zero = -1;
610 2546846 goto next_coef;
611 }
612 }
613
614 /* now quantify & encode AC coefs */
615 4609253 last_non_zero = i - 1;
616
617
2/2
✓ Branch 0 taken 163161265 times.
✓ Branch 1 taken 7156099 times.
170317364 for (; i <= last_index; i++) {
618 163161265 j = s->c.intra_scantable.permutated[i];
619 163161265 level = block[j];
620
621 165708111 next_coef:
622 /* encode using VLC */
623
2/2
✓ Branch 0 taken 68738052 times.
✓ Branch 1 taken 96970059 times.
165708111 if (level != 0) {
624 68738052 run = i - last_non_zero - 1;
625
626 68738052 alevel = level;
627 68738052 MASK_ABS(sign, alevel);
628 68738052 sign &= 1;
629
630
2/2
✓ Branch 0 taken 66540040 times.
✓ Branch 1 taken 2198012 times.
68738052 if (alevel <= mpeg12_max_level[run]) {
631 66540040 code = mpeg12_index_run[run] + alevel - 1;
632 /* store the VLC & sign at once */
633 66540040 put_bits(&s->pb, table_vlc[code][1] + 1,
634 66540040 (table_vlc[code][0] << 1) + sign);
635 } else {
636 /* Escape seems to be pretty rare <5% so I do not optimize it.
637 * The following encodes run together with the common escape
638 * value of both tables 000001b. */
639 2198012 put_bits(&s->pb, 6 + 6, 0x01 << 6 | run);
640 /* escape: only clip in this case */
641
2/2
✓ Branch 0 taken 82680 times.
✓ Branch 1 taken 2115332 times.
2198012 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO) {
642
1/2
✓ Branch 0 taken 82680 times.
✗ Branch 1 not taken.
82680 if (alevel < 128) {
643 82680 put_sbits(&s->pb, 8, level);
644 } else {
645 ✗ if (level < 0)
646 ✗ put_bits(&s->pb, 16, 0x8001 + level + 255);
647 else
648 ✗ put_sbits(&s->pb, 16, level);
649 }
650 } else {
651 2115332 put_sbits(&s->pb, 12, level);
652 }
653 }
654 68738052 last_non_zero = i;
655 }
656 }
657 /* end of block */
658 7156099 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
659 7156099 }
660
661 2267628 static av_always_inline void mpeg1_encode_mb_internal(MPVEncContext *const s,
662 const int16_t block[8][64],
663 int motion_x, int motion_y,
664 int mb_block_count,
665 int chroma_y_shift)
666 {
667 /* MPEG-1 is always 420. */
668 #define IS_MPEG1(s) (chroma_y_shift == 1 && (s)->c.codec_id == AV_CODEC_ID_MPEG1VIDEO)
669 int i, cbp;
670 2267628 const int mb_x = s->c.mb_x;
671 2267628 const int mb_y = s->c.mb_y;
672
4/4
✓ Branch 0 taken 349539 times.
✓ Branch 1 taken 1918089 times.
✓ Branch 2 taken 344882 times.
✓ Branch 3 taken 4657 times.
2267628 const int first_mb = mb_x == s->c.resync_mb_x && mb_y == s->c.resync_mb_y;
673
674 /* compute cbp */
675 2267628 cbp = 0;
676
2/2
✓ Branch 0 taken 14892592 times.
✓ Branch 1 taken 2267628 times.
17160220 for (i = 0; i < mb_block_count; i++)
677
2/2
✓ Branch 0 taken 7156099 times.
✓ Branch 1 taken 7736493 times.
14892592 if (s->c.block_last_index[i] >= 0)
678 7156099 cbp |= 1 << (mb_block_count - 1 - i);
679
680
6/6
✓ Branch 0 taken 773325 times.
✓ Branch 1 taken 1494303 times.
✓ Branch 2 taken 744967 times.
✓ Branch 3 taken 28358 times.
✓ Branch 4 taken 724856 times.
✓ Branch 5 taken 20111 times.
2267628 if (cbp == 0 && !first_mb && s->c.mv_type == MV_TYPE_16X16 &&
681
2/2
✓ Branch 0 taken 28920 times.
✓ Branch 1 taken 695936 times.
724856 (mb_x != s->c.mb_width - 1 ||
682
6/6
✓ Branch 0 taken 26867 times.
✓ Branch 1 taken 2053 times.
✓ Branch 2 taken 24929 times.
✓ Branch 3 taken 1938 times.
✓ Branch 4 taken 552 times.
✓ Branch 5 taken 24377 times.
28920 (mb_y != s->c.end_mb_y - 1 && IS_MPEG1(s))) &&
683
4/4
✓ Branch 0 taken 526399 times.
✓ Branch 1 taken 170089 times.
✓ Branch 2 taken 114071 times.
✓ Branch 3 taken 412328 times.
696488 ((s->c.pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
684
4/4
✓ Branch 0 taken 170089 times.
✓ Branch 1 taken 114071 times.
✓ Branch 2 taken 22327 times.
✓ Branch 3 taken 147762 times.
284160 (s->c.pict_type == AV_PICTURE_TYPE_B && s->c.mv_dir == s->last_mv_dir &&
685 22327 (((s->c.mv_dir & MV_DIR_FORWARD)
686 20352 ? ((s->c.mv[0][0][0] - s->c.last_mv[0][0][0]) |
687
4/4
✓ Branch 0 taken 20352 times.
✓ Branch 1 taken 1975 times.
✓ Branch 2 taken 4609 times.
✓ Branch 3 taken 17718 times.
44654 (s->c.mv[0][0][1] - s->c.last_mv[0][0][1])) : 0) |
688 22327 ((s->c.mv_dir & MV_DIR_BACKWARD)
689 21949 ? ((s->c.mv[1][0][0] - s->c.last_mv[1][0][0]) |
690
2/2
✓ Branch 0 taken 21949 times.
✓ Branch 1 taken 378 times.
22327 (s->c.mv[1][0][1] - s->c.last_mv[1][0][1])) : 0)) == 0))) {
691 416937 s->mb_skip_run++;
692 416937 s->c.qscale -= s->dquant;
693 416937 s->misc_bits++;
694 416937 s->last_bits++;
695
2/2
✓ Branch 0 taken 412328 times.
✓ Branch 1 taken 4609 times.
416937 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
696 412328 s->c.last_mv[0][0][0] =
697 412328 s->c.last_mv[0][0][1] =
698 412328 s->c.last_mv[0][1][0] =
699 412328 s->c.last_mv[0][1][1] = 0;
700 }
701 } else {
702
2/2
✓ Branch 0 taken 344882 times.
✓ Branch 1 taken 1505809 times.
1850691 if (first_mb) {
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 344882 times.
344882 av_assert0(s->mb_skip_run == 0);
704 344882 encode_mb_skip_run(s, s->c.mb_x);
705 } else {
706 1505809 encode_mb_skip_run(s, s->mb_skip_run);
707 }
708
709
2/2
✓ Branch 0 taken 415373 times.
✓ Branch 1 taken 1435318 times.
1850691 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
710
3/4
✓ Branch 0 taken 43087 times.
✓ Branch 1 taken 372286 times.
✓ Branch 2 taken 43087 times.
✗ Branch 3 not taken.
415373 if (s->dquant && cbp) {
711 /* macroblock_type: macroblock_quant = 1 */
712 43087 put_mb_modes(s, 2, 1, 0, 0);
713 43087 put_qscale(s);
714 } else {
715 /* macroblock_type: macroblock_quant = 0 */
716 372286 put_mb_modes(s, 1, 1, 0, 0);
717 372286 s->c.qscale -= s->dquant;
718 }
719 415373 s->misc_bits += get_bits_diff(s);
720 415373 s->i_count++;
721
2/2
✓ Branch 0 taken 73518 times.
✓ Branch 1 taken 1361800 times.
1435318 } else if (s->c.mb_intra) {
722
3/4
✓ Branch 0 taken 11685 times.
✓ Branch 1 taken 61833 times.
✓ Branch 2 taken 11685 times.
✗ Branch 3 not taken.
73518 if (s->dquant && cbp) {
723 11685 put_mb_modes(s, 6, 0x01, 0, 0);
724 11685 put_qscale(s);
725 } else {
726 61833 put_mb_modes(s, 5, 0x03, 0, 0);
727 61833 s->c.qscale -= s->dquant;
728 }
729 73518 s->misc_bits += get_bits_diff(s);
730 73518 s->i_count++;
731 73518 memset(s->c.last_mv, 0, sizeof(s->c.last_mv));
732
2/2
✓ Branch 0 taken 778427 times.
✓ Branch 1 taken 583373 times.
1361800 } else if (s->c.pict_type == AV_PICTURE_TYPE_P) {
733
2/2
✓ Branch 0 taken 755569 times.
✓ Branch 1 taken 22858 times.
778427 if (s->c.mv_type == MV_TYPE_16X16) {
734
2/2
✓ Branch 0 taken 594924 times.
✓ Branch 1 taken 160645 times.
755569 if (cbp != 0) {
735
2/2
✓ Branch 0 taken 80169 times.
✓ Branch 1 taken 514755 times.
594924 if ((motion_x | motion_y) == 0) {
736
2/2
✓ Branch 0 taken 1504 times.
✓ Branch 1 taken 78665 times.
80169 if (s->dquant) {
737 /* macroblock_pattern & quant */
738 1504 put_mb_modes(s, 5, 1, 0, 0);
739 1504 put_qscale(s);
740 } else {
741 /* macroblock_pattern only */
742 78665 put_mb_modes(s, 2, 1, 0, 0);
743 }
744 80169 s->misc_bits += get_bits_diff(s);
745 } else {
746
2/2
✓ Branch 0 taken 73008 times.
✓ Branch 1 taken 441747 times.
514755 if (s->dquant) {
747 73008 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
748 73008 put_qscale(s);
749 } else {
750 441747 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
751 }
752 514755 s->misc_bits += get_bits_diff(s);
753 // RAL: f_code parameter added
754 514755 mpeg1_encode_motion(s,
755 514755 motion_x - s->c.last_mv[0][0][0],
756 s->f_code);
757 // RAL: f_code parameter added
758 514755 mpeg1_encode_motion(s,
759 514755 motion_y - s->c.last_mv[0][0][1],
760 s->f_code);
761 514755 s->mv_bits += get_bits_diff(s);
762 }
763 } else {
764 160645 put_bits(&s->pb, 3, 1); /* motion only */
765
2/2
✓ Branch 0 taken 24912 times.
✓ Branch 1 taken 135733 times.
160645 if (!s->c.frame_pred_frame_dct)
766 24912 put_bits(&s->pb, 2, 2); /* motion_type: frame */
767 160645 s->misc_bits += get_bits_diff(s);
768 // RAL: f_code parameter added
769 160645 mpeg1_encode_motion(s,
770 160645 motion_x - s->c.last_mv[0][0][0],
771 s->f_code);
772 // RAL: f_code parameter added
773 160645 mpeg1_encode_motion(s,
774 160645 motion_y - s->c.last_mv[0][0][1],
775 s->f_code);
776 160645 s->c.qscale -= s->dquant;
777 160645 s->mv_bits += get_bits_diff(s);
778 }
779 755569 s->c.last_mv[0][1][0] = s->c.last_mv[0][0][0] = motion_x;
780 755569 s->c.last_mv[0][1][1] = s->c.last_mv[0][0][1] = motion_y;
781 } else {
782 av_assert2(!s->c.frame_pred_frame_dct && s->c.mv_type == MV_TYPE_FIELD);
783
784
2/2
✓ Branch 0 taken 21370 times.
✓ Branch 1 taken 1488 times.
22858 if (cbp) {
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21370 times.
21370 if (s->dquant) {
786 ✗ put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
787 ✗ put_qscale(s);
788 } else {
789 21370 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
790 }
791 } else {
792 1488 put_bits(&s->pb, 3, 1); /* motion only */
793 1488 put_bits(&s->pb, 2, 1); /* motion_type: field */
794 1488 s->c.qscale -= s->dquant;
795 }
796 22858 s->misc_bits += get_bits_diff(s);
797
2/2
✓ Branch 0 taken 45716 times.
✓ Branch 1 taken 22858 times.
68574 for (i = 0; i < 2; i++) {
798 45716 put_bits(&s->pb, 1, s->c.field_select[0][i]);
799 45716 mpeg1_encode_motion(s,
800 45716 s->c.mv[0][i][0] - s->c.last_mv[0][i][0],
801 s->f_code);
802 45716 mpeg1_encode_motion(s,
803 45716 s->c.mv[0][i][1] - (s->c.last_mv[0][i][1] >> 1),
804 s->f_code);
805 45716 s->c.last_mv[0][i][0] = s->c.mv[0][i][0];
806 45716 s->c.last_mv[0][i][1] = 2 * s->c.mv[0][i][1];
807 }
808 22858 s->mv_bits += get_bits_diff(s);
809 }
810
2/2
✓ Branch 0 taken 616294 times.
✓ Branch 1 taken 162133 times.
778427 if (cbp) {
811
2/2
✓ Branch 0 taken 541485 times.
✓ Branch 1 taken 74809 times.
616294 if (chroma_y_shift) {
812 541485 put_bits(&s->pb,
813 541485 ff_mpeg12_mbPatTable[cbp][1],
814 541485 ff_mpeg12_mbPatTable[cbp][0]);
815 } else {
816 74809 put_bits(&s->pb,
817 74809 ff_mpeg12_mbPatTable[cbp >> 2][1],
818 74809 ff_mpeg12_mbPatTable[cbp >> 2][0]);
819 74809 put_sbits(&s->pb, 2, cbp);
820 }
821 }
822 } else {
823
2/2
✓ Branch 0 taken 490343 times.
✓ Branch 1 taken 93030 times.
583373 if (s->c.mv_type == MV_TYPE_16X16) {
824
2/2
✓ Branch 0 taken 315132 times.
✓ Branch 1 taken 175211 times.
490343 if (cbp) { // With coded bloc pattern
825
2/2
✓ Branch 0 taken 54069 times.
✓ Branch 1 taken 261063 times.
315132 if (s->dquant) {
826
2/2
✓ Branch 0 taken 11443 times.
✓ Branch 1 taken 42626 times.
54069 if (s->c.mv_dir == MV_DIR_FORWARD)
827 11443 put_mb_modes(s, 6, 3, 1, 0);
828 else
829 42626 put_mb_modes(s, 8 - s->c.mv_dir, 2, 1, 0);
830 54069 put_qscale(s);
831 } else {
832 261063 put_mb_modes(s, 5 - s->c.mv_dir, 3, 1, 0);
833 }
834 } else { // No coded bloc pattern
835 175211 put_bits(&s->pb, 5 - s->c.mv_dir, 2);
836
2/2
✓ Branch 0 taken 81763 times.
✓ Branch 1 taken 93448 times.
175211 if (!s->c.frame_pred_frame_dct)
837 81763 put_bits(&s->pb, 2, 2); /* motion_type: frame */
838 175211 s->c.qscale -= s->dquant;
839 }
840 490343 s->misc_bits += get_bits_diff(s);
841
2/2
✓ Branch 0 taken 352710 times.
✓ Branch 1 taken 137633 times.
490343 if (s->c.mv_dir & MV_DIR_FORWARD) {
842 352710 mpeg1_encode_motion(s,
843 352710 s->c.mv[0][0][0] - s->c.last_mv[0][0][0],
844 s->f_code);
845 352710 mpeg1_encode_motion(s,
846 352710 s->c.mv[0][0][1] - s->c.last_mv[0][0][1],
847 s->f_code);
848 352710 s->c.last_mv[0][0][0] =
849 352710 s->c.last_mv[0][1][0] = s->c.mv[0][0][0];
850 352710 s->c.last_mv[0][0][1] =
851 352710 s->c.last_mv[0][1][1] = s->c.mv[0][0][1];
852 }
853
2/2
✓ Branch 0 taken 371523 times.
✓ Branch 1 taken 118820 times.
490343 if (s->c.mv_dir & MV_DIR_BACKWARD) {
854 371523 mpeg1_encode_motion(s,
855 371523 s->c.mv[1][0][0] - s->c.last_mv[1][0][0],
856 s->b_code);
857 371523 mpeg1_encode_motion(s,
858 371523 s->c.mv[1][0][1] - s->c.last_mv[1][0][1],
859 s->b_code);
860 371523 s->c.last_mv[1][0][0] =
861 371523 s->c.last_mv[1][1][0] = s->c.mv[1][0][0];
862 371523 s->c.last_mv[1][0][1] =
863 371523 s->c.last_mv[1][1][1] = s->c.mv[1][0][1];
864 }
865 } else {
866 av_assert2(s->c.mv_type == MV_TYPE_FIELD);
867 av_assert2(!s->c.frame_pred_frame_dct);
868
2/2
✓ Branch 0 taken 73986 times.
✓ Branch 1 taken 19044 times.
93030 if (cbp) { // With coded bloc pattern
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73986 times.
73986 if (s->dquant) {
870 ✗ if (s->c.mv_dir == MV_DIR_FORWARD)
871 ✗ put_mb_modes(s, 6, 3, 1, 1);
872 else
873 ✗ put_mb_modes(s, 8 - s->c.mv_dir, 2, 1, 1);
874 ✗ put_qscale(s);
875 } else {
876 73986 put_mb_modes(s, 5 - s->c.mv_dir, 3, 1, 1);
877 }
878 } else { // No coded bloc pattern
879 19044 put_bits(&s->pb, 5 - s->c.mv_dir, 2);
880 19044 put_bits(&s->pb, 2, 1); /* motion_type: field */
881 19044 s->c.qscale -= s->dquant;
882 }
883 93030 s->misc_bits += get_bits_diff(s);
884
2/2
✓ Branch 0 taken 59416 times.
✓ Branch 1 taken 33614 times.
93030 if (s->c.mv_dir & MV_DIR_FORWARD) {
885
2/2
✓ Branch 0 taken 118832 times.
✓ Branch 1 taken 59416 times.
178248 for (i = 0; i < 2; i++) {
886 118832 put_bits(&s->pb, 1, s->c.field_select[0][i]);
887 118832 mpeg1_encode_motion(s,
888 118832 s->c.mv[0][i][0] - s->c.last_mv[0][i][0],
889 s->f_code);
890 118832 mpeg1_encode_motion(s,
891 118832 s->c.mv[0][i][1] - (s->c.last_mv[0][i][1] >> 1),
892 s->f_code);
893 118832 s->c.last_mv[0][i][0] = s->c.mv[0][i][0];
894 118832 s->c.last_mv[0][i][1] = s->c.mv[0][i][1] * 2;
895 }
896 }
897
2/2
✓ Branch 0 taken 59730 times.
✓ Branch 1 taken 33300 times.
93030 if (s->c.mv_dir & MV_DIR_BACKWARD) {
898
2/2
✓ Branch 0 taken 119460 times.
✓ Branch 1 taken 59730 times.
179190 for (i = 0; i < 2; i++) {
899 119460 put_bits(&s->pb, 1, s->c.field_select[1][i]);
900 119460 mpeg1_encode_motion(s,
901 119460 s->c.mv[1][i][0] - s->c.last_mv[1][i][0],
902 s->b_code);
903 119460 mpeg1_encode_motion(s,
904 119460 s->c.mv[1][i][1] - (s->c.last_mv[1][i][1] >> 1),
905 s->b_code);
906 119460 s->c.last_mv[1][i][0] = s->c.mv[1][i][0];
907 119460 s->c.last_mv[1][i][1] = s->c.mv[1][i][1] * 2;
908 }
909 }
910 }
911 583373 s->mv_bits += get_bits_diff(s);
912
2/2
✓ Branch 0 taken 389118 times.
✓ Branch 1 taken 194255 times.
583373 if (cbp) {
913
2/2
✓ Branch 0 taken 208201 times.
✓ Branch 1 taken 180917 times.
389118 if (chroma_y_shift) {
914 208201 put_bits(&s->pb,
915 208201 ff_mpeg12_mbPatTable[cbp][1],
916 208201 ff_mpeg12_mbPatTable[cbp][0]);
917 } else {
918 180917 put_bits(&s->pb,
919 180917 ff_mpeg12_mbPatTable[cbp >> 2][1],
920 180917 ff_mpeg12_mbPatTable[cbp >> 2][0]);
921 180917 put_sbits(&s->pb, 2, cbp);
922 }
923 }
924 }
925
2/2
✓ Branch 0 taken 12389722 times.
✓ Branch 1 taken 1850691 times.
14240413 for (i = 0; i < mb_block_count; i++)
926
2/2
✓ Branch 0 taken 7156099 times.
✓ Branch 1 taken 5233623 times.
12389722 if (cbp & (1 << (mb_block_count - 1 - i)))
927 7156099 mpeg1_encode_block(s, block[i], i);
928 1850691 s->mb_skip_run = 0;
929
2/2
✓ Branch 0 taken 488891 times.
✓ Branch 1 taken 1361800 times.
1850691 if (s->c.mb_intra)
930 488891 s->i_tex_bits += get_bits_diff(s);
931 else
932 1361800 s->p_tex_bits += get_bits_diff(s);
933 }
934 2267628 }
935
936 2267628 static void mpeg12_encode_mb(MPVEncContext *const s, int16_t block[][64],
937 int motion_x, int motion_y)
938 {
939
2/2
✓ Branch 0 taken 1778737 times.
✓ Branch 1 taken 488891 times.
2267628 if (!s->c.mb_intra)
940 1778737 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 128 << s->c.intra_dc_precision;
941
2/2
✓ Branch 0 taken 1624216 times.
✓ Branch 1 taken 643412 times.
2267628 if (s->c.chroma_format == CHROMA_420)
942 1624216 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6, 1);
943 else
944 643412 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8, 0);
945 2267628 }
946
947 78 static av_cold void mpeg12_encode_init_static(void)
948 {
949 78 ff_rl_init_level_run(mpeg12_max_level, mpeg12_index_run,
950 ff_mpeg12_run, ff_mpeg12_level, MPEG12_RL_NB_ELEMS);
951
952 78 ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run,
953 ff_mpeg1_vlc_table, uni_mpeg1_ac_vlc_len);
954 78 ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run,
955 ff_mpeg2_vlc_table, uni_mpeg2_ac_vlc_len);
956
957 /* build unified dc encoding tables */
958
2/2
✓ Branch 0 taken 39858 times.
✓ Branch 1 taken 78 times.
39936 for (int i = -255; i < 256; i++) {
959 int adiff, index;
960 int bits, code;
961 39858 int diff = i;
962
963 39858 adiff = FFABS(diff);
964
2/2
✓ Branch 0 taken 19890 times.
✓ Branch 1 taken 19968 times.
39858 if (diff < 0)
965 19890 diff--;
966 39858 index = av_log2(2 * adiff);
967
968 39858 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
969 39858 code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
970 39858 av_zero_extend(diff, index);
971 39858 mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
972
973 39858 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
974 39858 code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
975 39858 av_zero_extend(diff, index);
976 39858 mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
977 }
978
979
2/2
✓ Branch 0 taken 546 times.
✓ Branch 1 taken 78 times.
624 for (int f_code = 1; f_code <= MAX_FCODE; f_code++)
980
2/2
✓ Branch 0 taken 8946210 times.
✓ Branch 1 taken 546 times.
8946756 for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) {
981 int len;
982
983
2/2
✓ Branch 0 taken 546 times.
✓ Branch 1 taken 8945664 times.
8946210 if (mv == 0) {
984 546 len = 1; /* ff_mpeg12_mbMotionVectorTable[0][1] */
985 } else {
986 int val, bit_size, code;
987
988 8945664 bit_size = f_code - 1;
989
990 8945664 val = mv;
991
2/2
✓ Branch 0 taken 4472832 times.
✓ Branch 1 taken 4472832 times.
8945664 if (val < 0)
992 4472832 val = -val;
993 8945664 val--;
994 8945664 code = (val >> bit_size) + 1;
995
2/2
✓ Branch 0 taken 316992 times.
✓ Branch 1 taken 8628672 times.
8945664 if (code < 17)
996 316992 len = ff_mpeg12_mbMotionVectorTable[code][1] +
997 1 + bit_size;
998 else
999 8628672 len = 10 /* ff_mpeg12_mbMotionVectorTable[16][1] */ +
1000 2 + bit_size;
1001 }
1002
1003 8946210 mv_penalty[f_code][mv + MAX_DMV] = len;
1004 }
1005
1006
1007
2/2
✓ Branch 0 taken 546 times.
✓ Branch 1 taken 78 times.
624 for (int f_code = MAX_FCODE; f_code > 0; f_code--)
1008
2/2
✓ Branch 0 taken 316992 times.
✓ Branch 1 taken 546 times.
317538 for (int mv = -(8 << f_code); mv < (8 << f_code); mv++)
1009 316992 fcode_tab[mv + MAX_MV] = f_code;
1010 78 }
1011
1012 78 static av_cold int find_frame_rate_index(AVCodecContext *avctx, MPEG12EncContext *mpeg12)
1013 {
1014 78 AVRational bestq = (AVRational) {0, 0};
1015 AVRational ext;
1016 78 AVRational target = av_inv_q(avctx->time_base);
1017
1018
2/2
✓ Branch 0 taken 706 times.
✓ Branch 1 taken 1 times.
707 for (int i = 1; i < 14; i++) {
1019
4/4
✓ Branch 0 taken 693 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 77 times.
✓ Branch 3 taken 616 times.
706 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
1020 i >= 9)
1021 77 break;
1022
1023
2/2
✓ Branch 0 taken 2516 times.
✓ Branch 1 taken 629 times.
3145 for (ext.num = 1; ext.num <= 4; ext.num++) {
1024
2/2
✓ Branch 0 taken 80512 times.
✓ Branch 1 taken 2516 times.
83028 for (ext.den = 1; ext.den <= 32; ext.den++) {
1025 80512 AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]);
1026
1027
6/6
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 69248 times.
✓ Branch 2 taken 352 times.
✓ Branch 3 taken 10912 times.
✓ Branch 4 taken 264 times.
✓ Branch 5 taken 88 times.
80512 if (avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
1028 33898 continue;
1029
2/2
✓ Branch 0 taken 22722 times.
✓ Branch 1 taken 46614 times.
69336 if (av_gcd(ext.den, ext.num) != 1)
1030 22722 continue;
1031
1032
2/2
✓ Branch 0 taken 46536 times.
✓ Branch 1 taken 78 times.
46614 if ( bestq.num==0
1033
2/2
✓ Branch 1 taken 46178 times.
✓ Branch 2 taken 358 times.
46536 || av_nearer_q(target, bestq, q) < 0
1034
5/6
✓ Branch 0 taken 16977 times.
✓ Branch 1 taken 29201 times.
✓ Branch 2 taken 423 times.
✓ Branch 3 taken 16554 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 423 times.
46178 || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) {
1035 436 bestq = q;
1036 436 mpeg12->frame_rate_index = i;
1037 436 mpeg12->frame_rate_ext.num = ext.num;
1038 436 mpeg12->frame_rate_ext.den = ext.den;
1039 }
1040 }
1041 }
1042 }
1043
1044
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 77 times.
78 if (av_cmp_q(target, bestq))
1045 1 return -1;
1046 else
1047 77 return 0;
1048 }
1049
1050 78 static av_cold int encode_init(AVCodecContext *avctx)
1051 {
1052 static AVOnce init_static_once = AV_ONCE_INIT;
1053 78 MPEG12EncContext *const mpeg12 = avctx->priv_data;
1054 78 MPVMainEncContext *const m = &mpeg12->mpeg;
1055 78 MPVEncContext *const s = &m->s;
1056 int ret;
1057
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 11 times.
78 int max_size = avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 16383 : 4095;
1058
1059
2/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 78 times.
78 if (avctx->width > max_size || avctx->height > max_size) {
1060 ✗ av_log(avctx, AV_LOG_ERROR, "%s does not support resolutions above %dx%d\n",
1061 ✗ CONFIG_SMALL ? avctx->codec->name : avctx->codec->long_name,
1062 max_size, max_size);
1063 ✗ return AVERROR(EINVAL);
1064 }
1065
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
78 if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
1066 ✗ av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
1067 ✗ return AVERROR(EINVAL);
1068 }
1069
1070
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 1 times.
78 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
1071
2/4
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 77 times.
77 if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) {
1072 ✗ av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiples of 4096\n"
1073 "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
1074 ✗ return AVERROR(EINVAL);
1075 }
1076 }
1077
1078
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 77 times.
78 if (s->c.q_scale_type == 1) {
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->qmax > 28) {
1080 ✗ av_log(avctx, AV_LOG_ERROR,
1081 "non linear quant only supports qmax <= 28 currently\n");
1082 ✗ return AVERROR_PATCHWELCOME;
1083 }
1084 }
1085
1086
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 if (avctx->profile == AV_PROFILE_UNKNOWN) {
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (avctx->level != AV_LEVEL_UNKNOWN) {
1088 ✗ av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
1089 ✗ return AVERROR(EINVAL);
1090 }
1091 /* Main or 4:2:2 */
1092 78 avctx->profile = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? AV_PROFILE_MPEG2_MAIN
1093
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 5 times.
78 : AV_PROFILE_MPEG2_422;
1094 }
1095
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 if (avctx->level == AV_LEVEL_UNKNOWN) {
1096
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 73 times.
78 if (avctx->profile == AV_PROFILE_MPEG2_422) { /* 4:2:2 */
1097
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)
1098 5 avctx->level = 5; /* Main */
1099 else
1100 ✗ avctx->level = 2; /* High */
1101 } else {
1102
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
73 if (avctx->profile != AV_PROFILE_MPEG2_HIGH &&
1103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
1104 ✗ av_log(avctx, AV_LOG_ERROR,
1105 "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
1106 ✗ return AVERROR(EINVAL);
1107 }
1108
3/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
73 if (avctx->width <= 720 && avctx->height <= 576)
1109 72 avctx->level = 8; /* Main */
1110
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (avctx->width <= 1440)
1111 1 avctx->level = 6; /* High 1440 */
1112 else
1113 ✗ avctx->level = 4; /* High */
1114 }
1115 }
1116
1117 78 m->encode_picture_header = mpeg1_encode_picture_header;
1118 78 s->encode_mb = mpeg12_encode_mb;
1119
1120 78 s->me.mv_penalty = mv_penalty;
1121 78 m->fcode_tab = fcode_tab + MAX_MV;
1122
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 67 times.
78 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1123 11 s->min_qcoeff = -255;
1124 11 s->max_qcoeff = 255;
1125 } else {
1126 67 s->min_qcoeff = -2047;
1127 67 s->max_qcoeff = 2047;
1128 67 s->mpeg_quant = 1;
1129 #if FF_API_INTRA_DC_PRECISION
1130
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 5 times.
67 if (s->c.intra_dc_precision < 0) {
1131 FF_DISABLE_DEPRECATION_WARNINGS
1132 62 s->c.intra_dc_precision = avctx->intra_dc_precision;
1133 FF_ENABLE_DEPRECATION_WARNINGS
1134 // workaround some differences between how applications specify dc precision
1135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (s->c.intra_dc_precision < 0) {
1136 ✗ s->c.intra_dc_precision += 8;
1137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 } else if (s->c.intra_dc_precision >= 8)
1138 ✗ s->c.intra_dc_precision -= 8;
1139
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (s->c.intra_dc_precision < 0) {
1141 ✗ av_log(avctx, AV_LOG_ERROR,
1142 "intra dc precision must be positive, note some applications use"
1143 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
1144 ✗ return AVERROR(EINVAL);
1145 }
1146
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (s->c.intra_dc_precision > 3) {
1148 ✗ av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
1149 ✗ return AVERROR(EINVAL);
1150 }
1151 }
1152 #endif
1153 }
1154 78 s->c.y_dc_scale_table =
1155 78 s->c.c_dc_scale_table = ff_mpeg12_dc_scale_table[s->c.intra_dc_precision];
1156
1157
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 65 times.
78 if (s->c.intra_vlc_format) {
1158 13 s->intra_ac_vlc_length =
1159 13 s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len;
1160 } else {
1161 65 s->intra_ac_vlc_length =
1162 65 s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1163 }
1164 78 s->inter_ac_vlc_length =
1165 78 s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
1166
1167 78 ret = ff_mpv_encode_init(avctx);
1168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (ret < 0)
1169 ✗ return ret;
1170
1171
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 67 times.
78 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 s->c.thread_context[s->c.slice_context_count - 1]->start_mb_y >
1173 SLICE_MAX_START_CODE - SLICE_MIN_START_CODE) {
1174 // MPEG-1 slices must not start at a MB row number that would make
1175 // their start code > SLICE_MAX_START_CODE. So make the last slice
1176 // bigger if needed and evenly distribute the first 174 rows.
1177 static_assert(MAX_THREADS <= 1 + SLICE_MAX_START_CODE - SLICE_MIN_START_CODE,
1178 "With more than 175 slice contexts, we have to handle "
1179 "the case in which there is no work to do for some "
1180 "slice contexts.");
1181 ✗ const int mb_height = SLICE_MAX_START_CODE - SLICE_MIN_START_CODE;
1182 ✗ const int nb_slices = s->c.slice_context_count - 1;
1183
1184 ✗ s->c.thread_context[nb_slices]->start_mb_y = mb_height;
1185
1186 av_assert1(nb_slices >= 1);
1187 ✗ for (int i = 0; i < nb_slices; i++) {
1188 ✗ s->c.thread_context[i]->start_mb_y =
1189 ✗ (mb_height * (i ) + nb_slices / 2) / nb_slices;
1190 ✗ s->c.thread_context[i]->end_mb_y =
1191 ✗ (mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1192 }
1193 }
1194
1195
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 77 times.
78 if (find_frame_rate_index(avctx, mpeg12) < 0) {
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1197 ✗ av_log(avctx, AV_LOG_ERROR, "MPEG-1/2 does not support %d/%d fps\n",
1198 avctx->time_base.den, avctx->time_base.num);
1199 ✗ return AVERROR(EINVAL);
1200 } else {
1201 1 av_log(avctx, AV_LOG_INFO,
1202 "MPEG-1/2 does not support %d/%d fps, there may be AV sync issues\n",
1203 avctx->time_base.den, avctx->time_base.num);
1204 }
1205 }
1206
1207
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 76 times.
78 if (avctx->rc_max_rate &&
1208
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 avctx->rc_min_rate == avctx->rc_max_rate &&
1209 2 90000LL * (avctx->rc_buffer_size - 1) >
1210
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 avctx->rc_max_rate * 0xFFFFLL) {
1211 1 av_log(avctx, AV_LOG_INFO,
1212 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
1213 "specified vbv buffer is too large for the given bitrate!\n");
1214 }
1215
1216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (mpeg12->drop_frame_timecode)
1217 ✗ mpeg12->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME;
1218
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
78 if (mpeg12->drop_frame_timecode && mpeg12->frame_rate_index != 4) {
1219 ✗ av_log(avctx, AV_LOG_ERROR,
1220 "Drop frame time code only allowed with 1001/30000 fps\n");
1221 ✗ return AVERROR(EINVAL);
1222 }
1223
1224
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 72 times.
78 if (mpeg12->tc_opt_str) {
1225 6 AVRational rate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index];
1226 6 int ret = av_timecode_init_from_string(&mpeg12->tc, rate, mpeg12->tc_opt_str, avctx);
1227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1228 ✗ return ret;
1229 6 mpeg12->drop_frame_timecode = !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME);
1230 6 mpeg12->timecode_frame_start = mpeg12->tc.start;
1231 } else {
1232 72 mpeg12->timecode_frame_start = 0; // default is -1
1233 }
1234
1235 78 ff_thread_once(&init_static_once, mpeg12_encode_init_static);
1236
1237 78 return 0;
1238 }
1239
1240 #define OFFSET(x) offsetof(MPEG12EncContext, x)
1241 #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
1242 #define COMMON_OPTS \
1243 { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format. Overrides timecode_frame_start.", \
1244 OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE },\
1245 { "drop_frame_timecode", "Timecode is in drop frame format.", \
1246 OFFSET(drop_frame_timecode), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
1247 { "scan_offset", "Reserve space for SVCD scan offset user data.", \
1248 OFFSET(scan_offset), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
1249 { "timecode_frame_start", "GOP timecode frame start number, in non-drop-frame format", \
1250 OFFSET(timecode_frame_start), AV_OPT_TYPE_INT64, {.i64 = -1 }, -1, INT64_MAX, VE}, \
1251 FF_MPV_COMMON_BFRAME_OPTS
1252
1253 static const AVOption mpeg1_options[] = {
1254 COMMON_OPTS
1255 FF_MPV_COMMON_OPTS
1256 FF_MPV_COMMON_MOTION_EST_OPTS
1257 { NULL },
1258 };
1259
1260 static const AVOption mpeg2_options[] = {
1261 COMMON_OPTS
1262 #if FF_API_INTRA_DC_PRECISION
1263 { "intra_dc_precision", "Precision of the DC coefficient - 8", FF_MPV_OFFSET(c.intra_dc_precision), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 3, VE },
1264 #else
1265 { "intra_dc_precision", "Precision of the DC coefficient - 8", FF_MPV_OFFSET(c.intra_dc_precision), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, VE },
1266 #endif
1267 { "intra_vlc", "Use MPEG-2 intra VLC table.",
1268 FF_MPV_OFFSET(c.intra_vlc_format), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1269 { "non_linear_quant", "Use nonlinear quantizer.", FF_MPV_OFFSET(c.q_scale_type), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1270 { "alternate_scan", "Enable alternate scantable.", FF_MPV_OFFSET(c.alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1271 { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE },
1272 { "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" },
1273 { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, VE, .unit = "seq_disp_ext" },
1274 { "never", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, VE, .unit = "seq_disp_ext" },
1275 { "always", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, VE, .unit = "seq_disp_ext" },
1276 { "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" },
1277 { "component", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_COMPONENT }, 0, 0, VE, .unit = "video_format" },
1278 { "pal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_PAL }, 0, 0, VE, .unit = "video_format" },
1279 { "ntsc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_NTSC }, 0, 0, VE, .unit = "video_format" },
1280 { "secam", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_SECAM }, 0, 0, VE, .unit = "video_format" },
1281 { "mac", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_MAC }, 0, 0, VE, .unit = "video_format" },
1282 { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_UNSPECIFIED}, 0, 0, VE, .unit = "video_format" },
1283 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, { .i64 = value }, 0, 0, VE, .unit = "avctx.level"
1284 { LEVEL("high", 4) },
1285 { LEVEL("high1440", 6) },
1286 { LEVEL("main", 8) },
1287 { LEVEL("low", 10) },
1288 #undef LEVEL
1289 FF_MPV_COMMON_OPTS
1290 FF_MPV_COMMON_MOTION_EST_OPTS
1291 FF_MPEG2_PROFILE_OPTS
1292 { NULL },
1293 };
1294
1295 #define mpeg12_class(x) \
1296 static const AVClass mpeg ## x ## _class = { \
1297 .class_name = "mpeg" # x "video encoder", \
1298 .item_name = av_default_item_name, \
1299 .option = mpeg ## x ## _options, \
1300 .version = LIBAVUTIL_VERSION_INT, \
1301 };
1302
1303 mpeg12_class(1)
1304 mpeg12_class(2)
1305
1306 const FFCodec ff_mpeg1video_encoder = {
1307 .p.name = "mpeg1video",
1308 CODEC_LONG_NAME("MPEG-1 video"),
1309 .p.type = AVMEDIA_TYPE_VIDEO,
1310 .p.id = AV_CODEC_ID_MPEG1VIDEO,
1311 .priv_data_size = sizeof(MPEG12EncContext),
1312 .init = encode_init,
1313 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
1314 .close = ff_mpv_encode_end,
1315 CODEC_FRAMERATES_ARRAY(ff_mpeg12_frame_rate_tab + 1),
1316 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P),
1317 CODEC_CHROMA_LOCS(AVCHROMA_LOC_CENTER),
1318 .color_ranges = AVCOL_RANGE_MPEG,
1319 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1320 AV_CODEC_CAP_SLICE_THREADS |
1321 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1322 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1323 .p.priv_class = &mpeg1_class,
1324 };
1325
1326 473 static int mpeg2_get_supported_config(const AVCodecContext *avctx,
1327 const AVCodec *codec,
1328 enum AVCodecConfig config,
1329 unsigned flags, const void **out,
1330 int *out_num)
1331 {
1332
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 406 times.
473 if (config == AV_CODEC_CONFIG_CHROMA_LOCATION) {
1333 static const enum AVChromaLocation left[] = { AVCHROMA_LOC_LEFT, AVCHROMA_LOC_UNSPECIFIED };
1334 static const enum AVChromaLocation topleft[] = { AVCHROMA_LOC_TOPLEFT, AVCHROMA_LOC_UNSPECIFIED };
1335
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 enum AVPixelFormat pix_fmt = avctx ? avctx->pix_fmt : AV_PIX_FMT_NONE;
1336
1337 /* 4:2:2 has no vertical subsampling, so the left siting of 4:2:0
1338 * fits it too when the pixel format is not decided yet. */
1339
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 62 times.
67 *out = pix_fmt == AV_PIX_FMT_YUV422P ? topleft : left;
1340 67 *out_num = 1;
1341 67 return 0;
1342 }
1343
1344 406 return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
1345 }
1346
1347 const FFCodec ff_mpeg2video_encoder = {
1348 .p.name = "mpeg2video",
1349 CODEC_LONG_NAME("MPEG-2 video"),
1350 .p.type = AVMEDIA_TYPE_VIDEO,
1351 .p.id = AV_CODEC_ID_MPEG2VIDEO,
1352 .priv_data_size = sizeof(MPEG12EncContext),
1353 .init = encode_init,
1354 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
1355 .close = ff_mpv_encode_end,
1356 CODEC_FRAMERATES_ARRAY(ff_mpeg2_frame_rate_tab),
1357 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P),
1358 .get_supported_config = mpeg2_get_supported_config,
1359 .color_ranges = AVCOL_RANGE_MPEG,
1360 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1361 AV_CODEC_CAP_SLICE_THREADS |
1362 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1363 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1364 .p.priv_class = &mpeg2_class,
1365 };
1366 #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */
1367