2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/emms.h"
22 #include "libavutil/intmath.h"
23 #include "libavutil/log.h"
24 #include "libavutil/mem.h"
26 #include "codec_internal.h"
31 #include "rangecoder.h"
34 static inline int get_symbol(RangeCoder
*c
, uint8_t *state
, int is_signed
)
36 if (get_rac(c
, state
+ 0))
42 while (get_rac(c
, state
+ 1 + FFMIN(e
, 9))) { //1..10
45 return AVERROR_INVALIDDATA
;
49 for (int i
= e
- 1; i
>= 0; i
--)
50 a
+= a
+ get_rac(c
, state
+ 22 + FFMIN(i
, 9)); //22..31
52 e
= -(is_signed
&& get_rac(c
, state
+ 11 + FFMIN(e
, 10))); //11..21
57 static inline int get_symbol2(RangeCoder
*c
, uint8_t *state
, int log2
)
59 int r
= log2
>= 0 ? 1 << log2
: 1;
62 av_assert2(log2
>= -4);
64 while (log2
< 28 && get_rac(c
, state
+ 4 + log2
)) {
70 for (int i
= log2
- 1; i
>= 0; i
--)
71 v
+= get_rac(c
, state
+ 31 - i
) << i
;
76 static void unpack_coeffs(SnowContext
*s
, SubBand
*b
, SubBand
* parent
, int orientation
)
78 const int w
= b
->width
;
79 const int h
= b
->height
;
82 x_and_coeff
*xc
= b
->x_coeff
;
83 x_and_coeff
*prev_xc
= NULL
;
84 x_and_coeff
*prev2_xc
= xc
;
85 x_and_coeff
*parent_xc
= parent
? parent
->x_coeff
: NULL
;
86 x_and_coeff
*prev_parent_xc
= parent_xc
;
88 runs
= get_symbol2(&s
->c
, b
->state
[30], 0);
89 if (runs
-- > 0) run
= get_symbol2(&s
->c
, b
->state
[1], 3);
92 for (int y
= 0; y
< h
; y
++) {
94 int lt
= 0, t
= 0, rt
= 0;
96 if (y
&& prev_xc
->x
== 0)
99 for (int x
= 0; x
< w
; x
++) {
108 if (prev_xc
->x
== x
+ 1)
114 if (x
>>1 > parent_xc
->x
)
116 if (x
>>1 == parent_xc
->x
)
117 p
= parent_xc
->coeff
;
119 if (/*ll|*/l
|lt
|t
|rt
|p
) {
120 int context
= av_log2(/*FFABS(ll) + */3*(l
>>1) + (lt
>>1) + (t
&~1) + (rt
>>1) + (p
>>1));
122 v
= get_rac(&s
->c
, &b
->state
[0][context
]);
124 v
= 2*(get_symbol2(&s
->c
, b
->state
[context
+ 2], context
-4) + 1);
125 v
+= get_rac(&s
->c
, &b
->state
[0][16 + 1 + 3 + ff_quant3bA
[l
&0xFF] + 3 * ff_quant3bA
[t
&0xFF]]);
126 if ((uint16_t)v
!= v
) {
127 av_log(s
->avctx
, AV_LOG_ERROR
, "Coefficient damaged\n");
135 if (runs
-- > 0) run
= get_symbol2(&s
->c
, b
->state
[1], 3);
137 v
= 2 * (get_symbol2(&s
->c
, b
->state
[0 + 2], 0-4) + 1);
138 v
+= get_rac(&s
->c
, &b
->state
[0][16 + 1 + 3]);
139 if ((uint16_t)v
!= v
) {
140 av_log(s
->avctx
, AV_LOG_ERROR
, "Coefficient damaged\n");
150 av_assert2(run
>= 0);
151 if (y
) max_run
= FFMIN(run
, prev_xc
->x
- x
- 2);
152 else max_run
= FFMIN(run
, w
-x
-1);
154 max_run
= FFMIN(max_run
, 2*parent_xc
->x
- x
- 1);
155 av_assert2(max_run
>= 0 && max_run
<= run
);
162 (xc
++)->x
= w
+1; //end marker
168 while (parent_xc
->x
!= parent
->width
+1)
171 prev_parent_xc
= parent_xc
;
173 parent_xc
= prev_parent_xc
;
178 (xc
++)->x
= w
+ 1; //end marker
181 static av_always_inline
void predict_slice_buffered(SnowContext
*s
, slice_buffer
* sb
, IDWTELEM
* old_buffer
, int plane_index
, int add
, int mb_y
){
182 Plane
*p
= &s
->plane
[plane_index
];
183 const int mb_w
= s
->b_width
<< s
->block_max_depth
;
184 const int mb_h
= s
->b_height
<< s
->block_max_depth
;
186 int block_size
= MB_SIZE
>> s
->block_max_depth
;
187 int block_w
= plane_index
? block_size
>>s
->chroma_h_shift
: block_size
;
188 int block_h
= plane_index
? block_size
>>s
->chroma_v_shift
: block_size
;
189 const uint8_t *obmc
= plane_index
? ff_obmc_tab
[s
->block_max_depth
+s
->chroma_h_shift
] : ff_obmc_tab
[s
->block_max_depth
];
190 int obmc_stride
= plane_index
? (2*block_size
)>>s
->chroma_h_shift
: 2*block_size
;
191 int ref_stride
= s
->current_picture
->linesize
[plane_index
];
192 uint8_t *dst8
= s
->current_picture
->data
[plane_index
];
196 if(s
->keyframe
|| (s
->avctx
->debug
&512)){
201 for(y
=block_h
*mb_y
; y
<FFMIN(h
,block_h
*(mb_y
+1)); y
++){
202 // DWTELEM * line = slice_buffer_get_line(sb, y);
203 IDWTELEM
* line
= sb
->line
[y
];
205 // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
206 int v
= line
[x
] + (128<<FRAC_BITS
) + (1<<(FRAC_BITS
-1));
208 if(v
&(~255)) v
= ~(v
>>31);
209 dst8
[x
+ y
*ref_stride
]= v
;
213 for(y
=block_h
*mb_y
; y
<FFMIN(h
,block_h
*(mb_y
+1)); y
++){
214 // DWTELEM * line = slice_buffer_get_line(sb, y);
215 IDWTELEM
* line
= sb
->line
[y
];
217 line
[x
] -= 128 << FRAC_BITS
;
218 // buf[x + y*w]-= 128<<FRAC_BITS;
226 for(mb_x
=0; mb_x
<=mb_w
; mb_x
++){
227 add_yblock(s
, 1, sb
, old_buffer
, dst8
, obmc
,
228 block_w
*mb_x
- block_w
/2,
229 block_h
*mb_y
- block_h
/2,
232 w
, ref_stride
, obmc_stride
,
234 add
, 0, plane_index
);
237 if(s
->avmv
&& mb_y
< mb_h
&& plane_index
== 0)
238 for(mb_x
=0; mb_x
<mb_w
; mb_x
++){
239 AVMotionVector
*avmv
= s
->avmv
+ s
->avmv_index
;
240 const int b_width
= s
->b_width
<< s
->block_max_depth
;
241 const int b_stride
= b_width
;
242 BlockNode
*bn
= &s
->block
[mb_x
+ mb_y
*b_stride
];
251 avmv
->dst_x
= block_w
*mb_x
- block_w
/2;
252 avmv
->dst_y
= block_h
*mb_y
- block_h
/2;
253 avmv
->motion_scale
= 8;
254 avmv
->motion_x
= bn
->mx
* s
->mv_scale
;
255 avmv
->motion_y
= bn
->my
* s
->mv_scale
;
256 avmv
->src_x
= avmv
->dst_x
+ avmv
->motion_x
/ 8;
257 avmv
->src_y
= avmv
->dst_y
+ avmv
->motion_y
/ 8;
258 avmv
->source
= -1 - bn
->ref
;
263 static inline void decode_subband_slice_buffered(SnowContext
*s
, SubBand
*b
, slice_buffer
* sb
, int start_y
, int h
, int save_state
[1]){
264 const int w
= b
->width
;
266 const int qlog
= av_clip(s
->qlog
+ (int64_t)b
->qlog
, 0, QROOT
*16);
267 int qmul
= ff_qexp
[qlog
&(QROOT
-1)]<<(qlog
>>QSHIFT
);
268 int qadd
= (s
->qbias
*qmul
)>>QBIAS_SHIFT
;
271 if(b
->ibuf
== s
->spatial_idwt_buffer
|| s
->qlog
== LOSSLESS_QLOG
){
276 /* If we are on the second or later slice, restore our index. */
278 new_index
= save_state
[0];
281 for(y
=start_y
; y
<h
; y
++){
284 IDWTELEM
* line
= slice_buffer_get_line(sb
, y
* b
->stride_line
+ b
->buf_y_offset
) + b
->buf_x_offset
;
285 memset(line
, 0, b
->width
*sizeof(IDWTELEM
));
286 v
= b
->x_coeff
[new_index
].coeff
;
287 x
= b
->x_coeff
[new_index
++].x
;
289 register int t
= (int)( (v
>>1)*(unsigned)qmul
+ qadd
)>>QEXPSHIFT
;
290 register int u
= -(v
&1);
293 v
= b
->x_coeff
[new_index
].coeff
;
294 x
= b
->x_coeff
[new_index
++].x
;
298 /* Save our variables for the next slice. */
299 save_state
[0] = new_index
;
304 static int decode_q_branch(SnowContext
*s
, int level
, int x
, int y
){
305 const int w
= s
->b_width
<< s
->block_max_depth
;
306 const int rem_depth
= s
->block_max_depth
- level
;
307 const int index
= (x
+ y
*w
) << rem_depth
;
308 int trx
= (x
+1)<<rem_depth
;
309 const BlockNode
*left
= x
? &s
->block
[index
-1] : &null_block
;
310 const BlockNode
*top
= y
? &s
->block
[index
-w
] : &null_block
;
311 const BlockNode
*tl
= y
&& x
? &s
->block
[index
-w
-1] : left
;
312 const BlockNode
*tr
= y
&& trx
<w
&& ((x
&1)==0 || level
==0) ? &s
->block
[index
-w
+(1<<rem_depth
)] : tl
; //FIXME use lt
313 int s_context
= 2*left
->level
+ 2*top
->level
+ tl
->level
+ tr
->level
;
317 set_blocks(s
, level
, x
, y
, null_block
.color
[0], null_block
.color
[1], null_block
.color
[2], null_block
.mx
, null_block
.my
, null_block
.ref
, BLOCK_INTRA
);
321 if(level
==s
->block_max_depth
|| get_rac(&s
->c
, &s
->block_state
[4 + s_context
])){
323 int l
= left
->color
[0];
324 int cb
= left
->color
[1];
325 int cr
= left
->color
[2];
327 int ref_context
= av_log2(2*left
->ref
) + av_log2(2*top
->ref
);
328 int mx_context
= av_log2(2*FFABS(left
->mx
- top
->mx
)) + 0*av_log2(2*FFABS(tr
->mx
- top
->mx
));
329 int my_context
= av_log2(2*FFABS(left
->my
- top
->my
)) + 0*av_log2(2*FFABS(tr
->my
- top
->my
));
331 type
= get_rac(&s
->c
, &s
->block_state
[1 + left
->type
+ top
->type
]) ? BLOCK_INTRA
: 0;
334 pred_mv(s
, &mx
, &my
, 0, left
, top
, tr
);
335 ld
= get_symbol(&s
->c
, &s
->block_state
[32], 1);
336 if (ld
< -255 || ld
> 255) {
337 return AVERROR_INVALIDDATA
;
340 if (s
->nb_planes
> 2) {
341 cbd
= get_symbol(&s
->c
, &s
->block_state
[64], 1);
342 crd
= get_symbol(&s
->c
, &s
->block_state
[96], 1);
343 if (cbd
< -255 || cbd
> 255 || crd
< -255 || crd
> 255) {
344 return AVERROR_INVALIDDATA
;
350 if(s
->ref_frames
> 1)
351 ref
= get_symbol(&s
->c
, &s
->block_state
[128 + 1024 + 32*ref_context
], 0);
352 if (ref
>= s
->ref_frames
) {
353 av_log(s
->avctx
, AV_LOG_ERROR
, "Invalid ref\n");
354 return AVERROR_INVALIDDATA
;
356 pred_mv(s
, &mx
, &my
, ref
, left
, top
, tr
);
357 mx
+= (unsigned)get_symbol(&s
->c
, &s
->block_state
[128 + 32*(mx_context
+ 16*!!ref
)], 1);
358 my
+= (unsigned)get_symbol(&s
->c
, &s
->block_state
[128 + 32*(my_context
+ 16*!!ref
)], 1);
360 set_blocks(s
, level
, x
, y
, l
, cb
, cr
, mx
, my
, ref
, type
);
362 if ((res
= decode_q_branch(s
, level
+1, 2*x
+0, 2*y
+0)) < 0 ||
363 (res
= decode_q_branch(s
, level
+1, 2*x
+1, 2*y
+0)) < 0 ||
364 (res
= decode_q_branch(s
, level
+1, 2*x
+0, 2*y
+1)) < 0 ||
365 (res
= decode_q_branch(s
, level
+1, 2*x
+1, 2*y
+1)) < 0)
371 static void dequantize_slice_buffered(SnowContext
*s
, slice_buffer
* sb
, SubBand
*b
, IDWTELEM
*src
, int stride
, int start_y
, int end_y
){
372 const int w
= b
->width
;
373 const int qlog
= av_clip(s
->qlog
+ (int64_t)b
->qlog
, 0, QROOT
*16);
374 const int qmul
= ff_qexp
[qlog
&(QROOT
-1)]<<(qlog
>>QSHIFT
);
375 const int qadd
= (s
->qbias
*qmul
)>>QBIAS_SHIFT
;
378 if(s
->qlog
== LOSSLESS_QLOG
) return;
380 for(y
=start_y
; y
<end_y
; y
++){
381 // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
382 IDWTELEM
* line
= slice_buffer_get_line(sb
, (y
* b
->stride_line
) + b
->buf_y_offset
) + b
->buf_x_offset
;
386 line
[x
]= -((-i
*(unsigned)qmul
+ qadd
)>>(QEXPSHIFT
)); //FIXME try different bias
388 line
[x
]= (( i
*(unsigned)qmul
+ qadd
)>>(QEXPSHIFT
));
394 static void correlate_slice_buffered(SnowContext
*s
, slice_buffer
* sb
, SubBand
*b
, IDWTELEM
*src
, int stride
, int inverse
, int use_median
, int start_y
, int end_y
){
395 const int w
= b
->width
;
398 IDWTELEM
* line
=0; // silence silly "could be used without having been initialized" warning
402 line
= slice_buffer_get_line(sb
, ((start_y
- 1) * b
->stride_line
) + b
->buf_y_offset
) + b
->buf_x_offset
;
404 for(y
=start_y
; y
<end_y
; y
++){
406 // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
407 line
= slice_buffer_get_line(sb
, (y
* b
->stride_line
) + b
->buf_y_offset
) + b
->buf_x_offset
;
411 if(y
&& x
+1<w
) line
[x
] += mid_pred(line
[x
- 1], prev
[x
], prev
[x
+ 1]);
412 else line
[x
] += line
[x
- 1];
414 if(y
) line
[x
] += mid_pred(line
[x
- 1], prev
[x
], line
[x
- 1] + prev
[x
] - prev
[x
- 1]);
415 else line
[x
] += line
[x
- 1];
418 if(y
) line
[x
] += prev
[x
];
424 static void decode_qlogs(SnowContext
*s
){
425 int plane_index
, level
, orientation
;
427 for(plane_index
=0; plane_index
< s
->nb_planes
; plane_index
++){
428 for(level
=0; level
<s
->spatial_decomposition_count
; level
++){
429 for(orientation
=level
? 1:0; orientation
<4; orientation
++){
431 if (plane_index
==2) q
= s
->plane
[1].band
[level
][orientation
].qlog
;
432 else if(orientation
==2) q
= s
->plane
[plane_index
].band
[level
][1].qlog
;
433 else q
= get_symbol(&s
->c
, s
->header_state
, 1);
434 s
->plane
[plane_index
].band
[level
][orientation
].qlog
= q
;
440 #define GET_S(dst, check) \
441 tmp= get_symbol(&s->c, s->header_state, 0);\
443 av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\
444 return AVERROR_INVALIDDATA;\
448 static int decode_header(SnowContext
*s
){
449 int plane_index
, tmp
;
452 memset(kstate
, MID_STATE
, sizeof(kstate
));
454 s
->keyframe
= get_rac(&s
->c
, kstate
);
455 if(s
->keyframe
|| s
->always_reset
){
456 ff_snow_reset_contexts(s
);
457 s
->spatial_decomposition_type
=
461 s
->block_max_depth
= 0;
464 GET_S(s
->version
, tmp
<= 0U)
465 s
->always_reset
= get_rac(&s
->c
, s
->header_state
);
466 s
->temporal_decomposition_type
= get_symbol(&s
->c
, s
->header_state
, 0);
467 s
->temporal_decomposition_count
= get_symbol(&s
->c
, s
->header_state
, 0);
468 GET_S(s
->spatial_decomposition_count
, 0 < tmp
&& tmp
<= MAX_DECOMPOSITIONS
)
469 s
->colorspace_type
= get_symbol(&s
->c
, s
->header_state
, 0);
470 if (s
->colorspace_type
== 1) {
471 s
->avctx
->pix_fmt
= AV_PIX_FMT_GRAY8
;
473 } else if(s
->colorspace_type
== 0) {
474 s
->chroma_h_shift
= get_symbol(&s
->c
, s
->header_state
, 0);
475 s
->chroma_v_shift
= get_symbol(&s
->c
, s
->header_state
, 0);
477 if(s
->chroma_h_shift
== 1 && s
->chroma_v_shift
==1){
478 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV420P
;
479 }else if(s
->chroma_h_shift
== 0 && s
->chroma_v_shift
==0){
480 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV444P
;
481 }else if(s
->chroma_h_shift
== 2 && s
->chroma_v_shift
==2){
482 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV410P
;
484 av_log(s
, AV_LOG_ERROR
, "unsupported color subsample mode %d %d\n", s
->chroma_h_shift
, s
->chroma_v_shift
);
485 s
->chroma_h_shift
= s
->chroma_v_shift
= 1;
486 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV420P
;
487 return AVERROR_INVALIDDATA
;
491 av_log(s
, AV_LOG_ERROR
, "unsupported color space\n");
492 s
->chroma_h_shift
= s
->chroma_v_shift
= 1;
493 s
->avctx
->pix_fmt
= AV_PIX_FMT_YUV420P
;
494 return AVERROR_INVALIDDATA
;
498 s
->spatial_scalability
= get_rac(&s
->c
, s
->header_state
);
499 // s->rate_scalability= get_rac(&s->c, s->header_state);
500 GET_S(s
->max_ref_frames
, tmp
< (unsigned)MAX_REF_FRAMES
)
507 if(get_rac(&s
->c
, s
->header_state
)){
508 for(plane_index
=0; plane_index
<FFMIN(s
->nb_planes
, 2); plane_index
++){
510 Plane
*p
= &s
->plane
[plane_index
];
511 p
->diag_mc
= get_rac(&s
->c
, s
->header_state
);
512 htaps
= get_symbol(&s
->c
, s
->header_state
, 0);
513 if((unsigned)htaps
>= HTAPS_MAX
/2 - 1)
514 return AVERROR_INVALIDDATA
;
517 for(i
= htaps
/2; i
; i
--){
518 unsigned hcoeff
= get_symbol(&s
->c
, s
->header_state
, 0);
520 return AVERROR_INVALIDDATA
;
521 p
->hcoeff
[i
]= hcoeff
* (1-2*(i
&1));
524 p
->hcoeff
[0]= 32-sum
;
526 s
->plane
[2].diag_mc
= s
->plane
[1].diag_mc
;
527 s
->plane
[2].htaps
= s
->plane
[1].htaps
;
528 memcpy(s
->plane
[2].hcoeff
, s
->plane
[1].hcoeff
, sizeof(s
->plane
[1].hcoeff
));
530 if(get_rac(&s
->c
, s
->header_state
)){
531 GET_S(s
->spatial_decomposition_count
, 0 < tmp
&& tmp
<= MAX_DECOMPOSITIONS
)
536 s
->spatial_decomposition_type
+= (unsigned)get_symbol(&s
->c
, s
->header_state
, 1);
537 if(s
->spatial_decomposition_type
> 1U){
538 av_log(s
->avctx
, AV_LOG_ERROR
, "spatial_decomposition_type %d not supported\n", s
->spatial_decomposition_type
);
539 return AVERROR_INVALIDDATA
;
541 if(FFMIN(s
->avctx
-> width
>>s
->chroma_h_shift
,
542 s
->avctx
->height
>>s
->chroma_v_shift
) >> (s
->spatial_decomposition_count
-1) <= 1){
543 av_log(s
->avctx
, AV_LOG_ERROR
, "spatial_decomposition_count %d too large for size\n", s
->spatial_decomposition_count
);
544 return AVERROR_INVALIDDATA
;
546 if (s
->avctx
->width
> 65536-4) {
547 av_log(s
->avctx
, AV_LOG_ERROR
, "Width %d is too large\n", s
->avctx
->width
);
548 return AVERROR_INVALIDDATA
;
552 s
->qlog
+= (unsigned)get_symbol(&s
->c
, s
->header_state
, 1);
553 s
->mv_scale
+= (unsigned)get_symbol(&s
->c
, s
->header_state
, 1);
554 s
->qbias
+= (unsigned)get_symbol(&s
->c
, s
->header_state
, 1);
555 s
->block_max_depth
+= (unsigned)get_symbol(&s
->c
, s
->header_state
, 1);
556 if(s
->block_max_depth
> 1 || s
->block_max_depth
< 0 || s
->mv_scale
> 256U){
557 av_log(s
->avctx
, AV_LOG_ERROR
, "block_max_depth= %d is too large\n", s
->block_max_depth
);
558 s
->block_max_depth
= 0;
560 return AVERROR_INVALIDDATA
;
562 if (FFABS(s
->qbias
) > 127) {
563 av_log(s
->avctx
, AV_LOG_ERROR
, "qbias %d is too large\n", s
->qbias
);
565 return AVERROR_INVALIDDATA
;
571 static int decode_blocks(SnowContext
*s
){
579 if (s
->c
.bytestream
>= s
->c
.bytestream_end
)
580 return AVERROR_INVALIDDATA
;
581 if ((res
= decode_q_branch(s
, 0, x
, y
)) < 0)
588 static int decode_frame(AVCodecContext
*avctx
, AVFrame
*picture
,
589 int *got_frame
, AVPacket
*avpkt
)
591 const uint8_t *buf
= avpkt
->data
;
592 int buf_size
= avpkt
->size
;
593 SnowContext
*s
= avctx
->priv_data
;
594 RangeCoder
* const c
= &s
->c
;
596 int level
, orientation
, plane_index
;
599 ff_init_range_decoder(c
, buf
, buf_size
);
600 ff_build_rac_states(c
, 0.05*(1LL<<32), 256-8);
602 s
->current_picture
->pict_type
= AV_PICTURE_TYPE_I
; //FIXME I vs. P
603 if ((res
= decode_header(s
)) < 0)
606 if (!s
->mconly_picture
->data
[0]) {
607 res
= ff_get_buffer(avctx
, s
->mconly_picture
, AV_GET_BUFFER_FLAG_REF
);
611 if (s
->mconly_picture
->format
!= avctx
->pix_fmt
) {
612 av_log(avctx
, AV_LOG_ERROR
, "pixel format changed\n");
613 return AVERROR_INVALIDDATA
;
616 if ((res
=ff_snow_common_init_after_header(avctx
)) < 0)
619 // realloc slice buffer for the case that spatial_decomposition_count changed
620 ff_slice_buffer_destroy(&s
->sb
);
621 if ((res
= ff_slice_buffer_init(&s
->sb
, s
->plane
[0].height
,
622 (MB_SIZE
>> s
->block_max_depth
) +
623 s
->spatial_decomposition_count
* 11 + 1,
625 s
->spatial_idwt_buffer
)) < 0)
628 for(plane_index
=0; plane_index
< s
->nb_planes
; plane_index
++){
629 Plane
*p
= &s
->plane
[plane_index
];
630 p
->fast_mc
= p
->diag_mc
&& p
->htaps
==6 && p
->hcoeff
[0]==40
635 ff_snow_alloc_blocks(s
);
637 if ((res
= ff_snow_frames_prepare(s
)) < 0)
640 s
->current_picture
->width
= s
->avctx
->width
;
641 s
->current_picture
->height
= s
->avctx
->height
;
642 res
= ff_get_buffer(s
->avctx
, s
->current_picture
, AV_GET_BUFFER_FLAG_REF
);
646 s
->current_picture
->pict_type
= s
->keyframe
? AV_PICTURE_TYPE_I
: AV_PICTURE_TYPE_P
;
648 //keyframe flag duplication mess FIXME
649 if(avctx
->debug
&FF_DEBUG_PICT_INFO
)
650 av_log(avctx
, AV_LOG_ERROR
,
651 "keyframe:%d qlog:%d qbias: %d mvscale: %d "
652 "decomposition_type:%d decomposition_count:%d\n",
653 s
->keyframe
, s
->qlog
, s
->qbias
, s
->mv_scale
,
654 s
->spatial_decomposition_type
,
655 s
->spatial_decomposition_count
658 if (s
->avctx
->export_side_data
& AV_CODEC_EXPORT_DATA_MVS
) {
660 res
= av_size_mult(s
->b_width
* s
->b_height
, sizeof(AVMotionVector
) << (s
->block_max_depth
*2), &size
);
663 av_fast_malloc(&s
->avmv
, &s
->avmv_size
, size
);
665 return AVERROR(ENOMEM
);
672 if ((res
= decode_blocks(s
)) < 0)
675 for(plane_index
=0; plane_index
< s
->nb_planes
; plane_index
++){
676 Plane
*p
= &s
->plane
[plane_index
];
680 int decode_state
[MAX_DECOMPOSITIONS
][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
682 if(s
->avctx
->debug
&2048){
683 memset(s
->spatial_dwt_buffer
, 0, sizeof(DWTELEM
)*w
*h
);
684 predict_plane(s
, s
->spatial_idwt_buffer
, plane_index
, 1);
688 int v
= s
->current_picture
->data
[plane_index
][y
*s
->current_picture
->linesize
[plane_index
] + x
];
689 s
->mconly_picture
->data
[plane_index
][y
*s
->mconly_picture
->linesize
[plane_index
] + x
]= v
;
694 for(level
=0; level
<s
->spatial_decomposition_count
; level
++){
695 for(orientation
=level
? 1 : 0; orientation
<4; orientation
++){
696 SubBand
*b
= &p
->band
[level
][orientation
];
697 unpack_coeffs(s
, b
, b
->parent
, orientation
);
702 const int mb_h
= s
->b_height
<< s
->block_max_depth
;
703 const int block_size
= MB_SIZE
>> s
->block_max_depth
;
704 const int block_h
= plane_index
? block_size
>>s
->chroma_v_shift
: block_size
;
706 DWTCompose cs
[MAX_DECOMPOSITIONS
];
711 ff_spatial_idwt_buffered_init(cs
, &s
->sb
, w
, h
, 1, s
->spatial_decomposition_type
, s
->spatial_decomposition_count
);
712 for(mb_y
=0; mb_y
<=mb_h
; mb_y
++){
714 int slice_starty
= block_h
*mb_y
;
715 int slice_h
= block_h
*(mb_y
+1);
717 if (!(s
->keyframe
|| s
->avctx
->debug
&512)){
718 slice_starty
= FFMAX(0, slice_starty
- (block_h
>> 1));
719 slice_h
-= (block_h
>> 1);
722 for(level
=0; level
<s
->spatial_decomposition_count
; level
++){
723 for(orientation
=level
? 1 : 0; orientation
<4; orientation
++){
724 SubBand
*b
= &p
->band
[level
][orientation
];
727 int our_mb_start
= mb_y
;
728 int our_mb_end
= (mb_y
+ 1);
730 start_y
= (mb_y
? ((block_h
* our_mb_start
) >> (s
->spatial_decomposition_count
- level
)) + s
->spatial_decomposition_count
- level
+ extra
: 0);
731 end_y
= (((block_h
* our_mb_end
) >> (s
->spatial_decomposition_count
- level
)) + s
->spatial_decomposition_count
- level
+ extra
);
732 if (!(s
->keyframe
|| s
->avctx
->debug
&512)){
733 start_y
= FFMAX(0, start_y
- (block_h
>> (1+s
->spatial_decomposition_count
- level
)));
734 end_y
= FFMAX(0, end_y
- (block_h
>> (1+s
->spatial_decomposition_count
- level
)));
736 start_y
= FFMIN(b
->height
, start_y
);
737 end_y
= FFMIN(b
->height
, end_y
);
739 if (start_y
!= end_y
){
740 if (orientation
== 0){
741 SubBand
* correlate_band
= &p
->band
[0][0];
742 int correlate_end_y
= FFMIN(b
->height
, end_y
+ 1);
743 int correlate_start_y
= FFMIN(b
->height
, (start_y
? start_y
+ 1 : 0));
744 decode_subband_slice_buffered(s
, correlate_band
, &s
->sb
, correlate_start_y
, correlate_end_y
, decode_state
[0][0]);
745 correlate_slice_buffered(s
, &s
->sb
, correlate_band
, correlate_band
->ibuf
, correlate_band
->stride
, 1, 0, correlate_start_y
, correlate_end_y
);
746 dequantize_slice_buffered(s
, &s
->sb
, correlate_band
, correlate_band
->ibuf
, correlate_band
->stride
, start_y
, end_y
);
749 decode_subband_slice_buffered(s
, b
, &s
->sb
, start_y
, end_y
, decode_state
[level
][orientation
]);
754 for(; yd
<slice_h
; yd
+=4){
755 ff_spatial_idwt_buffered_slice(&s
->dwt
, cs
, &s
->sb
, s
->temp_idwt_buffer
, w
, h
, 1, s
->spatial_decomposition_type
, s
->spatial_decomposition_count
, yd
);
758 if(s
->qlog
== LOSSLESS_QLOG
){
759 for(; yq
<slice_h
&& yq
<h
; yq
++){
760 IDWTELEM
* line
= slice_buffer_get_line(&s
->sb
, yq
);
762 line
[x
] *= 1<<FRAC_BITS
;
767 predict_slice_buffered(s
, &s
->sb
, s
->spatial_idwt_buffer
, plane_index
, 1, mb_y
);
769 y
= FFMIN(p
->height
, slice_starty
);
770 end_y
= FFMIN(p
->height
, slice_h
);
772 ff_slice_buffer_release(&s
->sb
, y
++);
775 ff_slice_buffer_flush(&s
->sb
);
782 ff_snow_release_buffer(avctx
);
784 if(!(s
->avctx
->debug
&2048))
785 res
= av_frame_ref(picture
, s
->current_picture
);
787 res
= av_frame_ref(picture
, s
->mconly_picture
);
788 if (res
>= 0 && s
->avmv_index
) {
791 sd
= av_frame_new_side_data(picture
, AV_FRAME_DATA_MOTION_VECTORS
, s
->avmv_index
* sizeof(AVMotionVector
));
793 return AVERROR(ENOMEM
);
794 memcpy(sd
->data
, s
->avmv
, s
->avmv_index
* sizeof(AVMotionVector
));
802 bytes_read
= c
->bytestream
- c
->bytestream_start
;
803 if(bytes_read
==0) av_log(s
->avctx
, AV_LOG_ERROR
, "error at end of frame\n"); //FIXME
808 static av_cold
int decode_end(AVCodecContext
*avctx
)
810 SnowContext
*s
= avctx
->priv_data
;
812 ff_slice_buffer_destroy(&s
->sb
);
814 ff_snow_common_end(s
);
822 const FFCodec ff_snow_decoder
= {
824 CODEC_LONG_NAME("Snow"),
825 .p
.type
= AVMEDIA_TYPE_VIDEO
,
826 .p
.id
= AV_CODEC_ID_SNOW
,
827 .priv_data_size
= sizeof(SnowContext
),
828 .init
= ff_snow_common_init
,
830 FF_CODEC_DECODE_CB(decode_frame
),
831 .p
.capabilities
= AV_CODEC_CAP_DR1
,
832 .caps_internal
= FF_CODEC_CAP_INIT_CLEANUP
,