LCOV - code coverage report
Current view: top level - libavcodec - mpeg12enc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 580 664 87.3 %
Date: 2017-12-17 16:07:53 Functions: 16 16 100.0 %

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

Generated by: LCOV version 1.13