libavcodec/ffv1.c
5e20f836
 /*
  * FFV1 codec for libavcodec
  *
ff0c6282
  * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
5e20f836
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
5e20f836
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
5e20f836
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
5e20f836
  * 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
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5e20f836
  */
115329f1
 
5e20f836
 /**
ba87f080
  * @file
0e22d688
  * FF Video Codec 1 (a lossless codec)
5e20f836
  */
 
9a978b33
 #include "libavutil/avassert.h"
d6e87190
 #include "libavutil/crc.h"
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/timer.h"
5e20f836
 #include "avcodec.h"
ed45636e
 #include "internal.h"
880eae9c
 #include "rangecoder.h"
11e659c2
 #include "golomb.h"
199436b9
 #include "mathops.h"
e4255eaf
 #include "ffv1.h"
5e20f836
 
e4255eaf
 av_cold int ffv1_common_init(AVCodecContext *avctx)
9a978b33
 {
5e20f836
     FFV1Context *s = avctx->priv_data;
 
d6e87190
     if (!avctx->width || !avctx->height)
856834a7
         return AVERROR_INVALIDDATA;
 
9a978b33
     s->avctx = avctx;
     s->flags = avctx->flags;
115329f1
 
4737a593
     avcodec_get_frame_defaults(&s->picture);
 
9cf0841e
     ff_dsputil_init(&s->dsp, avctx);
115329f1
 
9a978b33
     s->width  = avctx->width;
     s->height = avctx->height;
5e20f836
 
9a978b33
     // defaults
     s->num_h_slices = 1;
     s->num_v_slices = 1;
cbabccc3
 
5e20f836
     return 0;
 }
 
e4255eaf
 int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs)
9a978b33
 {
32883c06
     int j;
e2b2f845
 
0f13cd31
     fs->plane_count  = f->plane_count;
     fs->transparency = f->transparency;
     for (j = 0; j < f->plane_count; j++) {
         PlaneContext *const p = &fs->plane[j];
 
         if (fs->ac) {
             if (!p->state)
                 p->state = av_malloc(CONTEXT_SIZE * p->context_count *
                                      sizeof(uint8_t));
             if (!p->state)
                 return AVERROR(ENOMEM);
         } else {
             if (!p->vlc_state)
                 p->vlc_state = av_malloc(p->context_count * sizeof(VlcState));
             if (!p->vlc_state)
                 return AVERROR(ENOMEM);
e2b2f845
         }
0f13cd31
     }
e2b2f845
 
0f13cd31
     if (fs->ac > 1) {
         //FIXME only redo if state_transition changed
         for (j = 1; j < 256; j++) {
aa760b17
             fs->c. one_state[      j] = f->state_transition[j];
0f13cd31
             fs->c.zero_state[256 - j] = 256 - fs->c.one_state[j];
e2b2f845
         }
     }
 
     return 0;
 }
 
aa760b17
 int ffv1_init_slices_state(FFV1Context *f)
 {
     int i, ret;
d6e87190
     for (i = 0; i < f->slice_count; i++) {
         FFV1Context *fs = f->slice_context[i];
aa760b17
         if ((ret = ffv1_init_slice_state(f, fs)) < 0)
             return AVERROR(ENOMEM);
32883c06
     }
     return 0;
 }
 
e4255eaf
 av_cold int ffv1_init_slice_contexts(FFV1Context *f)
9a978b33
 {
e2b2f845
     int i;
 
9a978b33
     f->slice_count = f->num_h_slices * f->num_v_slices;
010f1ce9
     av_assert0(f->slice_count > 0);
9a978b33
 
     for (i = 0; i < f->slice_count; i++) {
         FFV1Context *fs = av_mallocz(sizeof(*fs));
         int sx          = i % f->num_h_slices;
         int sy          = i / f->num_h_slices;
         int sxs         = f->avctx->width  *  sx      / f->num_h_slices;
         int sxe         = f->avctx->width  * (sx + 1) / f->num_h_slices;
         int sys         = f->avctx->height *  sy      / f->num_v_slices;
         int sye         = f->avctx->height * (sy + 1) / f->num_v_slices;
5a831151
 
         if (!fs)
             return AVERROR(ENOMEM);
 
9a978b33
         f->slice_context[i] = fs;
e2b2f845
         memcpy(fs, f, sizeof(*fs));
b7b5bccb
         memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
e2b2f845
 
9a978b33
         fs->slice_width  = sxe - sxs;
         fs->slice_height = sye - sys;
         fs->slice_x      = sxs;
         fs->slice_y      = sys;
e2b2f845
 
0f13cd31
         fs->sample_buffer = av_malloc(3 * MAX_PLANES * (fs->width + 6) *
9a978b33
                                       sizeof(*fs->sample_buffer));
e2b2f845
         if (!fs->sample_buffer)
             return AVERROR(ENOMEM);
     }
     return 0;
 }
 
e4255eaf
 int ffv1_allocate_initial_states(FFV1Context *f)
9a978b33
 {
99a5e935
     int i;
 
9a978b33
     for (i = 0; i < f->quant_table_count; i++) {
         f->initial_states[i] = av_malloc(f->context_count[i] *
                                          sizeof(*f->initial_states[i]));
         if (!f->initial_states[i])
99a5e935
             return AVERROR(ENOMEM);
9a978b33
         memset(f->initial_states[i], 128,
                f->context_count[i] * sizeof(*f->initial_states[i]));
99a5e935
     }
     return 0;
 }
 
e4255eaf
 void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)
9a978b33
 {
c7a435aa
     int i, j;
5e20f836
 
0f13cd31
     for (i = 0; i < f->plane_count; i++) {
         PlaneContext *p = &fs->plane[i];
 
         p->interlace_bit_state[0] = 128;
         p->interlace_bit_state[1] = 128;
 
         if (fs->ac) {
             if (f->initial_states[p->quant_table_index]) {
                 memcpy(p->state, f->initial_states[p->quant_table_index],
                        CONTEXT_SIZE * p->context_count);
             } else
                 memset(p->state, 128, CONTEXT_SIZE * p->context_count);
         } else {
             for (j = 0; j < p->context_count; j++) {
                 p->vlc_state[j].drift     = 0;
                 p->vlc_state[j].error_sum = 4;    //FFMAX((RANGE + 32)/64, 2);
                 p->vlc_state[j].bias      = 0;
                 p->vlc_state[j].count     = 1;
11e659c2
             }
c2f1b2cb
         }
5e20f836
     }
c7a435aa
 }
 
5e20f836
 
71f7b22d
 av_cold int ffv1_close(AVCodecContext *avctx)
9a978b33
 {
0c2aaa88
     FFV1Context *s = avctx->priv_data;
e2b2f845
     int i, j;
5e20f836
 
58b4e540
     if (avctx->codec->decode && s->picture.data[0])
         avctx->release_buffer(avctx, &s->picture);
371d37fc
     if (avctx->codec->decode && s->last_picture.data[0])
         avctx->release_buffer(avctx, &s->last_picture);
58b4e540
 
9a978b33
     for (j = 0; j < s->slice_count; j++) {
         FFV1Context *fs = s->slice_context[j];
         for (i = 0; i < s->plane_count; i++) {
             PlaneContext *p = &fs->plane[i];
5e20f836
 
1da72577
             av_freep(&p->state);
             av_freep(&p->vlc_state);
         }
e2b2f845
         av_freep(&fs->sample_buffer);
     }
cbabccc3
 
bc29ae4a
     av_freep(&avctx->stats_out);
9a978b33
     for (j = 0; j < s->quant_table_count; j++) {
99a5e935
         av_freep(&s->initial_states[j]);
9a978b33
         for (i = 0; i < s->slice_count; i++) {
             FFV1Context *sf = s->slice_context[i];
672e7e39
             av_freep(&sf->rc_stat2[j]);
         }
b7b5bccb
         av_freep(&s->rc_stat2[j]);
672e7e39
     }
bc29ae4a
 
9a978b33
     for (i = 0; i < s->slice_count; i++)
a0e7079a
         av_freep(&s->slice_context[i]);
 
5e20f836
     return 0;
 }