2003-06-09 02:24:51 +00:00
|
|
|
/*
|
|
|
|
* FFV1 codec for libavcodec
|
|
|
|
*
|
2013-08-16 23:41:31 +00:00
|
|
|
* Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at>
|
2003-06-09 02:24:51 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2003-06-09 02:24:51 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 15:30:46 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2003-06-09 02:24:51 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2003-06-09 02:24:51 +00:00
|
|
|
* 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
|
2006-10-07 15:30:46 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-12 22:43:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-06-09 02:24:51 +00:00
|
|
|
*/
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2003-06-09 02:24:51 +00:00
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2010-03-22 14:26:17 +00:00
|
|
|
* FF Video Codec 1 (a lossless codec)
|
2003-06-09 02:24:51 +00:00
|
|
|
*/
|
|
|
|
|
2013-02-01 09:31:59 +00:00
|
|
|
#include "libavutil/attributes.h"
|
2012-10-11 16:55:53 +00:00
|
|
|
#include "libavutil/avassert.h"
|
2016-04-14 16:24:51 +00:00
|
|
|
|
2003-06-09 02:24:51 +00:00
|
|
|
#include "avcodec.h"
|
2004-10-31 10:08:50 +00:00
|
|
|
#include "rangecoder.h"
|
2012-10-21 12:05:46 +00:00
|
|
|
#include "ffv1.h"
|
2024-04-25 09:18:18 +00:00
|
|
|
#include "threadframe.h"
|
2003-06-09 02:24:51 +00:00
|
|
|
|
2015-08-22 01:14:12 +00:00
|
|
|
av_cold int ff_ffv1_common_init(AVCodecContext *avctx)
|
2012-10-11 16:55:53 +00:00
|
|
|
{
|
2003-06-09 02:24:51 +00:00
|
|
|
FFV1Context *s = avctx->priv_data;
|
|
|
|
|
2012-10-17 12:17:55 +00:00
|
|
|
if (!avctx->width || !avctx->height)
|
2012-10-06 01:12:25 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-10-11 16:55:53 +00:00
|
|
|
s->avctx = avctx;
|
|
|
|
s->flags = avctx->flags;
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2013-11-16 22:48:16 +00:00
|
|
|
s->picture.f = av_frame_alloc();
|
2013-04-27 20:21:16 +00:00
|
|
|
s->last_picture.f = av_frame_alloc();
|
2013-05-11 19:23:46 +00:00
|
|
|
if (!s->picture.f || !s->last_picture.f)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-10-19 10:14:22 +00:00
|
|
|
|
2012-10-11 16:55:53 +00:00
|
|
|
s->width = avctx->width;
|
|
|
|
s->height = avctx->height;
|
2003-06-09 02:24:51 +00:00
|
|
|
|
2012-10-11 16:55:53 +00:00
|
|
|
// defaults
|
|
|
|
s->num_h_slices = 1;
|
|
|
|
s->num_v_slices = 1;
|
2010-06-26 15:53:25 +00:00
|
|
|
|
2003-06-09 02:24:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
av_cold int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1Context *fs)
|
2012-10-11 16:55:53 +00:00
|
|
|
{
|
2015-10-17 00:13:42 +00:00
|
|
|
int j, i;
|
2010-10-14 22:03:32 +00:00
|
|
|
|
2012-10-19 10:14:22 +00:00
|
|
|
fs->plane_count = f->plane_count;
|
|
|
|
fs->transparency = f->transparency;
|
|
|
|
for (j = 0; j < f->plane_count; j++) {
|
|
|
|
PlaneContext *const p = &fs->plane[j];
|
|
|
|
|
2015-11-15 22:26:07 +00:00
|
|
|
if (fs->ac != AC_GOLOMB_RICE) {
|
2012-10-19 10:14:22 +00:00
|
|
|
if (!p->state)
|
2014-04-12 13:41:00 +00:00
|
|
|
p->state = av_malloc_array(p->context_count, CONTEXT_SIZE *
|
2012-10-19 10:14:22 +00:00
|
|
|
sizeof(uint8_t));
|
|
|
|
if (!p->state)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
} else {
|
2015-10-17 00:13:42 +00:00
|
|
|
if (!p->vlc_state) {
|
2024-04-25 09:18:18 +00:00
|
|
|
p->vlc_state = av_calloc(p->context_count, sizeof(*p->vlc_state));
|
2015-10-17 00:13:42 +00:00
|
|
|
if (!p->vlc_state)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (i = 0; i < p->context_count; i++) {
|
|
|
|
p->vlc_state[i].error_sum = 4;
|
|
|
|
p->vlc_state[i].count = 1;
|
|
|
|
}
|
|
|
|
}
|
2010-10-14 22:03:32 +00:00
|
|
|
}
|
2012-10-19 10:14:22 +00:00
|
|
|
}
|
2010-10-14 22:03:32 +00:00
|
|
|
|
2015-11-15 22:26:07 +00:00
|
|
|
if (fs->ac == AC_RANGE_CUSTOM_TAB) {
|
2012-10-19 10:14:22 +00:00
|
|
|
//FIXME only redo if state_transition changed
|
|
|
|
for (j = 1; j < 256; j++) {
|
2012-10-21 14:03:31 +00:00
|
|
|
fs->c. one_state[ j] = f->state_transition[j];
|
2012-10-19 10:14:22 +00:00
|
|
|
fs->c.zero_state[256 - j] = 256 - fs->c.one_state[j];
|
2010-10-14 22:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-22 01:14:12 +00:00
|
|
|
av_cold int ff_ffv1_init_slices_state(FFV1Context *f)
|
2012-10-21 14:03:31 +00:00
|
|
|
{
|
|
|
|
int i, ret;
|
2015-09-24 21:49:30 +00:00
|
|
|
for (i = 0; i < f->max_slice_count; i++) {
|
2012-10-17 12:17:55 +00:00
|
|
|
FFV1Context *fs = f->slice_context[i];
|
2015-08-22 01:14:12 +00:00
|
|
|
if ((ret = ff_ffv1_init_slice_state(f, fs)) < 0)
|
2012-10-21 14:03:31 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2012-04-20 12:50:07 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-22 01:14:12 +00:00
|
|
|
av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
|
2012-10-11 16:55:53 +00:00
|
|
|
{
|
2022-05-23 03:23:35 +00:00
|
|
|
int i, max_slice_count = f->num_h_slices * f->num_v_slices;
|
2010-10-14 22:03:32 +00:00
|
|
|
|
2022-05-23 03:23:35 +00:00
|
|
|
av_assert0(max_slice_count > 0);
|
2012-10-11 16:55:53 +00:00
|
|
|
|
2022-05-23 03:23:35 +00:00
|
|
|
for (i = 0; i < max_slice_count;) {
|
2012-10-11 16:55:53 +00:00
|
|
|
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;
|
2015-04-09 21:37:59 +00:00
|
|
|
FFV1Context *fs = av_mallocz(sizeof(*fs));
|
2013-02-04 23:22:29 +00:00
|
|
|
|
|
|
|
if (!fs)
|
2015-04-09 21:37:59 +00:00
|
|
|
goto memfail;
|
2013-02-04 23:22:29 +00:00
|
|
|
|
2022-05-23 03:23:35 +00:00
|
|
|
f->slice_context[i++] = fs;
|
2010-10-14 22:03:32 +00:00
|
|
|
memcpy(fs, f, sizeof(*fs));
|
2010-10-26 23:01:17 +00:00
|
|
|
memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
|
2010-10-14 22:03:32 +00:00
|
|
|
|
2012-10-11 16:55:53 +00:00
|
|
|
fs->slice_width = sxe - sxs;
|
|
|
|
fs->slice_height = sye - sys;
|
|
|
|
fs->slice_x = sxs;
|
|
|
|
fs->slice_y = sys;
|
2010-10-14 22:03:32 +00:00
|
|
|
|
2014-04-12 13:41:00 +00:00
|
|
|
fs->sample_buffer = av_malloc_array((fs->width + 6), 3 * MAX_PLANES *
|
2012-10-11 16:55:53 +00:00
|
|
|
sizeof(*fs->sample_buffer));
|
2016-08-07 22:00:46 +00:00
|
|
|
fs->sample_buffer32 = av_malloc_array((fs->width + 6), 3 * MAX_PLANES *
|
|
|
|
sizeof(*fs->sample_buffer32));
|
2022-05-23 03:23:35 +00:00
|
|
|
if (!fs->sample_buffer || !fs->sample_buffer32)
|
2015-04-09 21:37:59 +00:00
|
|
|
goto memfail;
|
2010-10-14 22:03:32 +00:00
|
|
|
}
|
2022-05-23 03:23:35 +00:00
|
|
|
f->max_slice_count = max_slice_count;
|
2010-10-14 22:03:32 +00:00
|
|
|
return 0;
|
2015-04-09 21:37:59 +00:00
|
|
|
|
|
|
|
memfail:
|
2022-05-23 03:23:35 +00:00
|
|
|
f->max_slice_count = i;
|
2015-04-09 21:37:59 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2010-10-14 22:03:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-22 01:14:12 +00:00
|
|
|
int ff_ffv1_allocate_initial_states(FFV1Context *f)
|
2012-10-11 16:55:53 +00:00
|
|
|
{
|
2010-10-26 23:01:11 +00:00
|
|
|
int i;
|
|
|
|
|
2012-10-11 16:55:53 +00:00
|
|
|
for (i = 0; i < f->quant_table_count; i++) {
|
2014-04-12 13:41:00 +00:00
|
|
|
f->initial_states[i] = av_malloc_array(f->context_count[i],
|
2012-10-11 16:55:53 +00:00
|
|
|
sizeof(*f->initial_states[i]));
|
|
|
|
if (!f->initial_states[i])
|
2010-10-26 23:01:11 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2012-10-11 16:55:53 +00:00
|
|
|
memset(f->initial_states[i], 128,
|
|
|
|
f->context_count[i] * sizeof(*f->initial_states[i]));
|
2010-10-26 23:01:11 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1Context *fs)
|
2012-10-11 16:55:53 +00:00
|
|
|
{
|
2012-10-21 12:05:46 +00:00
|
|
|
int i, j;
|
2010-10-24 16:55:42 +00:00
|
|
|
|
2012-10-19 10:14:22 +00:00
|
|
|
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;
|
|
|
|
|
2015-11-15 22:26:07 +00:00
|
|
|
if (fs->ac != AC_GOLOMB_RICE) {
|
2012-10-19 10:14:22 +00:00
|
|
|
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;
|
2010-10-24 16:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-09 02:24:51 +00:00
|
|
|
}
|
2010-10-24 16:55:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-21 12:05:46 +00:00
|
|
|
|
2015-08-22 01:14:12 +00:00
|
|
|
av_cold int ff_ffv1_close(AVCodecContext *avctx)
|
2003-06-09 02:24:51 +00:00
|
|
|
{
|
|
|
|
FFV1Context *s = avctx->priv_data;
|
2012-10-21 12:05:46 +00:00
|
|
|
int i, j;
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2013-04-27 20:21:16 +00:00
|
|
|
if (s->picture.f)
|
2024-04-25 09:18:18 +00:00
|
|
|
ff_thread_release_ext_buffer(avctx, &s->picture);
|
2013-04-27 20:21:16 +00:00
|
|
|
av_frame_free(&s->picture.f);
|
|
|
|
|
|
|
|
if (s->last_picture.f)
|
2024-04-25 09:18:18 +00:00
|
|
|
ff_thread_release_ext_buffer(avctx, &s->last_picture);
|
2013-04-27 20:21:16 +00:00
|
|
|
av_frame_free(&s->last_picture.f);
|
2012-02-15 18:34:08 +00:00
|
|
|
|
2015-09-24 21:49:30 +00:00
|
|
|
for (j = 0; j < s->max_slice_count; j++) {
|
2012-10-21 12:05:46 +00:00
|
|
|
FFV1Context *fs = s->slice_context[j];
|
|
|
|
for (i = 0; i < s->plane_count; i++) {
|
|
|
|
PlaneContext *p = &fs->plane[i];
|
2012-07-10 12:21:29 +00:00
|
|
|
|
2012-10-21 12:05:46 +00:00
|
|
|
av_freep(&p->state);
|
|
|
|
av_freep(&p->vlc_state);
|
2012-02-11 22:42:58 +00:00
|
|
|
}
|
2012-10-21 12:05:46 +00:00
|
|
|
av_freep(&fs->sample_buffer);
|
2016-08-07 22:00:46 +00:00
|
|
|
av_freep(&fs->sample_buffer32);
|
2012-02-11 22:42:58 +00:00
|
|
|
}
|
2003-11-01 17:38:25 +00:00
|
|
|
|
2012-10-21 12:05:46 +00:00
|
|
|
av_freep(&avctx->stats_out);
|
|
|
|
for (j = 0; j < s->quant_table_count; j++) {
|
|
|
|
av_freep(&s->initial_states[j]);
|
2015-09-24 21:49:30 +00:00
|
|
|
for (i = 0; i < s->max_slice_count; i++) {
|
2012-10-21 12:05:46 +00:00
|
|
|
FFV1Context *sf = s->slice_context[i];
|
|
|
|
av_freep(&sf->rc_stat2[j]);
|
2010-10-26 23:01:17 +00:00
|
|
|
}
|
2012-10-21 12:05:46 +00:00
|
|
|
av_freep(&s->rc_stat2[j]);
|
2010-10-26 23:01:17 +00:00
|
|
|
}
|
2010-10-24 12:31:09 +00:00
|
|
|
|
2015-09-24 21:49:30 +00:00
|
|
|
for (i = 0; i < s->max_slice_count; i++)
|
2012-10-21 12:05:46 +00:00
|
|
|
av_freep(&s->slice_context[i]);
|
2012-04-20 13:05:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|