1 /*
2 * Copyright (c) 2015-2016 Kieran Kunhya <kieran@kunhya.com>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * CFHD Video Decoder
24 */
25
31
37
38 #define SUBBAND_COUNT 10
39
41 {
43
48
50 }
51
53 {
57 }
58
60 {
73 }
74
75 /* TODO: merge with VLC tables or use LUT */
77 {
78 int64_t abslevel = abs(level);
79 return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
FFSIGN(level) * quantisation;
80 }
81
82 static inline void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride,
84 {
85 int16_t tmp;
86
87 int i;
88 for (i = 0; i <
len; i++) {
89 if (i == 0) {
90 tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
91 output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
92 if (clip)
94
95 tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
96 output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
97 if (clip)
99 } else if (i == len-1) {
100 tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
101 output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
102 if (clip)
104
105 tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
106 output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
107 if (clip)
109 } else {
110 tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
111 output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
112 if (clip)
114
115 tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
116 output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
117 if (clip)
119 }
120 }
121 }
122
124 {
125 filter(output, 1, low, 1, high, 1, width, 0);
126 }
127
129 {
130 filter(output, 1, low, 1, high, 1, width, clip);
131 }
132
133 static void vert_filter(int16_t *output,
int out_stride, int16_t *low,
int low_stride,
134 int16_t *high,
int high_stride,
int len)
135 {
136 filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
137 }
138
140 {
142 int i;
143
144 for (i = 0; i < 3; i++) {
147 }
150 }
151
153 {
155 int i, j, k, ret;
156
158 return ret;
159
161
162 for (i = 0; i < 3; i++) {
166 int w8, h8, w4, h4, w2, h2;
167 height =
FFALIGN(height / 8, 2) * 8;
171
174 w4 = w8 * 2;
175 h4 = h8 * 2;
176 w2 = w4 * 2;
177 h2 = h4 * 2;
178
183 }
184
195
197 for(k = 0; k < 4; k++) {
200 }
201 }
202
203 /* ll2 and ll1 commented out because they are done in-place */
206 //s->plane[i].l_h[2] = ll2;
209 //s->plane[i].l_h[5] = ll1;
212 }
213
216
217 return 0;
218 }
219
222 {
227 int ret = 0, i, j,
plane, got_buffer = 0;
228 int16_t *coeff_data;
229
232
234
236 /* Bit weird but implement the tag parsing as the spec says */
237 uint16_t tagu = bytestream2_get_be16(&gb);
238 int16_t
tag = (int16_t)tagu;
239 int8_t tag8 = (int8_t)(tagu >> 8);
240 uint16_t abstag = abs(tag);
241 int8_t abs_tag8 = abs(tag8);
242 uint16_t data = bytestream2_get_be16(&gb);
243 if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
245 } else if (tag == 20) {
248 } else if (tag == 21) {
251 } else if (tag == 101) {
254 } else if (tag == 12) {
257 if (data != 3) {
260 break;
261 }
262 } else if (tag == 14) {
267 break;
268 }
269 } else if (tag == 62) {
275 break;
276 }
278 } else if (tag == 48) {
286 break;
287 }
291 break;
292 }
293 } else if (tag == 51) {
299 break;
300 }
301 } else if (tag == 35)
303 else if (tag == 53) {
306 } else if (tag == 109) {
311 } else if (tag == 27) {
318 break;
319 }
320 } else if (tag == 28) {
326 break;
327 }
328 } else if (tag == 1)
330 else if (tag == 10) {
331 if (data != 0) {
334 break;
335 }
337 } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
338 av_log(avctx,
AV_LOG_DEBUG,
"Small chunk length %"PRIu16
" %s\n", data * 4, tag < 0 ?
"optional" :
"required");
340 } else if (tag == 23) {
344 break;
345 } else if (tag == 2) {
350 break;
351 }
352 for (i = 0; i <
data; i++) {
353 uint16_t tag2 = bytestream2_get_be16(&gb);
354 uint16_t val2 = bytestream2_get_be16(&gb);
356 }
357 } else if (tag == 41) {
361 if (data < 2) {
364 break;
365 }
366 } else if (tag == 42) {
369 if (data < 2) {
372 break;
373 }
374 } else if (tag == 49) {
378 if (data < 2) {
381 break;
382 }
383 } else if (tag == 50) {
386 if (data < 2) {
389 break;
390 }
391 } else if (tag == 71) {
394 } else if (tag == 72) {
397 } else if (tag == 70) {
400 if (!(s->
bpc == 10 || s->
bpc == 12)) {
403 break;
404 }
405 } else if (tag == 84) {
407 if (data == 1)
409 else if (data == 3)
411 else {
414 break;
415 }
416 } else
418
419 /* Some kind of end of header tag */
425 return ret;
426 }
427 }
428
430 return ret;
431
434 got_buffer = 1;
435 }
437
438 /* Lowpass coefficients */
444
445 if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
450 }
451
452 av_log(avctx,
AV_LOG_DEBUG,
"Start of lowpass coeffs component %"PRIu16
" height:%d, width:%d\n", s->
channel_num, lowpass_height, lowpass_width);
453 for (i = 0; i < lowpass_height; i++) {
454 for (j = 0; j < lowpass_width; j++)
455 coeff_data[j] = bytestream2_get_be16u(&gb);
456
457 coeff_data += lowpass_width;
458 }
459
460 /* Copy last line of coefficients if odd height */
461 if (lowpass_height & 1) {
462 memcpy(&coeff_data[lowpass_height * lowpass_width],
463 &coeff_data[(lowpass_height - 1) * lowpass_width],
464 lowpass_width * sizeof(*coeff_data));
465 }
466
467 av_log(avctx,
AV_LOG_DEBUG,
"Lowpass coefficients %"PRIu16
"\n", lowpass_width * lowpass_height);
468 }
469
476 int expected = highpass_height * highpass_stride;
477 int a_expected = highpass_a_height * highpass_a_width;
479 int count = 0, bytes;
480
481 if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < expected) {
485 }
486
488
490 {
493 while (1) {
497
498 /* escape */
499 if (level == 64)
500 break;
501
503
504 if (count > expected)
505 break;
506
508 for (i = 0; i <
run; i++)
509 *coeff_data++ = coeff;
510 }
511 } else {
512 while (1) {
516
517 /* escape */
518 if (level == 255 && run == 2)
519 break;
520
522
523 if (count > expected)
524 break;
525
527 for (i = 0; i <
run; i++)
528 *coeff_data++ = coeff;
529 }
530 }
532 }
533
534 if (count > expected) {
538 }
539
545 } else
547
548 av_log(avctx,
AV_LOG_DEBUG,
"End subband coeffs %i extra %i\n", count, count - expected);
550
551 /* Copy last line of coefficients if odd height */
552 if (highpass_height & 1) {
553 memcpy(&coeff_data[highpass_height * highpass_stride],
554 &coeff_data[(highpass_height - 1) * highpass_stride],
555 highpass_stride * sizeof(*coeff_data));
556 }
557 }
558 }
559
564 }
565
566 if (!got_buffer) {
570 }
571
572 for (plane = 0; plane < 3 && !ret; plane++) {
573 /* level 1 */
577 int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : 0;
578 int16_t *low, *high, *output, *dst;
579
585 }
586
587 av_log(avctx,
AV_LOG_DEBUG,
"Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
588
592 for (i = 0; i < lowpass_width; i++) {
593 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
594 low++;
595 high++;
596 output++;
597 }
598
602
603 for (i = 0; i < lowpass_width; i++) {
604 // note the stride of "low" is highpass_stride
605 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
606 low++;
607 high++;
608 output++;
609 }
610
614 for (i = 0; i < lowpass_height * 2; i++) {
616 low += lowpass_width;
617 high += lowpass_width;
618 output += lowpass_width * 2;
619 }
622 for (i = 0; i < lowpass_height * 2; i++) {
623 for (j = 0; j < lowpass_width * 2; j++)
624 output[j] <<= 2;
625
626 output += lowpass_width * 2;
627 }
628 }
629
630 /* level 2 */
634
640 }
641
642 av_log(avctx,
AV_LOG_DEBUG,
"Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
643
647 for (i = 0; i < lowpass_width; i++) {
648 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
649 low++;
650 high++;
651 output++;
652 }
653
657 for (i = 0; i < lowpass_width; i++) {
658 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
659 low++;
660 high++;
661 output++;
662 }
663
667 for (i = 0; i < lowpass_height * 2; i++) {
669 low += lowpass_width;
670 high += lowpass_width;
671 output += lowpass_width * 2;
672 }
673
675 for (i = 0; i < lowpass_height * 2; i++) {
676 for (j = 0; j < lowpass_width * 2; j++)
677 output[j] <<= 2;
678
679 output += lowpass_width * 2;
680 }
681
682 /* level 3 */
686
692 }
693
694 av_log(avctx,
AV_LOG_DEBUG,
"Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
695
699 for (i = 0; i < lowpass_width; i++) {
700 vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
701 low++;
702 high++;
703 output++;
704 }
705
709 for (i = 0; i < lowpass_width; i++) {
710 vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
711 low++;
712 high++;
713 output++;
714 }
715
716 dst = (int16_t *)pic->
data[act_plane];
719 for (i = 0; i < lowpass_height * 2; i++) {
721 low += lowpass_width;
722 high += lowpass_width;
723 dst += pic->
linesize[act_plane] / 2;
724 }
725 }
726
727
729 if (ret < 0)
730 return ret;
731
732 *got_frame = 1;
734 }
735
737 {
739
741
745 }
746
747 return 0;
748 }
749
761 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
static av_cold int init(AVCodecContext *avctx)
static int dequant_and_decompand(int level, int quantisation)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
static void horiz_filter(int16_t *output, int16_t *low, int16_t *high, int width)
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
static av_cold int end(AVCodecContext *avctx)
Multithreading support functions.
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
static int alloc_buffers(AVCodecContext *avctx)
static int get_bits_count(const GetBitContext *s)
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
CFHD_RL_VLC_ELEM table_18_rl_vlc[4572]
CFHD_RL_VLC_ELEM table_9_rl_vlc[2088]
uint8_t prescale_shift[3]
#define UPDATE_CACHE(name, gb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static void vert_filter(int16_t *output, int out_stride, int16_t *low, int low_stride, int16_t *high, int high_stride, int len)
static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high, int width, uint8_t clip)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
const char * name
Name of the codec implementation.
#define CLOSE_READER(name, gb)
static void init_frame_defaults(CFHDContext *s)
SubBand band[DWT_LEVELS][4]
int width
picture width / height.
static void init_plane_defaults(CFHDContext *s)
void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
#define OPEN_READER(name, gb)
BYTE int const BYTE int int int height
static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
refcounted data buffer API
static av_cold int cfhd_decode_init(AVCodecContext *avctx)
#define AV_PIX_FMT_GBRP12
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define AV_PIX_FMT_YUV422P10
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static void free_buffers(AVCodecContext *avctx)
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
GLint GLenum GLboolean GLsizei stride
common internal api header.
common internal and external API header
static double clip(void *opaque, double val)
Clip value val in the minval - maxval range.
struct AVCodecInternal * internal
Private context used for internal data.
static const double coeff[2][5]
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
int ff_cfhd_init_vlcs(CFHDContext *s)
#define av_malloc_array(a, b)
uint8_t lowpass_precision
This structure stores compressed data.
void ff_free_vlc(VLC *vlc)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
Clip a signed integer to an unsigned power of two range.