| 1 | /* |
| 2 | * FFV1 codec for libavcodec |
| 3 | * |
| 4 | * Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at> |
| 5 | * |
| 6 | * This file is part of FFmpeg. |
| 7 | * |
| 8 | * FFmpeg is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * FFmpeg is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with FFmpeg; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * @file |
| 25 | * FF Video Codec 1 (a lossless codec) |
| 26 | */ |
| 27 | |
| 28 | #include "libavutil/attributes.h" |
| 29 | #include "libavutil/avassert.h" |
| 30 | #include "libavutil/crc.h" |
| 31 | #include "libavutil/opt.h" |
| 32 | #include "libavutil/imgutils.h" |
| 33 | #include "libavutil/pixdesc.h" |
| 34 | #include "libavutil/timer.h" |
| 35 | #include "avcodec.h" |
| 36 | #include "internal.h" |
| 37 | #include "rangecoder.h" |
| 38 | #include "golomb.h" |
| 39 | #include "mathops.h" |
| 40 | #include "ffv1.h" |
| 41 | |
| 42 | av_cold int ffv1_common_init(AVCodecContext *avctx) |
| 43 | { |
| 44 | FFV1Context *s = avctx->priv_data; |
| 45 | |
| 46 | if (!avctx->width || !avctx->height) |
| 47 | return AVERROR_INVALIDDATA; |
| 48 | |
| 49 | s->avctx = avctx; |
| 50 | s->flags = avctx->flags; |
| 51 | |
| 52 | s->picture.f = av_frame_alloc(); |
| 53 | s->last_picture.f = av_frame_alloc(); |
| 54 | if (!s->picture.f || !s->last_picture.f) |
| 55 | return AVERROR(ENOMEM); |
| 56 | |
| 57 | s->width = avctx->width; |
| 58 | s->height = avctx->height; |
| 59 | |
| 60 | // defaults |
| 61 | s->num_h_slices = 1; |
| 62 | s->num_v_slices = 1; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | av_cold int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs) |
| 68 | { |
| 69 | int j; |
| 70 | |
| 71 | fs->plane_count = f->plane_count; |
| 72 | fs->transparency = f->transparency; |
| 73 | for (j = 0; j < f->plane_count; j++) { |
| 74 | PlaneContext *const p = &fs->plane[j]; |
| 75 | |
| 76 | if (fs->ac) { |
| 77 | if (!p->state) |
| 78 | p->state = av_malloc_array(p->context_count, CONTEXT_SIZE * |
| 79 | sizeof(uint8_t)); |
| 80 | if (!p->state) |
| 81 | return AVERROR(ENOMEM); |
| 82 | } else { |
| 83 | if (!p->vlc_state) |
| 84 | p->vlc_state = av_malloc_array(p->context_count, sizeof(VlcState)); |
| 85 | if (!p->vlc_state) |
| 86 | return AVERROR(ENOMEM); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (fs->ac > 1) { |
| 91 | //FIXME only redo if state_transition changed |
| 92 | for (j = 1; j < 256; j++) { |
| 93 | fs->c. one_state[ j] = f->state_transition[j]; |
| 94 | fs->c.zero_state[256 - j] = 256 - fs->c.one_state[j]; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | av_cold int ffv1_init_slices_state(FFV1Context *f) |
| 102 | { |
| 103 | int i, ret; |
| 104 | for (i = 0; i < f->slice_count; i++) { |
| 105 | FFV1Context *fs = f->slice_context[i]; |
| 106 | if ((ret = ffv1_init_slice_state(f, fs)) < 0) |
| 107 | return AVERROR(ENOMEM); |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | av_cold int ffv1_init_slice_contexts(FFV1Context *f) |
| 113 | { |
| 114 | int i; |
| 115 | |
| 116 | f->slice_count = f->num_h_slices * f->num_v_slices; |
| 117 | av_assert0(f->slice_count > 0); |
| 118 | |
| 119 | for (i = 0; i < f->slice_count; i++) { |
| 120 | FFV1Context *fs = av_mallocz(sizeof(*fs)); |
| 121 | int sx = i % f->num_h_slices; |
| 122 | int sy = i / f->num_h_slices; |
| 123 | int sxs = f->avctx->width * sx / f->num_h_slices; |
| 124 | int sxe = f->avctx->width * (sx + 1) / f->num_h_slices; |
| 125 | int sys = f->avctx->height * sy / f->num_v_slices; |
| 126 | int sye = f->avctx->height * (sy + 1) / f->num_v_slices; |
| 127 | |
| 128 | if (!fs) |
| 129 | return AVERROR(ENOMEM); |
| 130 | |
| 131 | f->slice_context[i] = fs; |
| 132 | memcpy(fs, f, sizeof(*fs)); |
| 133 | memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2)); |
| 134 | |
| 135 | fs->slice_width = sxe - sxs; |
| 136 | fs->slice_height = sye - sys; |
| 137 | fs->slice_x = sxs; |
| 138 | fs->slice_y = sys; |
| 139 | |
| 140 | fs->sample_buffer = av_malloc_array((fs->width + 6), 3 * MAX_PLANES * |
| 141 | sizeof(*fs->sample_buffer)); |
| 142 | if (!fs->sample_buffer) |
| 143 | return AVERROR(ENOMEM); |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | int ffv1_allocate_initial_states(FFV1Context *f) |
| 149 | { |
| 150 | int i; |
| 151 | |
| 152 | for (i = 0; i < f->quant_table_count; i++) { |
| 153 | f->initial_states[i] = av_malloc_array(f->context_count[i], |
| 154 | sizeof(*f->initial_states[i])); |
| 155 | if (!f->initial_states[i]) |
| 156 | return AVERROR(ENOMEM); |
| 157 | memset(f->initial_states[i], 128, |
| 158 | f->context_count[i] * sizeof(*f->initial_states[i])); |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs) |
| 164 | { |
| 165 | int i, j; |
| 166 | |
| 167 | for (i = 0; i < f->plane_count; i++) { |
| 168 | PlaneContext *p = &fs->plane[i]; |
| 169 | |
| 170 | p->interlace_bit_state[0] = 128; |
| 171 | p->interlace_bit_state[1] = 128; |
| 172 | |
| 173 | if (fs->ac) { |
| 174 | if (f->initial_states[p->quant_table_index]) { |
| 175 | memcpy(p->state, f->initial_states[p->quant_table_index], |
| 176 | CONTEXT_SIZE * p->context_count); |
| 177 | } else |
| 178 | memset(p->state, 128, CONTEXT_SIZE * p->context_count); |
| 179 | } else { |
| 180 | for (j = 0; j < p->context_count; j++) { |
| 181 | p->vlc_state[j].drift = 0; |
| 182 | p->vlc_state[j].error_sum = 4; //FFMAX((RANGE + 32)/64, 2); |
| 183 | p->vlc_state[j].bias = 0; |
| 184 | p->vlc_state[j].count = 1; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | av_cold int ffv1_close(AVCodecContext *avctx) |
| 192 | { |
| 193 | FFV1Context *s = avctx->priv_data; |
| 194 | int i, j; |
| 195 | |
| 196 | if (s->picture.f) |
| 197 | ff_thread_release_buffer(avctx, &s->picture); |
| 198 | av_frame_free(&s->picture.f); |
| 199 | |
| 200 | if (s->last_picture.f) |
| 201 | ff_thread_release_buffer(avctx, &s->last_picture); |
| 202 | av_frame_free(&s->last_picture.f); |
| 203 | |
| 204 | for (j = 0; j < s->slice_count; j++) { |
| 205 | FFV1Context *fs = s->slice_context[j]; |
| 206 | for (i = 0; i < s->plane_count; i++) { |
| 207 | PlaneContext *p = &fs->plane[i]; |
| 208 | |
| 209 | av_freep(&p->state); |
| 210 | av_freep(&p->vlc_state); |
| 211 | } |
| 212 | av_freep(&fs->sample_buffer); |
| 213 | } |
| 214 | |
| 215 | av_freep(&avctx->stats_out); |
| 216 | for (j = 0; j < s->quant_table_count; j++) { |
| 217 | av_freep(&s->initial_states[j]); |
| 218 | for (i = 0; i < s->slice_count; i++) { |
| 219 | FFV1Context *sf = s->slice_context[i]; |
| 220 | av_freep(&sf->rc_stat2[j]); |
| 221 | } |
| 222 | av_freep(&s->rc_stat2[j]); |
| 223 | } |
| 224 | |
| 225 | for (i = 0; i < s->slice_count; i++) |
| 226 | av_freep(&s->slice_context[i]); |
| 227 | |
| 228 | return 0; |
| 229 | } |