]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/dv.c
Check available size before writing in decode_frame()
[ffmpeg] / libavcodec / dv.c
index 505c88d494e16d1c5e73e217f665fd556403ca53..8ba952ae62378ea415f7579f04f9593721e72325 100644 (file)
@@ -36,7 +36,7 @@
 #define ALT_BITSTREAM_READER
 #include "avcodec.h"
 #include "dsputil.h"
-#include "mpegvideo.h"
+#include "bitstream.h"
 #include "simple_idct.h"
 #include "dvdata.h"
 
@@ -75,12 +75,12 @@ static void* dv_anchor[DV_ANCHOR_SIZE];
 #endif
 
 /* XXX: also include quantization */
-static RL_VLC_ELEM *dv_rl_vlc;
+static RL_VLC_ELEM dv_rl_vlc[1184];
 /* VLC encoding lookup table */
 static struct dv_vlc_pair {
    uint32_t vlc;
    uint8_t  size;
-} (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL;
+} dv_vlc_map[DV_VLC_MAP_RUN_SIZE][DV_VLC_MAP_LEV_SIZE];
 
 static void dv_build_unquantize_tables(DVVideoContext *s, uint8_t* perm)
 {
@@ -107,7 +107,7 @@ static void dv_build_unquantize_tables(DVVideoContext *s, uint8_t* perm)
     }
 }
 
-static int dvvideo_init(AVCodecContext *avctx)
+static av_cold int dvvideo_init(AVCodecContext *avctx)
 {
     DVVideoContext *s = avctx->priv_data;
     DSPContext dsp;
@@ -123,10 +123,6 @@ static int dvvideo_init(AVCodecContext *avctx)
 
         done = 1;
 
-        dv_vlc_map = av_mallocz_static(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair));
-        if (!dv_vlc_map)
-            return -ENOMEM;
-
         /* dv_anchor lets each thread know its Id */
         for (i=0; i<DV_ANCHOR_SIZE; i++)
             dv_anchor[i] = (void*)(size_t)i;
@@ -154,10 +150,7 @@ static int dvvideo_init(AVCodecContext *avctx)
            to accelerate the parsing of partial codes */
         init_vlc(&dv_vlc, TEX_VLC_BITS, j,
                  new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
-
-        dv_rl_vlc = av_mallocz_static(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
-        if (!dv_rl_vlc)
-            return -ENOMEM;
+        assert(dv_vlc.table_size == 1184);
 
         for(i = 0; i < dv_vlc.table_size; i++){
             int code= dv_vlc.table[i][0];
@@ -232,7 +225,7 @@ static int dvvideo_init(AVCodecContext *avctx)
 
     /* 248DCT setup */
     s->fdct[1] = dsp.fdct248;
-    s->idct_put[1] = simple_idct248_put;  // FIXME: need to add it to DSP
+    s->idct_put[1] = ff_simple_idct248_put;  // FIXME: need to add it to DSP
     if(avctx->lowres){
         for (i=0; i<64; i++){
             int j= ff_zigzag248_direct[i];
@@ -278,11 +271,6 @@ static inline int get_bits_left(GetBitContext *s)
     return s->size_in_bits - get_bits_count(s);
 }
 
-static inline int get_bits_size(GetBitContext *s)
-{
-    return s->size_in_bits;
-}
-
 static inline int put_bits_left(PutBitContext* s)
 {
     return (s->buf_end - s->buf) * 8 - put_bits_count(s);
@@ -291,7 +279,7 @@ static inline int put_bits_left(PutBitContext* s)
 /* decode ac coefs */
 static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
 {
-    int last_index = get_bits_size(gb);
+    int last_index = gb->size_in_bits;
     const uint8_t *scan_table = mb->scan_table;
     const uint8_t *shift_table = mb->shift_table;
     const int *iweight_table = mb->iweight_table;
@@ -370,7 +358,7 @@ static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
 
 /* mb_x and mb_y are in units of 8 pixels */
 static inline void dv_decode_video_segment(DVVideoContext *s,
-                                           uint8_t *buf_ptr1,
+                                           const uint8_t *buf_ptr1,
                                            const uint16_t *mb_pos_ptr)
 {
     int quant, dc, dct_mode, class1, j;
@@ -379,11 +367,11 @@ static inline void dv_decode_video_segment(DVVideoContext *s,
     int c_offset;
     uint8_t *y_ptr;
     void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
-    uint8_t *buf_ptr;
+    const uint8_t *buf_ptr;
     PutBitContext pb, vs_pb;
     GetBitContext gb;
     BlockInfo mb_data[5 * 6], *mb, *mb1;
-    DECLARE_ALIGNED_8(DCTELEM, sblock[5*6][64]);
+    DECLARE_ALIGNED_16(DCTELEM, sblock[5*6][64]);
     DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]); /* allow some slack */
     DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]); /* allow some slack */
     const int log2_blocksize= 3-s->avctx->lowres;
@@ -845,7 +833,7 @@ static inline void dv_encode_video_segment(DVVideoContext *s,
     uint8_t*  data;
     uint8_t*  ptr;
     int       do_edge_wrap;
-    DECLARE_ALIGNED_8(DCTELEM, block[64]);
+    DECLARE_ALIGNED_16(DCTELEM, block[64]);
     EncBlockInfo  enc_blks[5*6];
     PutBitContext pbs[5*6];
     PutBitContext* pb;
@@ -853,7 +841,7 @@ static inline void dv_encode_video_segment(DVVideoContext *s,
     int       vs_bit_size = 0;
     int       qnos[5];
 
-    assert((((int)block) & 7) == 0);
+    assert((((int)block) & 15) == 0);
 
     enc_blk = &enc_blks[0];
     pb = &pbs[0];
@@ -1012,7 +1000,7 @@ static int dv_decode_mt(AVCodecContext *avctx, void* sl)
     return 0;
 }
 
-#ifdef CONFIG_ENCODERS
+#ifdef CONFIG_DVVIDEO_ENCODER
 static int dv_encode_mt(AVCodecContext *avctx, void* sl)
 {
     DVVideoContext *s = avctx->priv_data;
@@ -1038,7 +1026,7 @@ static int dv_encode_mt(AVCodecContext *avctx, void* sl)
    144000 bytes for PAL - or twice those for 50Mbps) */
 static int dvvideo_decode_frame(AVCodecContext *avctx,
                                  void *data, int *data_size,
-                                 uint8_t *buf, int buf_size)
+                                 const uint8_t *buf, int buf_size)
 {
     DVVideoContext *s = avctx->priv_data;
 
@@ -1053,6 +1041,7 @@ static int dvvideo_decode_frame(AVCodecContext *avctx,
     s->picture.key_frame = 1;
     s->picture.pict_type = FF_I_TYPE;
     avctx->pix_fmt = s->sys->pix_fmt;
+    avctx->time_base = (AVRational){s->sys->frame_rate_base, s->sys->frame_rate};
     avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
     if(avctx->get_buffer(avctx, &s->picture) < 0) {
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
@@ -1148,6 +1137,7 @@ static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c, ui
     return 5;
 }
 
+#ifdef CONFIG_DVVIDEO_ENCODER
 static void dv_format_frame(DVVideoContext* c, uint8_t* buf)
 {
     int chan, i, j, k;
@@ -1198,7 +1188,6 @@ static void dv_format_frame(DVVideoContext* c, uint8_t* buf)
 }
 
 
-#ifdef CONFIG_ENCODERS
 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
                                 void *data)
 {
@@ -1246,7 +1235,8 @@ AVCodec dvvideo_encoder = {
     sizeof(DVVideoContext),
     dvvideo_init,
     dvvideo_encode_frame,
-    .pix_fmts = (enum PixelFormat[]) {PIX_FMT_YUV411P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, -1},
+    .pix_fmts = (enum PixelFormat[]) {PIX_FMT_YUV411P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, PIX_FMT_NONE},
+    .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
 };
 #endif // CONFIG_DVVIDEO_ENCODER
 
@@ -1261,6 +1251,7 @@ AVCodec dvvideo_decoder = {
     dvvideo_close,
     dvvideo_decode_frame,
     CODEC_CAP_DR1,
-    NULL
+    NULL,
+    .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
 };
 #endif