Skip to content

Commit 3d3ce96

Browse files
committed
avcodec/ffv1: split off and share frame header parsing code
Signed-off-by: James Almer <[email protected]>
1 parent 6da82b4 commit 3d3ce96

File tree

6 files changed

+491
-445
lines changed

6 files changed

+491
-445
lines changed

libavcodec/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ OBJS-$(CONFIG_EVRC_DECODER) += evrcdec.o acelp_vectors.o lsp.o
369369
OBJS-$(CONFIG_EXR_DECODER) += exr.o exrdsp.o half2float.o
370370
OBJS-$(CONFIG_EXR_ENCODER) += exrenc.o float2half.o
371371
OBJS-$(CONFIG_FASTAUDIO_DECODER) += fastaudio.o
372-
OBJS-$(CONFIG_FFV1_DECODER) += ffv1dec.o ffv1.o
373-
OBJS-$(CONFIG_FFV1_ENCODER) += ffv1enc.o ffv1.o
372+
OBJS-$(CONFIG_FFV1_DECODER) += ffv1dec.o ffv1_parse.o ffv1.o
373+
OBJS-$(CONFIG_FFV1_ENCODER) += ffv1enc.o ffv1_parse.o ffv1.o
374374
OBJS-$(CONFIG_FFV1_VULKAN_ENCODER) += ffv1enc.o ffv1.o ffv1enc_vulkan.o
375375
OBJS-$(CONFIG_FFWAVESYNTH_DECODER) += ffwavesynth.o
376376
OBJS-$(CONFIG_FIC_DECODER) += fic.o

libavcodec/ffv1.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
#include "ffv1.h"
3434
#include "libavutil/refstruct.h"
3535

36-
av_cold int ff_ffv1_common_init(AVCodecContext *avctx)
36+
av_cold int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s)
3737
{
38-
FFV1Context *s = avctx->priv_data;
39-
4038
if (!avctx->width || !avctx->height)
4139
return AVERROR_INVALIDDATA;
4240

@@ -221,10 +219,13 @@ void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
221219
}
222220
}
223221

222+
int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
223+
{
224+
return get_symbol_inline(c, state, is_signed);
225+
}
224226

225-
av_cold int ff_ffv1_close(AVCodecContext *avctx)
227+
av_cold void ff_ffv1_close(FFV1Context *s)
226228
{
227-
FFV1Context *s = avctx->priv_data;
228229
int i, j;
229230

230231
for (j = 0; j < s->max_slice_count; j++) {
@@ -238,7 +239,6 @@ av_cold int ff_ffv1_close(AVCodecContext *avctx)
238239

239240
av_refstruct_unref(&s->slice_damaged);
240241

241-
av_freep(&avctx->stats_out);
242242
for (j = 0; j < s->quant_table_count; j++) {
243243
av_freep(&s->initial_states[j]);
244244
for (i = 0; i < s->max_slice_count; i++) {
@@ -249,6 +249,4 @@ av_cold int ff_ffv1_close(AVCodecContext *avctx)
249249
}
250250

251251
av_freep(&s->slices);
252-
253-
return 0;
254252
}

libavcodec/ffv1.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* FF Video Codec 1 (a lossless codec)
2929
*/
3030

31+
#include "libavutil/attributes.h"
3132
#include "avcodec.h"
3233
#include "get_bits.h"
3334
#include "mathops.h"
@@ -169,15 +170,20 @@ typedef struct FFV1Context {
169170
uint8_t frame_damaged;
170171
} FFV1Context;
171172

172-
int ff_ffv1_common_init(AVCodecContext *avctx);
173+
int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s);
173174
int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc);
174175
int ff_ffv1_init_slices_state(FFV1Context *f);
175176
int ff_ffv1_init_slice_contexts(FFV1Context *f);
176177
PlaneContext *ff_ffv1_planes_alloc(void);
177178
int ff_ffv1_allocate_initial_states(FFV1Context *f);
178179
void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc);
179-
int ff_ffv1_close(AVCodecContext *avctx);
180+
void ff_ffv1_close(FFV1Context *s);
180181
int ff_need_new_slices(int width, int num_h_slices, int chroma_shift);
182+
int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state);
183+
int ff_ffv1_read_extra_header(FFV1Context *f);
184+
int ff_ffv1_read_quant_tables(RangeCoder *c,
185+
int16_t quant_table[MAX_CONTEXT_INPUTS][256]);
186+
int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed);
181187

182188
/**
183189
* This is intended for both width and height
@@ -223,4 +229,29 @@ static inline void update_vlc_state(VlcState *const state, const int v)
223229
state->count = count;
224230
}
225231

232+
233+
static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
234+
int is_signed)
235+
{
236+
if (get_rac(c, state + 0))
237+
return 0;
238+
else {
239+
int e;
240+
unsigned a;
241+
e = 0;
242+
while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
243+
e++;
244+
if (e > 31)
245+
return AVERROR_INVALIDDATA;
246+
}
247+
248+
a = 1;
249+
for (int i = e - 1; i >= 0; i--)
250+
a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
251+
252+
e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
253+
return (a ^ e) - e;
254+
}
255+
}
256+
226257
#endif /* AVCODEC_FFV1_H */

0 commit comments

Comments
 (0)