]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mimic.c
options_table: Add some missing #includes to fix "make checkheaders".
[ffmpeg] / libavcodec / mimic.c
index 6fa8534eb28717e8c40e4cbb3d73559a0a73e039..4d81a37259495618d51e9712fe39480541be7e81 100644 (file)
@@ -1,21 +1,21 @@
 /*
- * Copyright (C) 2005  Ole André Vadla Ravnås <oleavr@gmail.com>
- * Copyright (C) 2008  Ramiro Polla <ramiro@lisha.ufsc.br>
+ * Copyright (C) 2005  Ole André Vadla Ravnås <oleavr@gmail.com>
+ * Copyright (C) 2008  Ramiro Polla
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <stdint.h>
 
 #include "avcodec.h"
-#include "bitstream.h"
+#include "internal.h"
+#include "get_bits.h"
+#include "bytestream.h"
 #include "dsputil.h"
+#include "thread.h"
 
 #define MIMIC_HEADER_SIZE   20
 
@@ -35,7 +38,7 @@ typedef struct {
     int             num_vblocks[3];
     int             num_hblocks[3];
 
-    uint8_t        *swap_buf;
+    void           *swap_buf;
     int             swap_buf_size;
 
     int             cur_index;
@@ -44,12 +47,16 @@ typedef struct {
     AVFrame         buf_ptrs    [16];
     AVPicture       flipped_ptrs[16];
 
-    DECLARE_ALIGNED_16(DCTELEM, dct_block[64]);
+    DECLARE_ALIGNED(16, DCTELEM, dct_block)[64];
 
     GetBitContext   gb;
     ScanTable       scantable;
     DSPContext      dsp;
-    VLC             vlc1;
+    VLC             vlc;
+
+    /* Kept in the context so multithreading can have a constant to read from */
+    int             next_cur_index;
+    int             next_prev_index;
 } MimicContext;
 
 static const uint32_t huffcodes[] = {
@@ -99,7 +106,7 @@ static const uint8_t col_zag[64] = {
     28, 21, 14,  7, 15, 22, 29, 36,
     43, 50, 57, 58, 51, 44, 37, 30,
     23, 31, 38, 45, 52, 59, 39, 46,
-    53, 60, 61, 54, 47, 55, 62, 63
+    53, 60, 61, 54, 47, 55, 62, 63,
 };
 
 static av_cold int mimic_decode_init(AVCodecContext *avctx)
@@ -109,18 +116,35 @@ static av_cold int mimic_decode_init(AVCodecContext *avctx)
     ctx->prev_index = 0;
     ctx->cur_index = 15;
 
-    if(init_vlc(&ctx->vlc1, 8, sizeof(huffbits)/sizeof(huffbits[0]),
+    if(init_vlc(&ctx->vlc, 11, FF_ARRAY_ELEMS(huffbits),
                  huffbits, 1, 1, huffcodes, 4, 4, 0)) {
         av_log(avctx, AV_LOG_ERROR, "error initializing vlc table\n");
         return -1;
     }
-    dsputil_init(&ctx->dsp, avctx);
+    ff_dsputil_init(&ctx->dsp, avctx);
     ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, col_zag);
 
     return 0;
 }
 
-const static int8_t vlcdec_lookup[9][64] = {
+static int mimic_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
+{
+    MimicContext *dst = avctx->priv_data, *src = avctx_from->priv_data;
+
+    if (avctx == avctx_from) return 0;
+
+    dst->cur_index  = src->next_cur_index;
+    dst->prev_index = src->next_prev_index;
+
+    memcpy(dst->buf_ptrs, src->buf_ptrs, sizeof(src->buf_ptrs));
+    memcpy(dst->flipped_ptrs, src->flipped_ptrs, sizeof(src->flipped_ptrs));
+
+    memset(&dst->buf_ptrs[dst->cur_index], 0, sizeof(AVFrame));
+
+    return 0;
+}
+
+static const int8_t vlcdec_lookup[9][64] = {
     {    0, },
     {   -1,   1, },
     {   -3,   3,   -2,   2, },
@@ -157,12 +181,12 @@ const static int8_t vlcdec_lookup[9][64] = {
        -67,  67,  -66,  66,  -65,  65,  -64,  64, },
 };
 
-static int vlc_decode_block(MimicContext *ctx, DCTELEM *block, int num_coeffs,
-                            int qscale)
+static int vlc_decode_block(MimicContext *ctx, int num_coeffs, int qscale)
 {
+    DCTELEM *block = ctx->dct_block;
     unsigned int pos;
 
-    memset(block, 0, 64 * sizeof(DCTELEM));
+    ctx->dsp.clear_block(block);
 
     block[0] = get_bits(&ctx->gb, 8) << 3;
 
@@ -171,7 +195,7 @@ static int vlc_decode_block(MimicContext *ctx, DCTELEM *block, int num_coeffs,
         int value;
         int coeff;
 
-        vlc = get_vlc2(&ctx->gb, ctx->vlc1.table, ctx->vlc1.bits, 4);
+        vlc = get_vlc2(&ctx->gb, ctx->vlc.table, ctx->vlc.bits, 3);
         if(!vlc) /* end-of-block code */
             return 1;
         if(vlc == -1)
@@ -186,7 +210,7 @@ static int vlc_decode_block(MimicContext *ctx, DCTELEM *block, int num_coeffs,
 
         value = get_bits(&ctx->gb, num_bits);
 
-        /* FFmpeg's IDCT behaves somewhat different from the original code, so
+        /* Libav's IDCT behaves somewhat different from the original code, so
          * a factor of 4 was added to the input */
 
         coeff = vlcdec_lookup[num_bits][value];
@@ -204,7 +228,7 @@ static int vlc_decode_block(MimicContext *ctx, DCTELEM *block, int num_coeffs,
 static int decode(MimicContext *ctx, int quality, int num_coeffs,
                   int is_iframe)
 {
-    int y, x, plane;
+    int y, x, plane, cur_row = 0;
 
     for(plane = 0; plane < 3; plane++) {
         const int is_chroma = !!plane;
@@ -213,7 +237,7 @@ static int decode(MimicContext *ctx, int quality, int num_coeffs,
         const uint8_t *src = ctx->flipped_ptrs[ctx->prev_index].data[plane];
         uint8_t       *dst = ctx->flipped_ptrs[ctx->cur_index ].data[plane];
 
-        for(y = 0 ; y < ctx->num_vblocks[plane] ; y++) {
+        for(y = 0; y < ctx->num_vblocks[plane]; y++) {
             for(x = 0; x < ctx->num_hblocks[plane]; x++) {
 
                 /* Check for a change condition in the current block.
@@ -227,8 +251,7 @@ static int decode(MimicContext *ctx, int quality, int num_coeffs,
                      * Chroma planes don't use backreferences. */
                     if(is_chroma || is_iframe || !get_bits1(&ctx->gb)) {
 
-                        if(!vlc_decode_block(ctx, ctx->dct_block,
-                                             num_coeffs, qscale))
+                        if(!vlc_decode_block(ctx, num_coeffs, qscale))
                             return 0;
                         ctx->dsp.idct_put(dst, stride, ctx->dct_block);
                     } else {
@@ -236,7 +259,8 @@ static int decode(MimicContext *ctx, int quality, int num_coeffs,
                         int index = (ctx->cur_index+backref)&15;
                         uint8_t *p = ctx->flipped_ptrs[index].data[0];
 
-                        if(p) {
+                        if (index != ctx->cur_index && p) {
+                            ff_thread_await_progress(&ctx->buf_ptrs[index], cur_row, 0);
                             p += src -
                                 ctx->flipped_ptrs[ctx->prev_index].data[plane];
                             ctx->dsp.put_pixels_tab[1][0](dst, p, stride, 8);
@@ -246,6 +270,7 @@ static int decode(MimicContext *ctx, int quality, int num_coeffs,
                         }
                     }
                 } else {
+                    ff_thread_await_progress(&ctx->buf_ptrs[ctx->prev_index], cur_row, 0);
                     ctx->dsp.put_pixels_tab[1][0](dst, src, stride, 8);
                 }
                 src += 8;
@@ -253,6 +278,8 @@ static int decode(MimicContext *ctx, int quality, int num_coeffs,
             }
             src += (stride - ctx->num_hblocks[plane])<<3;
             dst += (stride - ctx->num_hblocks[plane])<<3;
+
+            ff_thread_report_progress(&ctx->buf_ptrs[ctx->cur_index], cur_row++, 0);
         }
     }
 
@@ -269,37 +296,37 @@ static void prepare_avpic(MimicContext *ctx, AVPicture *dst, AVPicture *src)
     dst->data[0] = src->data[0]+( ctx->avctx->height    -1)*src->linesize[0];
     dst->data[1] = src->data[2]+((ctx->avctx->height>>1)-1)*src->linesize[2];
     dst->data[2] = src->data[1]+((ctx->avctx->height>>1)-1)*src->linesize[1];
-    for(i = 0 ; i < 3 ; i++)
+    for(i = 0; i < 3; i++)
         dst->linesize[i] = -src->linesize[i];
 }
 
 static int mimic_decode_frame(AVCodecContext *avctx, void *data,
-                              int *data_size, const uint8_t *buf, int buf_size)
+                              int *data_size, AVPacket *avpkt)
 {
+    const uint8_t *buf = avpkt->data;
+    int buf_size = avpkt->size;
     MimicContext *ctx = avctx->priv_data;
+    GetByteContext gb;
     int is_pframe;
     int width, height;
     int quality, num_coeffs;
     int swap_buf_size = buf_size - MIMIC_HEADER_SIZE;
+    int res;
 
-    /*
-     * Header structure:
-     *  uint16_t    I_dont_remember;
-     *  uint16_t    quality;
-     *  uint16_t    width;
-     *  uint16_t    height;
-     *  uint32_t    some_constant;
-     *  uint32_t    is_pframe;
-     *  uint32_t    num_coeffs;
-     */
-
-    if(buf_size < MIMIC_HEADER_SIZE) {
+    if (buf_size <= MIMIC_HEADER_SIZE) {
         av_log(avctx, AV_LOG_ERROR, "insufficient data\n");
         return -1;
     }
 
-    width  = AV_RL16(buf + 4);
-    height = AV_RL16(buf + 6);
+    bytestream2_init(&gb, buf, MIMIC_HEADER_SIZE);
+    bytestream2_skip(&gb, 2); /* some constant (always 256) */
+    quality    = bytestream2_get_le16u(&gb);
+    width      = bytestream2_get_le16u(&gb);
+    height     = bytestream2_get_le16u(&gb);
+    bytestream2_skip(&gb, 4); /* some constant */
+    is_pframe  = bytestream2_get_le32u(&gb);
+    num_coeffs = bytestream2_get_byteu(&gb);
+    bytestream2_skip(&gb, 3); /* some constant */
 
     if(!ctx->avctx) {
         int i;
@@ -314,7 +341,7 @@ static int mimic_decode_frame(AVCodecContext *avctx, void *data,
         avctx->width   = width;
         avctx->height  = height;
         avctx->pix_fmt = PIX_FMT_YUV420P;
-        for(i = 0 ; i < 3 ; i++) {
+        for(i = 0; i < 3; i++) {
             ctx->num_vblocks[i] = -((-height) >> (3 + !!i));
             ctx->num_hblocks[i] =     width   >> (3 + !!i) ;
         }
@@ -323,50 +350,54 @@ static int mimic_decode_frame(AVCodecContext *avctx, void *data,
         return -1;
     }
 
-    quality    = AV_RL16(buf + 2);
-    is_pframe  = AV_RL32(buf + 12);
-    num_coeffs = buf[16];
-
     if(is_pframe && !ctx->buf_ptrs[ctx->prev_index].data[0]) {
         av_log(avctx, AV_LOG_ERROR, "decoding must start with keyframe\n");
         return -1;
     }
 
     ctx->buf_ptrs[ctx->cur_index].reference = 1;
-    if(avctx->get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) {
+    ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? AV_PICTURE_TYPE_P:AV_PICTURE_TYPE_I;
+    if(ff_thread_get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) {
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return -1;
     }
 
+    ctx->next_prev_index = ctx->cur_index;
+    ctx->next_cur_index  = (ctx->cur_index - 1) & 15;
+
     prepare_avpic(ctx, &ctx->flipped_ptrs[ctx->cur_index],
                   (AVPicture*) &ctx->buf_ptrs[ctx->cur_index]);
 
-    ctx->swap_buf = av_fast_realloc(ctx->swap_buf, &ctx->swap_buf_size,
+    ff_thread_finish_setup(avctx);
+
+    av_fast_malloc(&ctx->swap_buf, &ctx->swap_buf_size,
                                  swap_buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
     if(!ctx->swap_buf)
-        return AVERROR_NOMEM;
+        return AVERROR(ENOMEM);
 
-    ctx->dsp.bswap_buf((uint32_t*)ctx->swap_buf,
+    ctx->dsp.bswap_buf(ctx->swap_buf,
                         (const uint32_t*) (buf + MIMIC_HEADER_SIZE),
                         swap_buf_size>>2);
     init_get_bits(&ctx->gb, ctx->swap_buf, swap_buf_size << 3);
 
-    if(!decode(ctx, quality, num_coeffs, !is_pframe)) {
-        avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
-        return -1;
+    res = decode(ctx, quality, num_coeffs, !is_pframe);
+    ff_thread_report_progress(&ctx->buf_ptrs[ctx->cur_index], INT_MAX, 0);
+    if (!res) {
+        if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
+            ff_thread_release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
+            return -1;
+        }
     }
 
-    ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? FF_P_TYPE:FF_I_TYPE;
     *(AVFrame*)data = ctx->buf_ptrs[ctx->cur_index];
     *data_size = sizeof(AVFrame);
 
-    ctx->prev_index = ctx->cur_index;
-    ctx->cur_index--;
-    ctx->cur_index &= 15;
+    ctx->prev_index = ctx->next_prev_index;
+    ctx->cur_index  = ctx->next_cur_index;
 
     /* Only release frames that aren't used for backreferences anymore */
     if(ctx->buf_ptrs[ctx->cur_index].data[0])
-        avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
+        ff_thread_release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
 
     return buf_size;
 }
@@ -377,22 +408,27 @@ static av_cold int mimic_decode_end(AVCodecContext *avctx)
     int i;
 
     av_free(ctx->swap_buf);
+
+    if (avctx->internal->is_copy)
+        return 0;
+
     for(i = 0; i < 16; i++)
         if(ctx->buf_ptrs[i].data[0])
-            avctx->release_buffer(avctx, &ctx->buf_ptrs[i]);
-    free_vlc(&ctx->vlc1);
+            ff_thread_release_buffer(avctx, &ctx->buf_ptrs[i]);
+    ff_free_vlc(&ctx->vlc);
 
     return 0;
 }
 
-AVCodec mimic_decoder = {
-    "mimic",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_MIMIC,
-    sizeof(MimicContext),
-    mimic_decode_init,
-    NULL,
-    mimic_decode_end,
-    mimic_decode_frame,
-    CODEC_CAP_DR1,
+AVCodec ff_mimic_decoder = {
+    .name                  = "mimic",
+    .type                  = AVMEDIA_TYPE_VIDEO,
+    .id                    = CODEC_ID_MIMIC,
+    .priv_data_size        = sizeof(MimicContext),
+    .init                  = mimic_decode_init,
+    .close                 = mimic_decode_end,
+    .decode                = mimic_decode_frame,
+    .capabilities          = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
+    .long_name             = NULL_IF_CONFIG_SMALL("Mimic"),
+    .update_thread_context = ONLY_IF_THREADS_ENABLED(mimic_decode_update_thread_context)
 };