mpeg2enc: Don't mark all streams as component video
[libav.git] / libavcodec / mpeg12enc.c
CommitLineData
a6bc5731 1/*
41ed7ab4 2 * MPEG-1/2 encoder
406792e7 3 * Copyright (c) 2000,2001 Fabrice Bellard
a6bc5731
AJ
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
2912e87a 6 * This file is part of Libav.
a6bc5731 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
a6bc5731
AJ
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
a6bc5731
AJ
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
a6bc5731
AJ
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
ba87f080 24 * @file
41ed7ab4 25 * MPEG-1/2 encoder
a6bc5731
AJ
26 */
27
8f8bc923
DB
28#include <stdint.h>
29
7f93a861
VG
30#include "libavutil/attributes.h"
31#include "libavutil/log.h"
32#include "libavutil/opt.h"
3d1d1753
VG
33#include "libavutil/stereo3d.h"
34
a6bc5731 35#include "avcodec.h"
7f93a861 36#include "bytestream.h"
2f329db9 37#include "mathops.h"
a6bc5731
AJ
38#include "mpeg12.h"
39#include "mpeg12data.h"
8fc6a70c 40#include "mpegutils.h"
7f93a861 41#include "mpegvideo.h"
a6bc5731
AJ
42
43
7f93a861
VG
44static const uint8_t inv_non_linear_qscale[] = {
45 0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16,
a6bc5731
AJ
46};
47
7f93a861
VG
48static const uint8_t svcd_scan_offset_placeholder[] = {
49 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
50 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
a6bc5731
AJ
51};
52
7f93a861
VG
53static uint8_t mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
54static uint8_t fcode_tab[MAX_MV * 2 + 1];
a6bc5731 55
7f93a861
VG
56static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2];
57static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2];
a6bc5731 58
7f93a861
VG
59/* simple include everything table for dc, first byte is bits
60 * number next 3 are code */
a6bc5731
AJ
61static uint32_t mpeg1_lum_dc_uni[512];
62static uint32_t mpeg1_chr_dc_uni[512];
63
64static uint8_t mpeg1_index_run[2][64];
7f93a861 65static int8_t mpeg1_max_level[2][64];
a6bc5731 66
6fee1b90
DB
67static av_cold void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len)
68{
a6bc5731
AJ
69 int i;
70
7f93a861
VG
71 for (i = 0; i < 128; i++) {
72 int level = i - 64;
a6bc5731 73 int run;
7c5dfc17
AC
74 if (!level)
75 continue;
7f93a861 76 for (run = 0; run < 64; run++) {
090aaaf7 77 int len, code;
7f93a861 78 int alevel = FFABS(level);
a6bc5731
AJ
79
80 if (alevel > rl->max_level[0][run])
7f93a861 81 code = 111; /* rl->n */
a6bc5731 82 else
7f93a861 83 code = rl->index_run[0][run] + alevel - 1;
a6bc5731 84
7f93a861
VG
85 if (code < 111) { /* rl->n */
86 /* length of VLC and sign */
87 len = rl->table_vlc[code][1] + 1;
a6bc5731 88 } else {
7f93a861 89 len = rl->table_vlc[111][1] + 6; /* rl->n */
a6bc5731 90
7f93a861 91 if (alevel < 128)
090aaaf7 92 len += 8;
7f93a861 93 else
090aaaf7 94 len += 16;
a6bc5731
AJ
95 }
96
7f93a861 97 uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
a6bc5731
AJ
98 }
99 }
100}
101
7f93a861
VG
102static int find_frame_rate_index(MpegEncContext *s)
103{
a6bc5731 104 int i;
7f93a861 105 int64_t dmin = INT64_MAX;
a6bc5731
AJ
106 int64_t d;
107
7f93a861
VG
108 for (i = 1; i < 14; i++) {
109 int64_t n0 = 1001LL / ff_mpeg12_frame_rate_tab[i].den *
110 ff_mpeg12_frame_rate_tab[i].num * s->avctx->time_base.num;
111 int64_t n1 = 1001LL * s->avctx->time_base.den;
112
113 if (s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
114 i >= 9)
115 break;
a6bc5731
AJ
116
117 d = FFABS(n0 - n1);
7f93a861
VG
118 if (d < dmin) {
119 dmin = d;
120 s->frame_rate_index = i;
a6bc5731
AJ
121 }
122 }
7f93a861
VG
123
124 if (dmin)
a6bc5731
AJ
125 return -1;
126 else
127 return 0;
128}
129
98a6fff9 130static av_cold int encode_init(AVCodecContext *avctx)
a6bc5731
AJ
131{
132 MpegEncContext *s = avctx->priv_data;
133
835f798c 134 if (ff_mpv_encode_init(avctx) < 0)
a6bc5731
AJ
135 return -1;
136
7f93a861
VG
137 if (find_frame_rate_index(s) < 0) {
138 if (s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
41ed7ab4 139 av_log(avctx, AV_LOG_ERROR, "MPEG-1/2 does not support %d/%d fps\n",
7f93a861 140 avctx->time_base.den, avctx->time_base.num);
a6bc5731 141 return -1;
7f93a861
VG
142 } else {
143 av_log(avctx, AV_LOG_INFO,
41ed7ab4 144 "MPEG-1/2 does not support %d/%d fps, there may be AV sync issues\n",
7f93a861 145 avctx->time_base.den, avctx->time_base.num);
a6bc5731
AJ
146 }
147 }
148
7f93a861
VG
149 if (avctx->profile == FF_PROFILE_UNKNOWN) {
150 if (avctx->level != FF_LEVEL_UNKNOWN) {
a6bc5731
AJ
151 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
152 return -1;
153 }
7f93a861
VG
154 /* Main or 4:2:2 */
155 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
a6bc5731
AJ
156 }
157
7f93a861
VG
158 if (avctx->level == FF_LEVEL_UNKNOWN) {
159 if (avctx->profile == 0) { /* 4:2:2 */
160 if (avctx->width <= 720 && avctx->height <= 608)
161 avctx->level = 5; /* Main */
162 else
163 avctx->level = 2; /* High */
164 } else {
165 if (avctx->profile != 1 && s->chroma_format != CHROMA_420) {
166 av_log(avctx, AV_LOG_ERROR,
167 "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
a6bc5731
AJ
168 return -1;
169 }
7f93a861
VG
170 if (avctx->width <= 720 && avctx->height <= 576)
171 avctx->level = 8; /* Main */
172 else if (avctx->width <= 1440)
173 avctx->level = 6; /* High 1440 */
174 else
175 avctx->level = 4; /* High */
a6bc5731
AJ
176 }
177 }
178
2c5e1efc 179 if (s->drop_frame_timecode && s->frame_rate_index != 4) {
7f93a861
VG
180 av_log(avctx, AV_LOG_ERROR,
181 "Drop frame time code only allowed with 1001/30000 fps\n");
a6bc5731
AJ
182 return -1;
183 }
184
d7496153
VG
185#if FF_API_PRIVATE_OPT
186FF_DISABLE_DEPRECATION_WARNINGS
187 if (avctx->timecode_frame_start)
188 s->timecode_frame_start = avctx->timecode_frame_start;
189FF_ENABLE_DEPRECATION_WARNINGS
190#endif
191
a6bc5731
AJ
192 return 0;
193}
194
195static void put_header(MpegEncContext *s, int header)
196{
9f51c682 197 avpriv_align_put_bits(&s->pb);
7f93a861 198 put_bits(&s->pb, 16, header >> 16);
6647ab80 199 put_sbits(&s->pb, 16, header);
a6bc5731
AJ
200}
201
202/* put sequence header if needed */
203static void mpeg1_encode_sequence_header(MpegEncContext *s)
204{
7f93a861
VG
205 unsigned int vbv_buffer_size, fps, v;
206 int i, constraint_parameter_flag;
207 uint64_t time_code;
208 float best_aspect_error = 1E10;
209 float aspect_ratio = av_q2d(s->avctx->sample_aspect_ratio);
a6bc5731 210
7f93a861
VG
211 if (aspect_ratio == 0.0)
212 aspect_ratio = 1.0; // pixel aspect 1.1 (VGA)
a6bc5731 213
f6774f90 214 if (s->current_picture.f->key_frame) {
7f93a861 215 AVRational framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
a6bc5731 216
41ed7ab4 217 /* MPEG-1 header repeated every GOP */
7f93a861 218 put_header(s, SEQ_START_CODE);
a6bc5731 219
7f93a861
VG
220 put_sbits(&s->pb, 12, s->width);
221 put_sbits(&s->pb, 12, s->height);
a6bc5731 222
7f93a861
VG
223 for (i = 1; i < 15; i++) {
224 float error = aspect_ratio;
225 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
226 error -= 1.0 / ff_mpeg1_aspect[i];
227 else
228 error -= av_q2d(ff_mpeg2_aspect[i]) * s->height / s->width;
a6bc5731 229
7f93a861 230 error = FFABS(error);
a6bc5731 231
7f93a861
VG
232 if (error < best_aspect_error) {
233 best_aspect_error = error;
234 s->aspect_ratio_info = i;
a6bc5731 235 }
7f93a861 236 }
a6bc5731 237
7f93a861
VG
238 put_bits(&s->pb, 4, s->aspect_ratio_info);
239 put_bits(&s->pb, 4, s->frame_rate_index);
a6bc5731 240
7f93a861
VG
241 if (s->avctx->rc_max_rate) {
242 v = (s->avctx->rc_max_rate + 399) / 400;
243 if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
244 v = 0x3ffff;
245 } else {
246 v = 0x3FFFF;
247 }
a6bc5731 248
7f93a861
VG
249 if (s->avctx->rc_buffer_size)
250 vbv_buffer_size = s->avctx->rc_buffer_size;
251 else
252 /* VBV calculation: Scaled so that a VCD has the proper
253 * VBV size of 40 kilobytes */
254 vbv_buffer_size = ((20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
255 vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
256
257 put_sbits(&s->pb, 18, v);
258 put_bits(&s->pb, 1, 1); // marker
259 put_sbits(&s->pb, 10, vbv_buffer_size);
260
261 constraint_parameter_flag =
262 s->width <= 768 &&
263 s->height <= 576 &&
264 s->mb_width * s->mb_height <= 396 &&
265 s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den &&
266 framerate.num <= framerate.den * 30 &&
267 s->avctx->me_range &&
268 s->avctx->me_range < 128 &&
269 vbv_buffer_size <= 20 &&
270 v <= 1856000 / 400 &&
271 s->codec_id == AV_CODEC_ID_MPEG1VIDEO;
272
273 put_bits(&s->pb, 1, constraint_parameter_flag);
274
275 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
276 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
277
278 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
279 put_header(s, EXT_START_CODE);
280 put_bits(&s->pb, 4, 1); // seq ext
281
282 put_bits(&s->pb, 1, s->avctx->profile == 0); // escx 1 for 4:2:2 profile
283
284 put_bits(&s->pb, 3, s->avctx->profile); // profile
285 put_bits(&s->pb, 4, s->avctx->level); // level
286
287 put_bits(&s->pb, 1, s->progressive_sequence);
288 put_bits(&s->pb, 2, s->chroma_format);
289 put_bits(&s->pb, 2, s->width >> 12);
290 put_bits(&s->pb, 2, s->height >> 12);
291 put_bits(&s->pb, 12, v >> 18); // bitrate ext
292 put_bits(&s->pb, 1, 1); // marker
293 put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
294 put_bits(&s->pb, 1, s->low_delay);
295 put_bits(&s->pb, 2, 0); // frame_rate_ext_n
296 put_bits(&s->pb, 5, 0); // frame_rate_ext_d
8a067941
MAA
297
298 put_header(s, EXT_START_CODE);
299 put_bits(&s->pb, 4, 2); // sequence display extension
4c0588b4 300 put_bits(&s->pb, 3, 5); // video_format: 5 is unspecified
8a067941
MAA
301 put_bits(&s->pb, 1, 1); // colour_description
302 put_bits(&s->pb, 8, s->avctx->color_primaries); // colour_primaries
303 put_bits(&s->pb, 8, s->avctx->color_trc); // transfer_characteristics
304 put_bits(&s->pb, 8, s->avctx->colorspace); // matrix_coefficients
305 put_bits(&s->pb, 14, s->width); // display_horizontal_size
306 put_bits(&s->pb, 1, 1); // marker_bit
307 put_bits(&s->pb, 14, s->height); // display_vertical_size
308 put_bits(&s->pb, 3, 0); // remaining 3 bits are zero padding
7f93a861 309 }
a6bc5731 310
7f93a861
VG
311 put_header(s, GOP_START_CODE);
312 put_bits(&s->pb, 1, s->drop_frame_timecode); // drop frame flag
313 /* time code: we must convert from the real frame rate to a
314 * fake MPEG frame rate in case of low frame rate */
315 fps = (framerate.num + framerate.den / 2) / framerate.den;
f6774f90 316 time_code = s->current_picture_ptr->f->coded_picture_number +
d7496153 317 s->timecode_frame_start;
7f93a861 318
f6774f90 319 s->gop_picture_number = s->current_picture_ptr->f->coded_picture_number;
7f93a861
VG
320 if (s->drop_frame_timecode) {
321 /* only works for NTSC 29.97 */
322 int d = time_code / 17982;
323 int m = time_code % 17982;
324 /* not needed since -2,-1 / 1798 in C returns 0 */
325 // if (m < 2)
326 // m += 2;
327 time_code += 18 * d + 2 * ((m - 2) / 1798);
a6bc5731 328 }
7f93a861
VG
329 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
330 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
331 put_bits(&s->pb, 1, 1);
332 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
333 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
5e555f93 334 put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) || s->intra_only);
7f93a861
VG
335 put_bits(&s->pb, 1, 0); // broken link
336 }
a6bc5731
AJ
337}
338
7f93a861
VG
339static inline void encode_mb_skip_run(MpegEncContext *s, int run)
340{
a6bc5731
AJ
341 while (run >= 33) {
342 put_bits(&s->pb, 11, 0x008);
343 run -= 33;
344 }
eaa7557c
AJ
345 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
346 ff_mpeg12_mbAddrIncrTable[run][0]);
a6bc5731
AJ
347}
348
349static av_always_inline void put_qscale(MpegEncContext *s)
350{
7f93a861
VG
351 if (s->q_scale_type) {
352 assert(s->qscale >= 1 && s->qscale <= 12);
a6bc5731 353 put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
7f93a861 354 } else {
a6bc5731
AJ
355 put_bits(&s->pb, 5, s->qscale);
356 }
357}
358
7f93a861
VG
359void ff_mpeg1_encode_slice_header(MpegEncContext *s)
360{
4410adf1
MF
361 if (s->height > 2800) {
362 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
7f93a861
VG
363 /* slice_vertical_position_extension */
364 put_bits(&s->pb, 3, s->mb_y >> 7);
4410adf1 365 } else {
7a38c114 366 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
4410adf1 367 }
a6bc5731 368 put_qscale(s);
7f93a861
VG
369 /* slice extra information */
370 put_bits(&s->pb, 1, 0);
a6bc5731
AJ
371}
372
d6c8dcb8 373void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
a6bc5731 374{
3d1d1753 375 AVFrameSideData *side_data;
a6bc5731
AJ
376 mpeg1_encode_sequence_header(s);
377
41ed7ab4 378 /* MPEG-1 picture header */
a6bc5731
AJ
379 put_header(s, PICTURE_START_CODE);
380 /* temporal reference */
381
382 // RAL: s->picture_number instead of s->fake_picture_number
7f93a861
VG
383 put_bits(&s->pb, 10,
384 (s->picture_number - s->gop_picture_number) & 0x3ff);
a6bc5731
AJ
385 put_bits(&s->pb, 3, s->pict_type);
386
7f93a861
VG
387 s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8;
388 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
a6bc5731 389
7f93a861
VG
390 // RAL: Forward f_code also needed for B-frames
391 if (s->pict_type == AV_PICTURE_TYPE_P ||
392 s->pict_type == AV_PICTURE_TYPE_B) {
393 put_bits(&s->pb, 1, 0); /* half pel coordinates */
394 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
395 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
a6bc5731 396 else
7f93a861 397 put_bits(&s->pb, 3, 7); /* forward_f_code */
a6bc5731
AJ
398 }
399
7f93a861 400 // RAL: Backward f_code necessary for B-frames
975a1447 401 if (s->pict_type == AV_PICTURE_TYPE_B) {
7f93a861
VG
402 put_bits(&s->pb, 1, 0); /* half pel coordinates */
403 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
404 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
a6bc5731 405 else
7f93a861 406 put_bits(&s->pb, 3, 7); /* backward_f_code */
a6bc5731
AJ
407 }
408
7f93a861 409 put_bits(&s->pb, 1, 0); /* extra bit picture */
a6bc5731
AJ
410
411 s->frame_pred_frame_dct = 1;
7f93a861 412 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
a6bc5731 413 put_header(s, EXT_START_CODE);
7f93a861
VG
414 put_bits(&s->pb, 4, 8); /* pic ext */
415 if (s->pict_type == AV_PICTURE_TYPE_P ||
416 s->pict_type == AV_PICTURE_TYPE_B) {
a6bc5731
AJ
417 put_bits(&s->pb, 4, s->f_code);
418 put_bits(&s->pb, 4, s->f_code);
7f93a861 419 } else {
a6bc5731
AJ
420 put_bits(&s->pb, 8, 255);
421 }
975a1447 422 if (s->pict_type == AV_PICTURE_TYPE_B) {
a6bc5731
AJ
423 put_bits(&s->pb, 4, s->b_code);
424 put_bits(&s->pb, 4, s->b_code);
7f93a861 425 } else {
a6bc5731
AJ
426 put_bits(&s->pb, 8, 255);
427 }
428 put_bits(&s->pb, 2, s->intra_dc_precision);
429
430 assert(s->picture_structure == PICT_FRAME);
431 put_bits(&s->pb, 2, s->picture_structure);
7f93a861
VG
432 if (s->progressive_sequence)
433 put_bits(&s->pb, 1, 0); /* no repeat */
434 else
f6774f90 435 put_bits(&s->pb, 1, s->current_picture_ptr->f->top_field_first);
7f93a861 436 /* XXX: optimize the generation of this flag with entropy measures */
a6bc5731
AJ
437 s->frame_pred_frame_dct = s->progressive_sequence;
438
439 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
440 put_bits(&s->pb, 1, s->concealment_motion_vectors);
441 put_bits(&s->pb, 1, s->q_scale_type);
442 put_bits(&s->pb, 1, s->intra_vlc_format);
443 put_bits(&s->pb, 1, s->alternate_scan);
444 put_bits(&s->pb, 1, s->repeat_first_field);
445 s->progressive_frame = s->progressive_sequence;
7f93a861
VG
446 /* chroma_420_type */
447 put_bits(&s->pb, 1, s->chroma_format ==
448 CHROMA_420 ? s->progressive_frame : 0);
a6bc5731 449 put_bits(&s->pb, 1, s->progressive_frame);
7f93a861 450 put_bits(&s->pb, 1, 0); /* composite_display_flag */
a6bc5731 451 }
aed79007 452 if (s->scan_offset) {
a6bc5731
AJ
453 int i;
454
455 put_header(s, USER_START_CODE);
7f93a861 456 for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
a6bc5731 457 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
a6bc5731 458 }
f6774f90 459 side_data = av_frame_get_side_data(s->current_picture_ptr->f,
3d1d1753
VG
460 AV_FRAME_DATA_STEREO3D);
461 if (side_data) {
462 AVStereo3D *stereo = (AVStereo3D *)side_data->data;
463 uint8_t fpa_type;
464
465 switch (stereo->type) {
466 case AV_STEREO3D_SIDEBYSIDE:
467 fpa_type = 0x03;
468 break;
469 case AV_STEREO3D_TOPBOTTOM:
470 fpa_type = 0x04;
471 break;
472 case AV_STEREO3D_2D:
473 fpa_type = 0x08;
474 break;
475 case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
476 fpa_type = 0x23;
477 break;
478 default:
479 fpa_type = 0;
480 break;
481 }
482
483 if (fpa_type != 0) {
484 put_header(s, USER_START_CODE);
485 put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier
486 put_bits(&s->pb, 8, 'P');
487 put_bits(&s->pb, 8, '3');
488 put_bits(&s->pb, 8, 'D');
489 put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
490
491 put_bits(&s->pb, 1, 1); // reserved_bit
492 put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
493 put_bits(&s->pb, 8, 0x04); // reserved_data[0]
494 put_bits(&s->pb, 8, 0xFF); // reserved_data[1]
495 }
496 }
a6bc5731 497
7f93a861 498 s->mb_y = 0;
a6bc5731
AJ
499 ff_mpeg1_encode_slice_header(s);
500}
501
502static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
503 int has_mv, int field_motion)
504{
505 put_bits(&s->pb, n, bits);
506 if (!s->frame_pred_frame_dct) {
507 if (has_mv)
7f93a861
VG
508 /* motion_type: frame/field */
509 put_bits(&s->pb, 2, 2 - field_motion);
a6bc5731
AJ
510 put_bits(&s->pb, 1, s->interlaced_dct);
511 }
512}
513
66f09105
VG
514// RAL: Parameter added: f_or_b_code
515static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
516{
517 if (val == 0) {
518 /* zero vector */
519 put_bits(&s->pb,
520 ff_mpeg12_mbMotionVectorTable[0][1],
521 ff_mpeg12_mbMotionVectorTable[0][0]);
522 } else {
523 int code, sign, bits;
524 int bit_size = f_or_b_code - 1;
7f93a861 525 int range = 1 << bit_size;
66f09105
VG
526 /* modulo encoding */
527 val = sign_extend(val, 5 + bit_size);
528
529 if (val >= 0) {
530 val--;
531 code = (val >> bit_size) + 1;
532 bits = val & (range - 1);
533 sign = 0;
534 } else {
535 val = -val;
536 val--;
537 code = (val >> bit_size) + 1;
538 bits = val & (range - 1);
539 sign = 1;
540 }
541
542 assert(code > 0 && code <= 16);
543
544 put_bits(&s->pb,
545 ff_mpeg12_mbMotionVectorTable[code][1],
546 ff_mpeg12_mbMotionVectorTable[code][0]);
547
548 put_bits(&s->pb, 1, sign);
7f93a861 549 if (bit_size > 0)
66f09105 550 put_bits(&s->pb, bit_size, bits);
66f09105
VG
551 }
552}
553
554static inline void encode_dc(MpegEncContext *s, int diff, int component)
555{
37b3361e
VG
556 unsigned int diff_u = diff + 255;
557 if (diff_u >= 511) {
66f09105
VG
558 int index;
559
7f93a861
VG
560 if (diff < 0) {
561 index = av_log2_16bit(-2 * diff);
66f09105 562 diff--;
7f93a861
VG
563 } else {
564 index = av_log2_16bit(2 * diff);
66f09105 565 }
7f93a861
VG
566 if (component == 0)
567 put_bits(&s->pb,
568 ff_mpeg12_vlc_dc_lum_bits[index] + index,
569 (ff_mpeg12_vlc_dc_lum_code[index] << index) +
570 (diff & ((1 << index) - 1)));
571 else
572 put_bits(&s->pb,
573 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
574 (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
575 (diff & ((1 << index) - 1)));
66f09105 576 } else {
7f93a861
VG
577 if (component == 0)
578 put_bits(&s->pb,
579 mpeg1_lum_dc_uni[diff + 255] & 0xFF,
580 mpeg1_lum_dc_uni[diff + 255] >> 8);
581 else
582 put_bits(&s->pb,
583 mpeg1_chr_dc_uni[diff + 255] & 0xFF,
584 mpeg1_chr_dc_uni[diff + 255] >> 8);
66f09105 585 }
66f09105
VG
586}
587
7f93a861 588static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
66f09105
VG
589{
590 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
591 int code, component;
592 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
593
594 last_index = s->block_last_index[n];
595
596 /* DC coef */
597 if (s->mb_intra) {
7f93a861
VG
598 component = (n <= 3 ? 0 : (n & 1) + 1);
599 dc = block[0]; /* overflow is impossible */
600 diff = dc - s->last_dc[component];
66f09105
VG
601 encode_dc(s, diff, component);
602 s->last_dc[component] = dc;
603 i = 1;
604 if (s->intra_vlc_format)
605 table_vlc = ff_rl_mpeg2.table_vlc;
606 } else {
7f93a861
VG
607 /* encode the first coefficient: needs to be done here because
608 * it is handled slightly differently */
66f09105
VG
609 level = block[0];
610 if (abs(level) == 1) {
7f93a861
VG
611 code = ((uint32_t)level >> 31); /* the sign bit */
612 put_bits(&s->pb, 2, code | 0x02);
613 i = 1;
66f09105 614 } else {
7f93a861 615 i = 0;
66f09105
VG
616 last_non_zero = -1;
617 goto next_coef;
618 }
619 }
620
621 /* now quantify & encode AC coefs */
622 last_non_zero = i - 1;
623
7f93a861
VG
624 for (; i <= last_index; i++) {
625 j = s->intra_scantable.permutated[i];
66f09105 626 level = block[j];
7f93a861
VG
627
628next_coef:
66f09105
VG
629 /* encode using VLC */
630 if (level != 0) {
631 run = i - last_non_zero - 1;
632
7f93a861 633 alevel = level;
66f09105 634 MASK_ABS(sign, alevel);
7f93a861 635 sign &= 1;
66f09105 636
7f93a861
VG
637 if (alevel <= mpeg1_max_level[0][run]) {
638 code = mpeg1_index_run[0][run] + alevel - 1;
639 /* store the VLC & sign at once */
640 put_bits(&s->pb, table_vlc[code][1] + 1,
641 (table_vlc[code][0] << 1) + sign);
66f09105
VG
642 } else {
643 /* escape seems to be pretty rare <5% so I do not optimize it */
644 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
645 /* escape: only clip in this case */
646 put_bits(&s->pb, 6, run);
7f93a861 647 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
66f09105
VG
648 if (alevel < 128) {
649 put_sbits(&s->pb, 8, level);
650 } else {
7f93a861 651 if (level < 0)
66f09105 652 put_bits(&s->pb, 16, 0x8001 + level + 255);
7f93a861 653 else
66f09105 654 put_sbits(&s->pb, 16, level);
66f09105 655 }
7f93a861 656 } else {
66f09105
VG
657 put_sbits(&s->pb, 12, level);
658 }
659 }
660 last_non_zero = i;
661 }
662 }
663 /* end of block */
664 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
665}
666
a6bc5731 667static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
fe27aeae 668 int16_t block[8][64],
7f93a861
VG
669 int motion_x, int motion_y,
670 int mb_block_count)
a6bc5731
AJ
671{
672 int i, cbp;
7f93a861
VG
673 const int mb_x = s->mb_x;
674 const int mb_y = s->mb_y;
675 const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
a6bc5731
AJ
676
677 /* compute cbp */
678 cbp = 0;
7f93a861 679 for (i = 0; i < mb_block_count; i++)
a6bc5731
AJ
680 if (s->block_last_index[i] >= 0)
681 cbp |= 1 << (mb_block_count - 1 - i);
a6bc5731
AJ
682
683 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
7f93a861
VG
684 (mb_x != s->mb_width - 1 ||
685 (mb_y != s->mb_height - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
975a1447 686 ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
7f93a861
VG
687 (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir &&
688 (((s->mv_dir & MV_DIR_FORWARD)
689 ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) |
690 (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
691 ((s->mv_dir & MV_DIR_BACKWARD)
692 ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) |
693 (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
a6bc5731
AJ
694 s->mb_skip_run++;
695 s->qscale -= s->dquant;
696 s->skip_count++;
697 s->misc_bits++;
698 s->last_bits++;
7f93a861
VG
699 if (s->pict_type == AV_PICTURE_TYPE_P) {
700 s->last_mv[0][0][0] =
701 s->last_mv[0][0][1] =
702 s->last_mv[0][1][0] =
703 s->last_mv[0][1][1] = 0;
a6bc5731
AJ
704 }
705 } else {
7f93a861 706 if (first_mb) {
a6bc5731
AJ
707 assert(s->mb_skip_run == 0);
708 encode_mb_skip_run(s, s->mb_x);
7f93a861 709 } else {
a6bc5731
AJ
710 encode_mb_skip_run(s, s->mb_skip_run);
711 }
712
975a1447 713 if (s->pict_type == AV_PICTURE_TYPE_I) {
7f93a861
VG
714 if (s->dquant && cbp) {
715 /* macroblock_type: macroblock_quant = 1 */
716 put_mb_modes(s, 2, 1, 0, 0);
a6bc5731 717 put_qscale(s);
7f93a861
VG
718 } else {
719 /* macroblock_type: macroblock_quant = 0 */
720 put_mb_modes(s, 1, 1, 0, 0);
a6bc5731
AJ
721 s->qscale -= s->dquant;
722 }
7f93a861 723 s->misc_bits += get_bits_diff(s);
a6bc5731
AJ
724 s->i_count++;
725 } else if (s->mb_intra) {
7f93a861 726 if (s->dquant && cbp) {
a6bc5731
AJ
727 put_mb_modes(s, 6, 0x01, 0, 0);
728 put_qscale(s);
7f93a861 729 } else {
a6bc5731
AJ
730 put_mb_modes(s, 5, 0x03, 0, 0);
731 s->qscale -= s->dquant;
732 }
7f93a861 733 s->misc_bits += get_bits_diff(s);
a6bc5731
AJ
734 s->i_count++;
735 memset(s->last_mv, 0, sizeof(s->last_mv));
975a1447 736 } else if (s->pict_type == AV_PICTURE_TYPE_P) {
7f93a861 737 if (s->mv_type == MV_TYPE_16X16) {
a6bc5731 738 if (cbp != 0) {
7f93a861
VG
739 if ((motion_x | motion_y) == 0) {
740 if (s->dquant) {
741 /* macroblock_pattern & quant */
742 put_mb_modes(s, 5, 1, 0, 0);
a6bc5731 743 put_qscale(s);
7f93a861
VG
744 } else {
745 /* macroblock_pattern only */
746 put_mb_modes(s, 2, 1, 0, 0);
a6bc5731 747 }
7f93a861 748 s->misc_bits += get_bits_diff(s);
a6bc5731 749 } else {
7f93a861
VG
750 if (s->dquant) {
751 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
a6bc5731 752 put_qscale(s);
7f93a861
VG
753 } else {
754 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
a6bc5731 755 }
7f93a861
VG
756 s->misc_bits += get_bits_diff(s);
757 // RAL: f_code parameter added
758 mpeg1_encode_motion(s,
759 motion_x - s->last_mv[0][0][0],
760 s->f_code);
761 // RAL: f_code parameter added
762 mpeg1_encode_motion(s,
763 motion_y - s->last_mv[0][0][1],
764 s->f_code);
765 s->mv_bits += get_bits_diff(s);
a6bc5731
AJ
766 }
767 } else {
7f93a861 768 put_bits(&s->pb, 3, 1); /* motion only */
a6bc5731 769 if (!s->frame_pred_frame_dct)
7f93a861
VG
770 put_bits(&s->pb, 2, 2); /* motion_type: frame */
771 s->misc_bits += get_bits_diff(s);
772 // RAL: f_code parameter added
773 mpeg1_encode_motion(s,
774 motion_x - s->last_mv[0][0][0],
775 s->f_code);
776 // RAL: f_code parameter added
777 mpeg1_encode_motion(s,
778 motion_y - s->last_mv[0][0][1],
779 s->f_code);
780 s->qscale -= s->dquant;
781 s->mv_bits += get_bits_diff(s);
a6bc5731 782 }
7f93a861
VG
783 s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
784 s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y;
785 } else {
a6bc5731
AJ
786 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
787
788 if (cbp) {
7f93a861
VG
789 if (s->dquant) {
790 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
a6bc5731 791 put_qscale(s);
7f93a861
VG
792 } else {
793 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
a6bc5731
AJ
794 }
795 } else {
7f93a861
VG
796 put_bits(&s->pb, 3, 1); /* motion only */
797 put_bits(&s->pb, 2, 1); /* motion_type: field */
a6bc5731
AJ
798 s->qscale -= s->dquant;
799 }
7f93a861
VG
800 s->misc_bits += get_bits_diff(s);
801 for (i = 0; i < 2; i++) {
a6bc5731 802 put_bits(&s->pb, 1, s->field_select[0][i]);
7f93a861
VG
803 mpeg1_encode_motion(s,
804 s->mv[0][i][0] - s->last_mv[0][i][0],
805 s->f_code);
806 mpeg1_encode_motion(s,
807 s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
808 s->f_code);
809 s->last_mv[0][i][0] = s->mv[0][i][0];
810 s->last_mv[0][i][1] = 2 * s->mv[0][i][1];
a6bc5731 811 }
7f93a861 812 s->mv_bits += get_bits_diff(s);
a6bc5731 813 }
7f93a861 814 if (cbp) {
a6bc5731 815 if (s->chroma_y_shift) {
7f93a861
VG
816 put_bits(&s->pb,
817 ff_mpeg12_mbPatTable[cbp][1],
818 ff_mpeg12_mbPatTable[cbp][0]);
a6bc5731 819 } else {
7f93a861
VG
820 put_bits(&s->pb,
821 ff_mpeg12_mbPatTable[cbp >> 2][1],
822 ff_mpeg12_mbPatTable[cbp >> 2][0]);
6647ab80 823 put_sbits(&s->pb, 2, cbp);
a6bc5731
AJ
824 }
825 }
826 s->f_count++;
7f93a861
VG
827 } else {
828 if (s->mv_type == MV_TYPE_16X16) {
829 if (cbp) { // With coded bloc pattern
a6bc5731 830 if (s->dquant) {
7f93a861 831 if (s->mv_dir == MV_DIR_FORWARD)
a6bc5731
AJ
832 put_mb_modes(s, 6, 3, 1, 0);
833 else
7f93a861 834 put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0);
a6bc5731
AJ
835 put_qscale(s);
836 } else {
7f93a861 837 put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0);
a6bc5731 838 }
7f93a861
VG
839 } else { // No coded bloc pattern
840 put_bits(&s->pb, 5 - s->mv_dir, 2);
a6bc5731
AJ
841 if (!s->frame_pred_frame_dct)
842 put_bits(&s->pb, 2, 2); /* motion_type: frame */
843 s->qscale -= s->dquant;
844 }
845 s->misc_bits += get_bits_diff(s);
7f93a861
VG
846 if (s->mv_dir & MV_DIR_FORWARD) {
847 mpeg1_encode_motion(s,
848 s->mv[0][0][0] - s->last_mv[0][0][0],
849 s->f_code);
850 mpeg1_encode_motion(s,
851 s->mv[0][0][1] - s->last_mv[0][0][1],
852 s->f_code);
853 s->last_mv[0][0][0] =
854 s->last_mv[0][1][0] = s->mv[0][0][0];
855 s->last_mv[0][0][1] =
856 s->last_mv[0][1][1] = s->mv[0][0][1];
a6bc5731
AJ
857 s->f_count++;
858 }
7f93a861
VG
859 if (s->mv_dir & MV_DIR_BACKWARD) {
860 mpeg1_encode_motion(s,
861 s->mv[1][0][0] - s->last_mv[1][0][0],
862 s->b_code);
863 mpeg1_encode_motion(s,
864 s->mv[1][0][1] - s->last_mv[1][0][1],
865 s->b_code);
866 s->last_mv[1][0][0] =
867 s->last_mv[1][1][0] = s->mv[1][0][0];
868 s->last_mv[1][0][1] =
869 s->last_mv[1][1][1] = s->mv[1][0][1];
a6bc5731
AJ
870 s->b_count++;
871 }
7f93a861 872 } else {
a6bc5731
AJ
873 assert(s->mv_type == MV_TYPE_FIELD);
874 assert(!s->frame_pred_frame_dct);
7f93a861 875 if (cbp) { // With coded bloc pattern
a6bc5731 876 if (s->dquant) {
7f93a861 877 if (s->mv_dir == MV_DIR_FORWARD)
a6bc5731
AJ
878 put_mb_modes(s, 6, 3, 1, 1);
879 else
7f93a861 880 put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1);
a6bc5731
AJ
881 put_qscale(s);
882 } else {
7f93a861 883 put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1);
a6bc5731 884 }
7f93a861
VG
885 } else { // No coded bloc pattern
886 put_bits(&s->pb, 5 - s->mv_dir, 2);
887 put_bits(&s->pb, 2, 1); /* motion_type: field */
a6bc5731
AJ
888 s->qscale -= s->dquant;
889 }
890 s->misc_bits += get_bits_diff(s);
7f93a861
VG
891 if (s->mv_dir & MV_DIR_FORWARD) {
892 for (i = 0; i < 2; i++) {
a6bc5731 893 put_bits(&s->pb, 1, s->field_select[0][i]);
7f93a861
VG
894 mpeg1_encode_motion(s,
895 s->mv[0][i][0] - s->last_mv[0][i][0],
896 s->f_code);
897 mpeg1_encode_motion(s,
898 s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
899 s->f_code);
900 s->last_mv[0][i][0] = s->mv[0][i][0];
901 s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
a6bc5731
AJ
902 }
903 s->f_count++;
904 }
7f93a861
VG
905 if (s->mv_dir & MV_DIR_BACKWARD) {
906 for (i = 0; i < 2; i++) {
a6bc5731 907 put_bits(&s->pb, 1, s->field_select[1][i]);
7f93a861
VG
908 mpeg1_encode_motion(s,
909 s->mv[1][i][0] - s->last_mv[1][i][0],
910 s->b_code);
911 mpeg1_encode_motion(s,
912 s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1),
913 s->b_code);
914 s->last_mv[1][i][0] = s->mv[1][i][0];
915 s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
a6bc5731
AJ
916 }
917 s->b_count++;
918 }
919 }
920 s->mv_bits += get_bits_diff(s);
7f93a861 921 if (cbp) {
a6bc5731 922 if (s->chroma_y_shift) {
7f93a861
VG
923 put_bits(&s->pb,
924 ff_mpeg12_mbPatTable[cbp][1],
925 ff_mpeg12_mbPatTable[cbp][0]);
a6bc5731 926 } else {
7f93a861
VG
927 put_bits(&s->pb,
928 ff_mpeg12_mbPatTable[cbp >> 2][1],
929 ff_mpeg12_mbPatTable[cbp >> 2][0]);
6647ab80 930 put_sbits(&s->pb, 2, cbp);
a6bc5731
AJ
931 }
932 }
933 }
7f93a861
VG
934 for (i = 0; i < mb_block_count; i++)
935 if (cbp & (1 << (mb_block_count - 1 - i)))
a6bc5731 936 mpeg1_encode_block(s, block[i], i);
a6bc5731 937 s->mb_skip_run = 0;
7f93a861
VG
938 if (s->mb_intra)
939 s->i_tex_bits += get_bits_diff(s);
a6bc5731 940 else
7f93a861 941 s->p_tex_bits += get_bits_diff(s);
a6bc5731
AJ
942 }
943}
944
fe27aeae 945void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64],
7f93a861 946 int motion_x, int motion_y)
a6bc5731 947{
7f93a861
VG
948 if (s->chroma_format == CHROMA_420)
949 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
950 else
951 mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
a6bc5731
AJ
952}
953
6fee1b90 954av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
a6bc5731 955{
7f93a861 956 static int done = 0;
a6bc5731 957
eaa7557c 958 ff_mpeg12_common_init(s);
a6bc5731 959
7f93a861 960 if (!done) {
a6bc5731
AJ
961 int f_code;
962 int mv;
963 int i;
964
7f93a861 965 done = 1;
6f57375d
AK
966 ff_rl_init(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
967 ff_rl_init(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
a6bc5731 968
7f93a861
VG
969 for (i = 0; i < 64; i++) {
970 mpeg1_max_level[0][i] = ff_rl_mpeg1.max_level[0][i];
971 mpeg1_index_run[0][i] = ff_rl_mpeg1.index_run[0][i];
a6bc5731
AJ
972 }
973
eaa7557c 974 init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
7f93a861 975 if (s->intra_vlc_format)
eaa7557c 976 init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
a6bc5731
AJ
977
978 /* build unified dc encoding tables */
7f93a861
VG
979 for (i = -255; i < 256; i++) {
980 int adiff, index;
981 int bits, code;
982 int diff = i;
983
984 adiff = FFABS(diff);
985 if (diff < 0)
986 diff--;
987 index = av_log2(2 * adiff);
988
989 bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
990 code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
991 (diff & ((1 << index) - 1));
992 mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
993
994 bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
995 code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
996 (diff & ((1 << index) - 1));
997 mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
a6bc5731
AJ
998 }
999
7f93a861
VG
1000 for (f_code = 1; f_code <= MAX_FCODE; f_code++)
1001 for (mv = -MAX_MV; mv <= MAX_MV; mv++) {
a6bc5731
AJ
1002 int len;
1003
7f93a861
VG
1004 if (mv == 0) {
1005 len = ff_mpeg12_mbMotionVectorTable[0][1];
1006 } else {
e65ab9d9 1007 int val, bit_size, code;
a6bc5731
AJ
1008
1009 bit_size = f_code - 1;
a6bc5731 1010
7f93a861 1011 val = mv;
a6bc5731
AJ
1012 if (val < 0)
1013 val = -val;
1014 val--;
1015 code = (val >> bit_size) + 1;
7f93a861
VG
1016 if (code < 17)
1017 len = ff_mpeg12_mbMotionVectorTable[code][1] +
1018 1 + bit_size;
1019 else
1020 len = ff_mpeg12_mbMotionVectorTable[16][1] +
1021 2 + bit_size;
a6bc5731
AJ
1022 }
1023
7f93a861 1024 mv_penalty[f_code][mv + MAX_MV] = len;
a6bc5731 1025 }
a6bc5731
AJ
1026
1027
7f93a861
VG
1028 for (f_code = MAX_FCODE; f_code > 0; f_code--)
1029 for (mv = -(8 << f_code); mv < (8 << f_code); mv++)
1030 fcode_tab[mv + MAX_MV] = f_code;
a6bc5731 1031 }
7f93a861
VG
1032 s->me.mv_penalty = mv_penalty;
1033 s->fcode_tab = fcode_tab;
1034 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1035 s->min_qcoeff = -255;
1036 s->max_qcoeff = 255;
1037 } else {
1038 s->min_qcoeff = -2047;
1039 s->max_qcoeff = 2047;
a6bc5731
AJ
1040 }
1041 if (s->intra_vlc_format) {
7f93a861
VG
1042 s->intra_ac_vlc_length =
1043 s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len;
a6bc5731 1044 } else {
7f93a861
VG
1045 s->intra_ac_vlc_length =
1046 s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
a6bc5731 1047 }
7f93a861
VG
1048 s->inter_ac_vlc_length =
1049 s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
a6bc5731
AJ
1050}
1051
297d9cb3
AK
1052#define OFFSET(x) offsetof(MpegEncContext, x)
1053#define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
7f93a861
VG
1054#define COMMON_OPTS \
1055 { "intra_vlc", "Use MPEG-2 intra VLC table.", \
1056 OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1057 { "drop_frame_timecode", "Timecode is in drop frame format.", \
1058 OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1059 { "scan_offset", "Reserve space for SVCD scan offset user data.", \
d7496153
VG
1060 OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
1061 { "timecode_frame_start", "GOP timecode frame start number, in non-drop-frame format", \
1062 OFFSET(timecode_frame_start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VE}, \
88262ca8
AK
1063
1064static const AVOption mpeg1_options[] = {
1065 COMMON_OPTS
ed019b8e 1066 FF_MPV_COMMON_OPTS
88262ca8
AK
1067 { NULL },
1068};
1069
1070static const AVOption mpeg2_options[] = {
1071 COMMON_OPTS
7f93a861
VG
1072 { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1073 { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
ed019b8e 1074 FF_MPV_COMMON_OPTS
297d9cb3
AK
1075 { NULL },
1076};
1077
7f93a861
VG
1078#define mpeg12_class(x) \
1079static const AVClass mpeg ## x ## _class = { \
1080 .class_name = "mpeg" # x "video encoder", \
1081 .item_name = av_default_item_name, \
1082 .option = mpeg ## x ## _options, \
1083 .version = LIBAVUTIL_VERSION_INT, \
297d9cb3
AK
1084};
1085
1086mpeg12_class(1)
1087mpeg12_class(2)
1088
d36beb3f 1089AVCodec ff_mpeg1video_encoder = {
00c3b67b 1090 .name = "mpeg1video",
b2bed932 1091 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
00c3b67b 1092 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 1093 .id = AV_CODEC_ID_MPEG1VIDEO,
00c3b67b
MS
1094 .priv_data_size = sizeof(MpegEncContext),
1095 .init = encode_init,
835f798c
DB
1096 .encode2 = ff_mpv_encode_picture,
1097 .close = ff_mpv_encode_end,
eaa9b2e6 1098 .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
7f93a861
VG
1099 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1100 AV_PIX_FMT_NONE },
def97856 1101 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
00c3b67b 1102 .priv_class = &mpeg1_class,
a6bc5731
AJ
1103};
1104
d36beb3f 1105AVCodec ff_mpeg2video_encoder = {
00c3b67b 1106 .name = "mpeg2video",
b2bed932 1107 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
00c3b67b 1108 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 1109 .id = AV_CODEC_ID_MPEG2VIDEO,
00c3b67b
MS
1110 .priv_data_size = sizeof(MpegEncContext),
1111 .init = encode_init,
835f798c
DB
1112 .encode2 = ff_mpv_encode_picture,
1113 .close = ff_mpv_encode_end,
eaa9b2e6 1114 .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
7f93a861
VG
1115 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
1116 AV_PIX_FMT_YUV422P,
1117 AV_PIX_FMT_NONE },
def97856 1118 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
00c3b67b 1119 .priv_class = &mpeg2_class,
a6bc5731 1120};