]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpegvideo.c
kill one vector constant value load by the right combination of vec_splatX/vec_sl
[ffmpeg] / libavcodec / mpegvideo.c
index 89e333a040fbf029fe638223360a0c5a68c83478..8e4e4a20905dfcc9e8a3059e8918cdfe03012bf6 100644 (file)
 #include "avcodec.h"
 #include "dsputil.h"
 #include "mpegvideo.h"
+#include "mjpegenc.h"
+#include "msmpeg4.h"
 #include "faandct.h"
 #include <limits.h>
 
-#ifdef USE_FASTMEMCPY
-#include "libvo/fastmemcpy.h"
-#endif
-
 //#undef NDEBUG
 //#include <assert.h>
 
@@ -107,7 +105,7 @@ static const uint8_t ff_default_chroma_qscale_table[32]={
 };
 
 #ifdef CONFIG_ENCODERS
-static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
+static uint8_t default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
 static uint8_t default_fcode_tab[MAX_MV*2+1];
 
 enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
@@ -297,6 +295,9 @@ int DCT_common_init(MpegEncContext *s)
 #ifdef ARCH_POWERPC
     MPV_common_init_ppc(s);
 #endif
+#ifdef ARCH_BFIN
+    MPV_common_init_bfin(s);
+#endif
 
 #ifdef CONFIG_ENCODERS
     s->fast_dct_quantize= s->dct_quantize;
@@ -374,7 +375,7 @@ static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *sr
  * The pixels are allocated/set by calling get_buffer() if shared=0
  */
 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
-    const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
+    const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
     const int mb_array_size= s->mb_stride*s->mb_height;
     const int b8_array_size= s->b8_stride*s->mb_height*2;
     const int b4_array_size= s->b4_stride*s->mb_height*4;
@@ -443,11 +444,12 @@ static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
         CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
     }
 
-    //it might be nicer if the application would keep track of these but it would require a API change
+    /* It might be nicer if the application would keep track of these
+     * but it would require an API change. */
     memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
     s->prev_pict_types[0]= s->pict_type;
     if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
-        pic->age= INT_MAX; // skipped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
+        pic->age= INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
 
     return 0;
 fail: //for the CHECKED_ALLOCZ macro
@@ -494,7 +496,7 @@ static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
     CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*21*2); //(width + edge + align)*interlaced*MBsize*tolerance
     s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
 
-     //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
+     //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
     CHECKED_ALLOCZ(s->me.scratchpad,  (s->width+64)*4*16*2*sizeof(uint8_t))
     s->rd_scratchpad=   s->me.scratchpad;
     s->b_scratchpad=    s->me.scratchpad;
@@ -625,20 +627,11 @@ void MPV_decode_defaults(MpegEncContext *s){
 
 #ifdef CONFIG_ENCODERS
 static void MPV_encode_defaults(MpegEncContext *s){
-    static int done=0;
-
+    int i;
     MPV_common_defaults(s);
 
-    if(!done){
-        int i;
-        done=1;
-
-        default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
-        memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
-
-        for(i=-16; i<16; i++){
-            default_fcode_tab[i + MAX_MV]= 1;
-        }
+    for(i=-16; i<16; i++){
+        default_fcode_tab[i + MAX_MV]= 1;
     }
     s->me.mv_penalty= default_mv_penalty;
     s->fcode_tab= default_fcode_tab;
@@ -1015,6 +1008,7 @@ int MPV_encode_init(AVCodecContext *avctx)
     s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
     s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
     s->intra_vlc_format= !!(s->flags2 & CODEC_FLAG2_INTRA_VLC);
+    s->q_scale_type= !!(s->flags2 & CODEC_FLAG2_NON_LINEAR_QUANT);
 
     if(avctx->rc_max_rate && !avctx->rc_buffer_size){
         av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
@@ -1026,7 +1020,7 @@ int MPV_encode_init(AVCodecContext *avctx)
     }
 
     if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
-        av_log(avctx, AV_LOG_INFO, "bitrate below min bitrate\n");
+        av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
         return -1;
     }
 
@@ -1035,6 +1029,16 @@ int MPV_encode_init(AVCodecContext *avctx)
         return -1;
     }
 
+    if(avctx->rc_buffer_size && avctx->bit_rate*av_q2d(avctx->time_base) > avctx->rc_buffer_size){
+        av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
+        return -1;
+    }
+
+    if(avctx->bit_rate*av_q2d(avctx->time_base) > avctx->bit_rate_tolerance){
+        av_log(avctx, AV_LOG_ERROR, "bitrate tolerance too small for bitrate\n");
+        return -1;
+    }
+
     if(   s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
        && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
        && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
@@ -1115,6 +1119,17 @@ int MPV_encode_init(AVCodecContext *avctx)
         }
     }
 
+    if(s->q_scale_type == 1){
+        if(s->codec_id != CODEC_ID_MPEG2VIDEO){
+            av_log(avctx, AV_LOG_ERROR, "non linear quant is only available for mpeg2\n");
+            return -1;
+        }
+        if(avctx->qmax > 12){
+            av_log(avctx, AV_LOG_ERROR, "non linear quant only supports qmax <= 12 currently\n");
+            return -1;
+        }
+    }
+
     if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
        && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
        && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
@@ -1190,25 +1205,23 @@ int MPV_encode_init(AVCodecContext *avctx)
         s->rtp_mode= 1;
         break;
     case CODEC_ID_LJPEG:
-    case CODEC_ID_JPEGLS:
     case CODEC_ID_MJPEG:
         s->out_format = FMT_MJPEG;
         s->intra_only = 1; /* force intra only for jpeg */
-        s->mjpeg_write_tables = avctx->codec->id != CODEC_ID_JPEGLS;
-        s->mjpeg_data_only_frames = 0; /* write all the needed headers */
         s->mjpeg_vsample[0] = 2;
         s->mjpeg_vsample[1] = 2>>chroma_v_shift;
         s->mjpeg_vsample[2] = 2>>chroma_v_shift;
         s->mjpeg_hsample[0] = 2;
         s->mjpeg_hsample[1] = 2>>chroma_h_shift;
         s->mjpeg_hsample[2] = 2>>chroma_h_shift;
-        if (mjpeg_init(s) < 0)
+        if (!(ENABLE_MJPEG_ENCODER || ENABLE_LJPEG_ENCODER)
+            || ff_mjpeg_encode_init(s) < 0)
             return -1;
         avctx->delay=0;
         s->low_delay=1;
         break;
-#ifdef CONFIG_H261_ENCODER
     case CODEC_ID_H261:
+        if (!ENABLE_H261_ENCODER)  return -1;
         if (ff_h261_get_picture_format(s->width, s->height) < 0) {
             av_log(avctx, AV_LOG_ERROR, "The specified picture size of %dx%d is not valid for the H.261 codec.\nValid sizes are 176x144, 352x288\n", s->width, s->height);
             return -1;
@@ -1217,7 +1230,6 @@ int MPV_encode_init(AVCodecContext *avctx)
         avctx->delay=0;
         s->low_delay=1;
         break;
-#endif
     case CODEC_ID_H263:
         if (h263_get_picture_format(s->width, s->height) == 7) {
             av_log(avctx, AV_LOG_INFO, "The specified picture size of %dx%d is not valid for the H.263 codec.\nValid sizes are 128x96, 176x144, 352x288, 704x576, and 1408x1152. Try H.263+.\n", s->width, s->height);
@@ -1345,13 +1357,11 @@ int MPV_encode_init(AVCodecContext *avctx)
     ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
     ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
 
-#ifdef CONFIG_H261_ENCODER
-    if (s->out_format == FMT_H261)
+    if (ENABLE_H261_ENCODER && s->out_format == FMT_H261)
         ff_h261_encode_init(s);
-#endif
     if (s->out_format == FMT_H263)
         h263_encode_init(s);
-    if(s->msmpeg4_version)
+    if (ENABLE_MSMPEG4_ENCODER && s->msmpeg4_version)
         ff_msmpeg4_encode_init(s);
     if (s->out_format == FMT_MPEG1)
         ff_mpeg1_encode_init(s);
@@ -1398,8 +1408,8 @@ int MPV_encode_end(AVCodecContext *avctx)
     ff_rate_control_uninit(s);
 
     MPV_common_end(s);
-    if (s->out_format == FMT_MJPEG)
-        mjpeg_close(s);
+    if ((ENABLE_MJPEG_ENCODER || ENABLE_LJPEG_ENCODER) && s->out_format == FMT_MJPEG)
+        ff_mjpeg_encode_close(s);
 
     av_freep(&avctx->extradata);
 
@@ -1408,14 +1418,14 @@ int MPV_encode_end(AVCodecContext *avctx)
 
 #endif //CONFIG_ENCODERS
 
-void init_rl(RLTable *rl, int use_static)
+void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
 {
     int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
     uint8_t index_run[MAX_RUN+1];
     int last, run, level, start, end, i;
 
     /* If table is static, we can quit if rl->max_level[0] is not NULL */
-    if(use_static && rl->max_level[0])
+    if(static_store && rl->max_level[0])
         return;
 
     /* compute max_level[], max_run[] and index_run[] */
@@ -1441,18 +1451,18 @@ void init_rl(RLTable *rl, int use_static)
             if (run > max_run[level])
                 max_run[level] = run;
         }
-        if(use_static)
-            rl->max_level[last] = av_mallocz_static(MAX_RUN + 1);
+        if(static_store)
+            rl->max_level[last] = static_store[last];
         else
             rl->max_level[last] = av_malloc(MAX_RUN + 1);
         memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
-        if(use_static)
-            rl->max_run[last] = av_mallocz_static(MAX_LEVEL + 1);
+        if(static_store)
+            rl->max_run[last] = static_store[last] + MAX_RUN + 1;
         else
             rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
         memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
-        if(use_static)
-            rl->index_run[last] = av_mallocz_static(MAX_RUN + 1);
+        if(static_store)
+            rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
         else
             rl->index_run[last] = av_malloc(MAX_RUN + 1);
         memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
@@ -1627,7 +1637,7 @@ alloc:
     s->error_resilience= avctx->error_resilience;
 
     /* set dequantizer, we can't do it during init as it might change for mpeg4
-       and we can't do it in the header decode as init isnt called for mpeg4 there yet */
+       and we can't do it in the header decode as init is not called for mpeg4 there yet */
     if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
         s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
         s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
@@ -2544,8 +2554,8 @@ vbv_retry:
 
         MPV_frame_end(s);
 
-        if (s->out_format == FMT_MJPEG)
-            mjpeg_picture_trailer(s);
+        if (ENABLE_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
+            ff_mjpeg_encode_picture_trailer(s);
 
         if(avctx->rc_buffer_size){
             RateControlContext *rcc= &s->rc_context;
@@ -3054,11 +3064,9 @@ if(s->quarter_sample)
         pix_op[s->chroma_x_shift][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
         pix_op[s->chroma_x_shift][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
     }
-#if defined(CONFIG_H261_ENCODER) || defined(CONFIG_H261_DECODER)
-    if(s->out_format == FMT_H261){
+    if((ENABLE_H261_ENCODER || ENABLE_H261_DECODER) && s->out_format == FMT_H261){
         ff_h261_loop_filter(s);
     }
-#endif
 }
 
 /* apply one mpeg motion vector to the three components */
@@ -3549,7 +3557,7 @@ static inline void MPV_motion(MpegEncContext *s,
                         0, 0, 0,
                         ref_picture, pix_op, qpix_op,
                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
-        }else if(s->mspel){
+        }else if(ENABLE_WMV2 && s->mspel){
             ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
                         ref_picture, pix_op,
                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
@@ -4073,7 +4081,7 @@ static av_always_inline void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM b
                     }
                 }//fi gray
             }
-            else{
+            else if (ENABLE_WMV2) {
                 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
             }
         } else {
@@ -4578,13 +4586,17 @@ static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x,
     case CODEC_ID_MSMPEG4V2:
     case CODEC_ID_MSMPEG4V3:
     case CODEC_ID_WMV1:
-        msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
+        if (ENABLE_MSMPEG4_ENCODER)
+            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
+        break;
     case CODEC_ID_WMV2:
-         ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
-#ifdef CONFIG_H261_ENCODER
+        if (ENABLE_WMV2_ENCODER)
+            ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
+        break;
     case CODEC_ID_H261:
-        ff_h261_encode_mb(s, s->block, motion_x, motion_y); break;
-#endif
+        if (ENABLE_H261_ENCODER)
+            ff_h261_encode_mb(s, s->block, motion_x, motion_y);
+        break;
     case CODEC_ID_H263:
     case CODEC_ID_H263P:
     case CODEC_ID_FLV1:
@@ -4592,7 +4604,9 @@ static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x,
     case CODEC_ID_RV20:
         h263_encode_mb(s, s->block, motion_x, motion_y); break;
     case CODEC_ID_MJPEG:
-        mjpeg_encode_mb(s, s->block); break;
+        if (ENABLE_MJPEG_ENCODER)
+            ff_mjpeg_encode_mb(s, s->block);
+        break;
     default:
         assert(0);
     }
@@ -4892,8 +4906,8 @@ static void write_slice_end(MpegEncContext *s){
         }
 
         ff_mpeg4_stuffing(&s->pb);
-    }else if(s->out_format == FMT_MJPEG){
-        ff_mjpeg_stuffing(&s->pb);
+    }else if(ENABLE_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
+        ff_mjpeg_encode_stuffing(&s->pb);
     }
 
     align_put_bits(&s->pb);
@@ -4991,13 +5005,11 @@ static int encode_thread(AVCodecContext *c, void *arg){
             s->mb_y = mb_y;  // moved into loop, can get changed by H.261
             ff_update_block_index(s);
 
-#ifdef CONFIG_H261_ENCODER
-            if(s->codec_id == CODEC_ID_H261){
+            if(ENABLE_H261_ENCODER && s->codec_id == CODEC_ID_H261){
                 ff_h261_reorder_mb_index(s);
                 xy= s->mb_y*s->mb_stride + s->mb_x;
                 mb_type= s->mb_type[xy];
             }
-#endif
 
             /* write gob / video packet header  */
             if(s->rtp_mode){
@@ -5505,7 +5517,7 @@ static int encode_thread(AVCodecContext *c, void *arg){
     }
 
     //not beautiful here but we must write it before flushing so it has to be here
-    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
+    if (ENABLE_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
         msmpeg4_encode_ext_header(s);
 
     write_slice_end(s);
@@ -5761,28 +5773,24 @@ static int encode_picture(MpegEncContext *s, int picture_number)
     s->last_bits= put_bits_count(&s->pb);
     switch(s->out_format) {
     case FMT_MJPEG:
-        mjpeg_picture_header(s);
+        if (ENABLE_MJPEG_ENCODER)
+            ff_mjpeg_encode_picture_header(s);
         break;
-#ifdef CONFIG_H261_ENCODER
     case FMT_H261:
-        ff_h261_encode_picture_header(s, picture_number);
+        if (ENABLE_H261_ENCODER)
+            ff_h261_encode_picture_header(s, picture_number);
         break;
-#endif
     case FMT_H263:
-        if (s->codec_id == CODEC_ID_WMV2)
+        if (ENABLE_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2)
             ff_wmv2_encode_picture_header(s, picture_number);
-        else if (s->h263_msmpeg4)
+        else if (ENABLE_MSMPEG4_ENCODER && s->h263_msmpeg4)
             msmpeg4_encode_picture_header(s, picture_number);
         else if (s->h263_pred)
             mpeg4_encode_picture_header(s, picture_number);
-#ifdef CONFIG_RV10_ENCODER
-        else if (s->codec_id == CODEC_ID_RV10)
+        else if (ENABLE_RV10_ENCODER && s->codec_id == CODEC_ID_RV10)
             rv10_encode_picture_header(s, picture_number);
-#endif
-#ifdef CONFIG_RV20_ENCODER
-        else if (s->codec_id == CODEC_ID_RV20)
+        else if (ENABLE_RV20_ENCODER && s->codec_id == CODEC_ID_RV20)
             rv20_encode_picture_header(s, picture_number);
-#endif
         else if (s->codec_id == CODEC_ID_FLV1)
             ff_flv_encode_picture_header(s, picture_number);
         else
@@ -6932,15 +6940,4 @@ AVCodec wmv1_encoder = {
     .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
 };
 
-AVCodec mjpeg_encoder = {
-    "mjpeg",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_MJPEG,
-    sizeof(MpegEncContext),
-    MPV_encode_init,
-    MPV_encode_picture,
-    MPV_encode_end,
-    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, -1},
-};
-
 #endif //CONFIG_ENCODERS