00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "libavutil/avassert.h"
00029 #include "libavutil/crc.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/imgutils.h"
00032 #include "libavutil/pixdesc.h"
00033 #include "libavutil/timer.h"
00034 #include "avcodec.h"
00035 #include "internal.h"
00036 #include "dsputil.h"
00037 #include "rangecoder.h"
00038 #include "golomb.h"
00039 #include "mathops.h"
00040 #include "ffv1.h"
00041
00042 av_cold int ffv1_common_init(AVCodecContext *avctx)
00043 {
00044 FFV1Context *s = avctx->priv_data;
00045
00046 if (!avctx->width || !avctx->height)
00047 return AVERROR_INVALIDDATA;
00048
00049 s->avctx = avctx;
00050 s->flags = avctx->flags;
00051
00052 avcodec_get_frame_defaults(&s->picture);
00053
00054 ff_dsputil_init(&s->dsp, avctx);
00055
00056 s->width = avctx->width;
00057 s->height = avctx->height;
00058
00059
00060 s->num_h_slices = 1;
00061 s->num_v_slices = 1;
00062
00063 return 0;
00064 }
00065
00066 int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs)
00067 {
00068 int j;
00069
00070 fs->plane_count = f->plane_count;
00071 fs->transparency = f->transparency;
00072 for (j = 0; j < f->plane_count; j++) {
00073 PlaneContext *const p = &fs->plane[j];
00074
00075 if (fs->ac) {
00076 if (!p->state)
00077 p->state = av_malloc(CONTEXT_SIZE * p->context_count *
00078 sizeof(uint8_t));
00079 if (!p->state)
00080 return AVERROR(ENOMEM);
00081 } else {
00082 if (!p->vlc_state)
00083 p->vlc_state = av_malloc(p->context_count * sizeof(VlcState));
00084 if (!p->vlc_state)
00085 return AVERROR(ENOMEM);
00086 }
00087 }
00088
00089 if (fs->ac > 1) {
00090
00091 for (j = 1; j < 256; j++) {
00092 fs->c. one_state[ j] = f->state_transition[j];
00093 fs->c.zero_state[256 - j] = 256 - fs->c.one_state[j];
00094 }
00095 }
00096
00097 return 0;
00098 }
00099
00100 int ffv1_init_slices_state(FFV1Context *f)
00101 {
00102 int i, ret;
00103 for (i = 0; i < f->slice_count; i++) {
00104 FFV1Context *fs = f->slice_context[i];
00105 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
00106 return AVERROR(ENOMEM);
00107 }
00108 return 0;
00109 }
00110
00111 av_cold int ffv1_init_slice_contexts(FFV1Context *f)
00112 {
00113 int i;
00114
00115 f->slice_count = f->num_h_slices * f->num_v_slices;
00116 av_assert0(f->slice_count > 0);
00117
00118 for (i = 0; i < f->slice_count; i++) {
00119 FFV1Context *fs = av_mallocz(sizeof(*fs));
00120 int sx = i % f->num_h_slices;
00121 int sy = i / f->num_h_slices;
00122 int sxs = f->avctx->width * sx / f->num_h_slices;
00123 int sxe = f->avctx->width * (sx + 1) / f->num_h_slices;
00124 int sys = f->avctx->height * sy / f->num_v_slices;
00125 int sye = f->avctx->height * (sy + 1) / f->num_v_slices;
00126 f->slice_context[i] = fs;
00127 memcpy(fs, f, sizeof(*fs));
00128 memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
00129
00130 fs->slice_width = sxe - sxs;
00131 fs->slice_height = sye - sys;
00132 fs->slice_x = sxs;
00133 fs->slice_y = sys;
00134
00135 fs->sample_buffer = av_malloc(3 * MAX_PLANES * (fs->width + 6) *
00136 sizeof(*fs->sample_buffer));
00137 if (!fs->sample_buffer)
00138 return AVERROR(ENOMEM);
00139 }
00140 return 0;
00141 }
00142
00143 int ffv1_allocate_initial_states(FFV1Context *f)
00144 {
00145 int i;
00146
00147 for (i = 0; i < f->quant_table_count; i++) {
00148 f->initial_states[i] = av_malloc(f->context_count[i] *
00149 sizeof(*f->initial_states[i]));
00150 if (!f->initial_states[i])
00151 return AVERROR(ENOMEM);
00152 memset(f->initial_states[i], 128,
00153 f->context_count[i] * sizeof(*f->initial_states[i]));
00154 }
00155 return 0;
00156 }
00157
00158 void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)
00159 {
00160 int i, j;
00161
00162 for (i = 0; i < f->plane_count; i++) {
00163 PlaneContext *p = &fs->plane[i];
00164
00165 p->interlace_bit_state[0] = 128;
00166 p->interlace_bit_state[1] = 128;
00167
00168 if (fs->ac) {
00169 if (f->initial_states[p->quant_table_index]) {
00170 memcpy(p->state, f->initial_states[p->quant_table_index],
00171 CONTEXT_SIZE * p->context_count);
00172 } else
00173 memset(p->state, 128, CONTEXT_SIZE * p->context_count);
00174 } else {
00175 for (j = 0; j < p->context_count; j++) {
00176 p->vlc_state[j].drift = 0;
00177 p->vlc_state[j].error_sum = 4;
00178 p->vlc_state[j].bias = 0;
00179 p->vlc_state[j].count = 1;
00180 }
00181 }
00182 }
00183 }
00184
00185
00186 av_cold int ffv1_close(AVCodecContext *avctx)
00187 {
00188 FFV1Context *s = avctx->priv_data;
00189 int i, j;
00190
00191 if (avctx->codec->decode && s->picture.data[0])
00192 avctx->release_buffer(avctx, &s->picture);
00193 if (avctx->codec->decode && s->last_picture.data[0])
00194 avctx->release_buffer(avctx, &s->last_picture);
00195
00196 for (j = 0; j < s->slice_count; j++) {
00197 FFV1Context *fs = s->slice_context[j];
00198 for (i = 0; i < s->plane_count; i++) {
00199 PlaneContext *p = &fs->plane[i];
00200
00201 av_freep(&p->state);
00202 av_freep(&p->vlc_state);
00203 }
00204 av_freep(&fs->sample_buffer);
00205 }
00206
00207 av_freep(&avctx->stats_out);
00208 for (j = 0; j < s->quant_table_count; j++) {
00209 av_freep(&s->initial_states[j]);
00210 for (i = 0; i < s->slice_count; i++) {
00211 FFV1Context *sf = s->slice_context[i];
00212 av_freep(&sf->rc_stat2[j]);
00213 }
00214 av_freep(&s->rc_stat2[j]);
00215 }
00216
00217 for (i = 0; i < s->slice_count; i++)
00218 av_freep(&s->slice_context[i]);
00219
00220 return 0;
00221 }