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