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