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