You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1589 lines
62KB

  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. * @file
  22. * Cineform HD video decoder
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/buffer.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/opt.h"
  30. #include "avcodec.h"
  31. #include "bytestream.h"
  32. #include "get_bits.h"
  33. #include "internal.h"
  34. #include "thread.h"
  35. #include "cfhd.h"
  36. #define ALPHA_COMPAND_DC_OFFSET 256
  37. #define ALPHA_COMPAND_GAIN 9400
  38. static av_cold int cfhd_init(AVCodecContext *avctx)
  39. {
  40. CFHDContext *s = avctx->priv_data;
  41. s->avctx = avctx;
  42. for (int i = 0; i < 64; i++) {
  43. int val = i;
  44. if (val >= 40) {
  45. if (val >= 54) {
  46. val -= 54;
  47. val <<= 2;
  48. val += 54;
  49. }
  50. val -= 40;
  51. val <<= 2;
  52. val += 40;
  53. }
  54. s->lut[0][i] = val;
  55. }
  56. for (int i = 0; i < 256; i++)
  57. s->lut[1][i] = i + ((768LL * i * i * i) / (256 * 256 * 256));
  58. return ff_cfhd_init_vlcs(s);
  59. }
  60. static void init_plane_defaults(CFHDContext *s)
  61. {
  62. s->subband_num = 0;
  63. s->level = 0;
  64. s->subband_num_actual = 0;
  65. }
  66. static void init_peak_table_defaults(CFHDContext *s)
  67. {
  68. s->peak.level = 0;
  69. s->peak.offset = 0;
  70. memset(&s->peak.base, 0, sizeof(s->peak.base));
  71. }
  72. static void init_frame_defaults(CFHDContext *s)
  73. {
  74. s->coded_width = 0;
  75. s->coded_height = 0;
  76. s->coded_format = AV_PIX_FMT_YUV422P10;
  77. s->cropped_height = 0;
  78. s->bpc = 10;
  79. s->channel_cnt = 3;
  80. s->subband_cnt = SUBBAND_COUNT;
  81. s->channel_num = 0;
  82. s->lowpass_precision = 16;
  83. s->quantisation = 1;
  84. s->codebook = 0;
  85. s->difference_coding = 0;
  86. s->frame_type = 0;
  87. s->sample_type = 0;
  88. init_plane_defaults(s);
  89. init_peak_table_defaults(s);
  90. }
  91. static inline int dequant_and_decompand(CFHDContext *s, int level, int quantisation, int codebook)
  92. {
  93. if (codebook == 0 || codebook == 1) {
  94. return s->lut[codebook][abs(level)] * FFSIGN(level) * quantisation;
  95. } else
  96. return level * quantisation;
  97. }
  98. static inline void difference_coding(int16_t *band, int width, int height)
  99. {
  100. int i,j;
  101. for (i = 0; i < height; i++) {
  102. for (j = 1; j < width; j++) {
  103. band[j] += band[j-1];
  104. }
  105. band += width;
  106. }
  107. }
  108. static inline void peak_table(int16_t *band, Peak *peak, int length)
  109. {
  110. int i;
  111. for (i = 0; i < length; i++)
  112. if (abs(band[i]) > peak->level)
  113. band[i] = bytestream2_get_le16(&peak->base);
  114. }
  115. static inline void process_alpha(int16_t *alpha, int width)
  116. {
  117. int i, channel;
  118. for (i = 0; i < width; i++) {
  119. channel = alpha[i];
  120. channel -= ALPHA_COMPAND_DC_OFFSET;
  121. channel <<= 3;
  122. channel *= ALPHA_COMPAND_GAIN;
  123. channel >>= 16;
  124. channel = av_clip_uintp2(channel, 12);
  125. alpha[i] = channel;
  126. }
  127. }
  128. static inline void process_bayer(AVFrame *frame, int bpc)
  129. {
  130. const int linesize = frame->linesize[0];
  131. uint16_t *r = (uint16_t *)frame->data[0];
  132. uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
  133. uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
  134. uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
  135. const int mid = 1 << (bpc - 1);
  136. const int factor = 1 << (16 - bpc);
  137. for (int y = 0; y < frame->height >> 1; y++) {
  138. for (int x = 0; x < frame->width; x += 2) {
  139. int R, G1, G2, B;
  140. int g, rg, bg, gd;
  141. g = r[x];
  142. rg = g1[x];
  143. bg = g2[x];
  144. gd = b[x];
  145. gd -= mid;
  146. R = (rg - mid) * 2 + g;
  147. G1 = g + gd;
  148. G2 = g - gd;
  149. B = (bg - mid) * 2 + g;
  150. R = av_clip_uintp2(R * factor, 16);
  151. G1 = av_clip_uintp2(G1 * factor, 16);
  152. G2 = av_clip_uintp2(G2 * factor, 16);
  153. B = av_clip_uintp2(B * factor, 16);
  154. r[x] = R;
  155. g1[x] = G1;
  156. g2[x] = G2;
  157. b[x] = B;
  158. }
  159. r += linesize;
  160. g1 += linesize;
  161. g2 += linesize;
  162. b += linesize;
  163. }
  164. }
  165. static inline void filter(int16_t *output, ptrdiff_t out_stride,
  166. int16_t *low, ptrdiff_t low_stride,
  167. int16_t *high, ptrdiff_t high_stride,
  168. int len, int clip)
  169. {
  170. int16_t tmp;
  171. int i;
  172. tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
  173. output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
  174. if (clip)
  175. output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
  176. tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
  177. output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
  178. if (clip)
  179. output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
  180. for (i = 1; i < len - 1; i++) {
  181. tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
  182. output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
  183. if (clip)
  184. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  185. tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
  186. output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
  187. if (clip)
  188. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  189. }
  190. tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
  191. output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
  192. if (clip)
  193. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  194. tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
  195. output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
  196. if (clip)
  197. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  198. }
  199. static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
  200. int width, int linesize, int plane)
  201. {
  202. int i;
  203. int16_t even, odd;
  204. for (i = 0; i < width; i++) {
  205. even = (low[i] - high[i])/2;
  206. odd = (low[i] + high[i])/2;
  207. output[i] = av_clip_uintp2(even, 10);
  208. output[i + linesize] = av_clip_uintp2(odd, 10);
  209. }
  210. }
  211. static inline void inverse_temporal_filter(int16_t *output, int16_t *low, int16_t *high,
  212. int width)
  213. {
  214. for (int i = 0; i < width; i++) {
  215. int even = (low[i] - high[i]) / 2;
  216. int odd = (low[i] + high[i]) / 2;
  217. low[i] = even;
  218. high[i] = odd;
  219. }
  220. }
  221. static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
  222. int width)
  223. {
  224. filter(output, 1, low, 1, high, 1, width, 0);
  225. }
  226. static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
  227. int width, int clip)
  228. {
  229. filter(output, 1, low, 1, high, 1, width, clip);
  230. }
  231. static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
  232. int width, int clip)
  233. {
  234. filter(output, 2, low, 1, high, 1, width, clip);
  235. }
  236. static void vert_filter(int16_t *output, ptrdiff_t out_stride,
  237. int16_t *low, ptrdiff_t low_stride,
  238. int16_t *high, ptrdiff_t high_stride, int len)
  239. {
  240. filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
  241. }
  242. static void free_buffers(CFHDContext *s)
  243. {
  244. int i, j;
  245. for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
  246. av_freep(&s->plane[i].idwt_buf);
  247. av_freep(&s->plane[i].idwt_tmp);
  248. s->plane[i].idwt_size = 0;
  249. for (j = 0; j < SUBBAND_COUNT_3D; j++)
  250. s->plane[i].subband[j] = NULL;
  251. for (j = 0; j < 10; j++)
  252. s->plane[i].l_h[j] = NULL;
  253. }
  254. s->a_height = 0;
  255. s->a_width = 0;
  256. }
  257. static int alloc_buffers(AVCodecContext *avctx)
  258. {
  259. CFHDContext *s = avctx->priv_data;
  260. int i, j, ret, planes, bayer = 0;
  261. int chroma_x_shift, chroma_y_shift;
  262. unsigned k;
  263. if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
  264. return ret;
  265. avctx->pix_fmt = s->coded_format;
  266. if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
  267. &chroma_x_shift,
  268. &chroma_y_shift)) < 0)
  269. return ret;
  270. planes = av_pix_fmt_count_planes(s->coded_format);
  271. if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
  272. planes = 4;
  273. chroma_x_shift = 1;
  274. chroma_y_shift = 1;
  275. bayer = 1;
  276. }
  277. for (i = 0; i < planes; i++) {
  278. int w8, h8, w4, h4, w2, h2;
  279. int width = (i || bayer) ? s->coded_width >> chroma_x_shift : s->coded_width;
  280. int height = (i || bayer) ? s->coded_height >> chroma_y_shift : s->coded_height;
  281. ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
  282. if (chroma_y_shift && !bayer)
  283. height = FFALIGN(height / 8, 2) * 8;
  284. s->plane[i].width = width;
  285. s->plane[i].height = height;
  286. s->plane[i].stride = stride;
  287. w8 = FFALIGN(s->plane[i].width / 8, 8);
  288. h8 = FFALIGN(height, 8) / 8;
  289. w4 = w8 * 2;
  290. h4 = h8 * 2;
  291. w2 = w4 * 2;
  292. h2 = h4 * 2;
  293. if (s->transform_type == 0) {
  294. s->plane[i].idwt_size = FFALIGN(height, 8) * stride;
  295. s->plane[i].idwt_buf =
  296. av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
  297. s->plane[i].idwt_tmp =
  298. av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
  299. } else {
  300. s->plane[i].idwt_size = FFALIGN(height, 8) * stride * 2;
  301. s->plane[i].idwt_buf =
  302. av_mallocz_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_buf));
  303. s->plane[i].idwt_tmp =
  304. av_malloc_array(s->plane[i].idwt_size, sizeof(*s->plane[i].idwt_tmp));
  305. }
  306. if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
  307. return AVERROR(ENOMEM);
  308. s->plane[i].subband[0] = s->plane[i].idwt_buf;
  309. s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
  310. s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
  311. s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
  312. s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
  313. s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
  314. s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
  315. if (s->transform_type == 0) {
  316. s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
  317. s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
  318. s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
  319. } else {
  320. int16_t *frame2 =
  321. s->plane[i].subband[7] = s->plane[i].idwt_buf + 4 * w2 * h2;
  322. s->plane[i].subband[8] = frame2 + 2 * w4 * h4;
  323. s->plane[i].subband[9] = frame2 + 1 * w4 * h4;
  324. s->plane[i].subband[10] = frame2 + 3 * w4 * h4;
  325. s->plane[i].subband[11] = frame2 + 2 * w2 * h2;
  326. s->plane[i].subband[12] = frame2 + 1 * w2 * h2;
  327. s->plane[i].subband[13] = frame2 + 3 * w2 * h2;
  328. s->plane[i].subband[14] = s->plane[i].idwt_buf + 2 * w2 * h2;
  329. s->plane[i].subband[15] = s->plane[i].idwt_buf + 1 * w2 * h2;
  330. s->plane[i].subband[16] = s->plane[i].idwt_buf + 3 * w2 * h2;
  331. }
  332. if (s->transform_type == 0) {
  333. for (j = 0; j < DWT_LEVELS; j++) {
  334. for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  335. s->plane[i].band[j][k].a_width = w8 << j;
  336. s->plane[i].band[j][k].a_height = h8 << j;
  337. }
  338. }
  339. } else {
  340. for (j = 0; j < DWT_LEVELS_3D; j++) {
  341. int t = j < 1 ? 0 : (j < 3 ? 1 : 2);
  342. for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  343. s->plane[i].band[j][k].a_width = w8 << t;
  344. s->plane[i].band[j][k].a_height = h8 << t;
  345. }
  346. }
  347. }
  348. /* ll2 and ll1 commented out because they are done in-place */
  349. s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
  350. s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
  351. // s->plane[i].l_h[2] = ll2;
  352. s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
  353. s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
  354. // s->plane[i].l_h[5] = ll1;
  355. s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
  356. s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
  357. if (s->transform_type != 0) {
  358. int16_t *frame2 = s->plane[i].idwt_tmp + 4 * w2 * h2;
  359. s->plane[i].l_h[8] = frame2;
  360. s->plane[i].l_h[9] = frame2 + 2 * w2 * h2;
  361. }
  362. }
  363. s->a_height = s->coded_height;
  364. s->a_width = s->coded_width;
  365. s->a_format = s->coded_format;
  366. return 0;
  367. }
  368. static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
  369. AVPacket *avpkt)
  370. {
  371. CFHDContext *s = avctx->priv_data;
  372. GetByteContext gb;
  373. ThreadFrame frame = { .f = data };
  374. AVFrame *pic = data;
  375. int ret = 0, i, j, plane, got_buffer = 0;
  376. int16_t *coeff_data;
  377. init_frame_defaults(s);
  378. s->planes = av_pix_fmt_count_planes(s->coded_format);
  379. bytestream2_init(&gb, avpkt->data, avpkt->size);
  380. while (bytestream2_get_bytes_left(&gb) >= 4) {
  381. /* Bit weird but implement the tag parsing as the spec says */
  382. uint16_t tagu = bytestream2_get_be16(&gb);
  383. int16_t tag = (int16_t)tagu;
  384. int8_t tag8 = (int8_t)(tagu >> 8);
  385. uint16_t abstag = abs(tag);
  386. int8_t abs_tag8 = abs(tag8);
  387. uint16_t data = bytestream2_get_be16(&gb);
  388. if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
  389. av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
  390. } else if (tag == SampleFlags) {
  391. av_log(avctx, AV_LOG_DEBUG, "Progressive? %"PRIu16"\n", data);
  392. s->progressive = data & 0x0001;
  393. } else if (tag == FrameType) {
  394. s->frame_type = data;
  395. av_log(avctx, AV_LOG_DEBUG, "Frame type %"PRIu16"\n", data);
  396. } else if (tag == ImageWidth) {
  397. av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
  398. s->coded_width = data;
  399. } else if (tag == ImageHeight) {
  400. av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
  401. s->coded_height = data;
  402. } else if (tag == ChannelCount) {
  403. av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
  404. s->channel_cnt = data;
  405. if (data > 4) {
  406. av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
  407. ret = AVERROR_PATCHWELCOME;
  408. break;
  409. }
  410. } else if (tag == SubbandCount) {
  411. av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
  412. if (data != SUBBAND_COUNT && data != SUBBAND_COUNT_3D) {
  413. av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
  414. ret = AVERROR_PATCHWELCOME;
  415. break;
  416. }
  417. } else if (tag == ChannelNumber) {
  418. s->channel_num = data;
  419. av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
  420. if (s->channel_num >= s->planes) {
  421. av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
  422. ret = AVERROR(EINVAL);
  423. break;
  424. }
  425. init_plane_defaults(s);
  426. } else if (tag == SubbandNumber) {
  427. if (s->subband_num != 0 && data == 1) // hack
  428. s->level++;
  429. av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
  430. s->subband_num = data;
  431. if ((s->transform_type == 0 && s->level >= DWT_LEVELS) ||
  432. (s->transform_type == 2 && s->level >= DWT_LEVELS_3D)) {
  433. av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
  434. ret = AVERROR(EINVAL);
  435. break;
  436. }
  437. if (s->subband_num > 3) {
  438. av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
  439. ret = AVERROR(EINVAL);
  440. break;
  441. }
  442. } else if (tag == SubbandBand) {
  443. av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
  444. s->subband_num_actual = data;
  445. if ((s->transform_type == 0 && s->subband_num_actual >= SUBBAND_COUNT) ||
  446. (s->transform_type == 2 && s->subband_num_actual >= SUBBAND_COUNT_3D && s->subband_num_actual != 255)) {
  447. av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
  448. ret = AVERROR(EINVAL);
  449. break;
  450. }
  451. } else if (tag == LowpassPrecision)
  452. av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
  453. else if (tag == Quantization) {
  454. s->quantisation = data;
  455. av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
  456. } else if (tag == PrescaleShift) {
  457. s->prescale_shift[0] = (data >> 0) & 0x7;
  458. s->prescale_shift[1] = (data >> 3) & 0x7;
  459. s->prescale_shift[2] = (data >> 6) & 0x7;
  460. av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
  461. } else if (tag == BandEncoding) {
  462. if (!data || data > 5) {
  463. av_log(avctx, AV_LOG_ERROR, "Invalid band encoding\n");
  464. ret = AVERROR(EINVAL);
  465. break;
  466. }
  467. s->band_encoding = data;
  468. av_log(avctx, AV_LOG_DEBUG, "Encode Method for Subband %d : %x\n", s->subband_num_actual, data);
  469. } else if (tag == LowpassWidth) {
  470. av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
  471. s->plane[s->channel_num].band[0][0].width = data;
  472. s->plane[s->channel_num].band[0][0].stride = data;
  473. } else if (tag == LowpassHeight) {
  474. av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
  475. s->plane[s->channel_num].band[0][0].height = data;
  476. } else if (tag == SampleType) {
  477. s->sample_type = data;
  478. av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
  479. } else if (tag == TransformType) {
  480. if (data > 2) {
  481. av_log(avctx, AV_LOG_ERROR, "Invalid transform type\n");
  482. ret = AVERROR(EINVAL);
  483. break;
  484. }
  485. s->transform_type = data;
  486. av_log(avctx, AV_LOG_DEBUG, "Transform type %"PRIu16"\n", data);
  487. } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
  488. if (abstag == 0x4001)
  489. s->peak.level = 0;
  490. av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
  491. bytestream2_skipu(&gb, data * 4);
  492. } else if (tag == FrameIndex) {
  493. av_log(avctx, AV_LOG_DEBUG, "Frame index %"PRIu16"\n", data);
  494. s->frame_index = data;
  495. } else if (tag == SampleIndexTable) {
  496. av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
  497. if (data > bytestream2_get_bytes_left(&gb) / 4) {
  498. av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
  499. ret = AVERROR_INVALIDDATA;
  500. break;
  501. }
  502. for (i = 0; i < data; i++) {
  503. uint16_t tag2 = bytestream2_get_be16(&gb);
  504. uint16_t val2 = bytestream2_get_be16(&gb);
  505. av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
  506. }
  507. } else if (tag == HighpassWidth) {
  508. av_log(avctx, AV_LOG_DEBUG, "Highpass width %i channel %i level %i subband %i\n", data, s->channel_num, s->level, s->subband_num);
  509. if (data < 3) {
  510. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
  511. ret = AVERROR(EINVAL);
  512. break;
  513. }
  514. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  515. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  516. } else if (tag == HighpassHeight) {
  517. av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
  518. if (data < 3) {
  519. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
  520. ret = AVERROR(EINVAL);
  521. break;
  522. }
  523. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  524. } else if (tag == BandWidth) {
  525. av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
  526. if (data < 3) {
  527. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
  528. ret = AVERROR(EINVAL);
  529. break;
  530. }
  531. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  532. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  533. } else if (tag == BandHeight) {
  534. av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
  535. if (data < 3) {
  536. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
  537. ret = AVERROR(EINVAL);
  538. break;
  539. }
  540. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  541. } else if (tag == InputFormat) {
  542. av_log(avctx, AV_LOG_DEBUG, "Input format %i\n", data);
  543. if (s->coded_format == AV_PIX_FMT_NONE ||
  544. s->coded_format == AV_PIX_FMT_YUV422P10) {
  545. if (data >= 100 && data <= 105) {
  546. s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
  547. } else if (data >= 122 && data <= 128) {
  548. s->coded_format = AV_PIX_FMT_GBRP12;
  549. } else if (data == 30) {
  550. s->coded_format = AV_PIX_FMT_GBRAP12;
  551. } else {
  552. s->coded_format = AV_PIX_FMT_YUV422P10;
  553. }
  554. s->planes = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 4 : av_pix_fmt_count_planes(s->coded_format);
  555. }
  556. } else if (tag == BandCodingFlags) {
  557. s->codebook = data & 0xf;
  558. s->difference_coding = (data >> 4) & 1;
  559. av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
  560. } else if (tag == Precision) {
  561. av_log(avctx, AV_LOG_DEBUG, "Precision %i\n", data);
  562. if (!(data == 10 || data == 12)) {
  563. av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
  564. ret = AVERROR(EINVAL);
  565. break;
  566. }
  567. avctx->bits_per_raw_sample = s->bpc = data;
  568. } else if (tag == EncodedFormat) {
  569. av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
  570. if (data == 1) {
  571. s->coded_format = AV_PIX_FMT_YUV422P10;
  572. } else if (data == 2) {
  573. s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
  574. } else if (data == 3) {
  575. s->coded_format = AV_PIX_FMT_GBRP12;
  576. } else if (data == 4) {
  577. s->coded_format = AV_PIX_FMT_GBRAP12;
  578. } else {
  579. avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
  580. ret = AVERROR_PATCHWELCOME;
  581. break;
  582. }
  583. s->planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
  584. } else if (tag == -85) {
  585. av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
  586. s->cropped_height = data;
  587. } else if (tag == -75) {
  588. s->peak.offset &= ~0xffff;
  589. s->peak.offset |= (data & 0xffff);
  590. s->peak.base = gb;
  591. s->peak.level = 0;
  592. } else if (tag == -76) {
  593. s->peak.offset &= 0xffff;
  594. s->peak.offset |= (data & 0xffffU)<<16;
  595. s->peak.base = gb;
  596. s->peak.level = 0;
  597. } else if (tag == -74 && s->peak.offset) {
  598. s->peak.level = data;
  599. bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
  600. } else
  601. av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
  602. if (tag == BitstreamMarker && data == 0xf0f &&
  603. s->coded_format != AV_PIX_FMT_NONE) {
  604. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  605. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  606. int factor = s->coded_format == AV_PIX_FMT_BAYER_RGGB16 ? 2 : 1;
  607. if (s->coded_width) {
  608. s->coded_width *= factor;
  609. }
  610. if (s->coded_height) {
  611. s->coded_height *= factor;
  612. }
  613. if (!s->a_width && !s->coded_width) {
  614. s->coded_width = lowpass_width * factor * 8;
  615. }
  616. if (!s->a_height && !s->coded_height) {
  617. s->coded_height = lowpass_height * factor * 8;
  618. }
  619. if (s->a_width && !s->coded_width)
  620. s->coded_width = s->a_width;
  621. if (s->a_height && !s->coded_height)
  622. s->coded_height = s->a_height;
  623. if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
  624. s->a_format != s->coded_format) {
  625. free_buffers(s);
  626. if ((ret = alloc_buffers(avctx)) < 0) {
  627. free_buffers(s);
  628. return ret;
  629. }
  630. }
  631. ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  632. if (ret < 0)
  633. return ret;
  634. if (s->cropped_height) {
  635. unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
  636. if (avctx->height < height)
  637. return AVERROR_INVALIDDATA;
  638. avctx->height = height;
  639. }
  640. frame.f->width =
  641. frame.f->height = 0;
  642. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  643. return ret;
  644. s->coded_width = 0;
  645. s->coded_height = 0;
  646. s->coded_format = AV_PIX_FMT_NONE;
  647. got_buffer = 1;
  648. } else if (tag == FrameIndex && data == 1 && s->sample_type == 1 && s->frame_type == 2) {
  649. frame.f->width =
  650. frame.f->height = 0;
  651. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  652. return ret;
  653. s->coded_width = 0;
  654. s->coded_height = 0;
  655. s->coded_format = AV_PIX_FMT_NONE;
  656. got_buffer = 1;
  657. }
  658. if (s->subband_num_actual == 255)
  659. goto finish;
  660. coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  661. /* Lowpass coefficients */
  662. if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
  663. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  664. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  665. int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  666. int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
  667. if (lowpass_width < 3 ||
  668. lowpass_width > lowpass_a_width) {
  669. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
  670. ret = AVERROR(EINVAL);
  671. goto end;
  672. }
  673. if (lowpass_height < 3 ||
  674. lowpass_height > lowpass_a_height) {
  675. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
  676. ret = AVERROR(EINVAL);
  677. goto end;
  678. }
  679. if (!got_buffer) {
  680. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  681. ret = AVERROR(EINVAL);
  682. goto end;
  683. }
  684. if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  685. lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  686. av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  687. ret = AVERROR(EINVAL);
  688. goto end;
  689. }
  690. av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  691. for (i = 0; i < lowpass_height; i++) {
  692. for (j = 0; j < lowpass_width; j++)
  693. coeff_data[j] = bytestream2_get_be16u(&gb);
  694. coeff_data += lowpass_width;
  695. }
  696. /* Align to mod-4 position to continue reading tags */
  697. bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
  698. /* Copy last line of coefficients if odd height */
  699. if (lowpass_height & 1) {
  700. memcpy(&coeff_data[lowpass_height * lowpass_width],
  701. &coeff_data[(lowpass_height - 1) * lowpass_width],
  702. lowpass_width * sizeof(*coeff_data));
  703. }
  704. av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
  705. }
  706. if ((tag == BandHeader || tag == BandSecondPass) && s->subband_num_actual != 255 && s->a_width && s->a_height) {
  707. int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  708. int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  709. int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  710. int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  711. int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  712. int expected;
  713. int a_expected = highpass_a_height * highpass_a_width;
  714. int level, run, coeff;
  715. int count = 0, bytes;
  716. if (!got_buffer) {
  717. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  718. ret = AVERROR(EINVAL);
  719. goto end;
  720. }
  721. if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
  722. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
  723. ret = AVERROR(EINVAL);
  724. goto end;
  725. }
  726. expected = highpass_height * highpass_stride;
  727. av_log(avctx, AV_LOG_DEBUG, "Start subband coeffs plane %i level %i codebook %i expected %i\n", s->channel_num, s->level, s->codebook, expected);
  728. ret = init_get_bits8(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb));
  729. if (ret < 0)
  730. goto end;
  731. {
  732. OPEN_READER(re, &s->gb);
  733. const int lossless = s->band_encoding == 5;
  734. if (s->codebook == 0 && s->transform_type == 2 && s->subband_num_actual == 7)
  735. s->codebook = 1;
  736. if (!s->codebook) {
  737. while (1) {
  738. UPDATE_CACHE(re, &s->gb);
  739. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  740. VLC_BITS, 3, 1);
  741. /* escape */
  742. if (level == 64)
  743. break;
  744. count += run;
  745. if (count > expected)
  746. break;
  747. if (!lossless)
  748. coeff = dequant_and_decompand(s, level, s->quantisation, 0);
  749. else
  750. coeff = level;
  751. if (tag == BandSecondPass) {
  752. const uint16_t q = s->quantisation;
  753. for (i = 0; i < run; i++) {
  754. *coeff_data |= coeff << 8;
  755. *coeff_data++ *= q;
  756. }
  757. } else {
  758. for (i = 0; i < run; i++)
  759. *coeff_data++ = coeff;
  760. }
  761. }
  762. } else {
  763. while (1) {
  764. UPDATE_CACHE(re, &s->gb);
  765. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  766. VLC_BITS, 3, 1);
  767. /* escape */
  768. if (level == 255 && run == 2)
  769. break;
  770. count += run;
  771. if (count > expected)
  772. break;
  773. if (!lossless)
  774. coeff = dequant_and_decompand(s, level, s->quantisation, s->codebook);
  775. else
  776. coeff = level;
  777. if (tag == BandSecondPass) {
  778. const uint16_t q = s->quantisation;
  779. for (i = 0; i < run; i++) {
  780. *coeff_data |= coeff << 8;
  781. *coeff_data++ *= q;
  782. }
  783. } else {
  784. for (i = 0; i < run; i++)
  785. *coeff_data++ = coeff;
  786. }
  787. }
  788. }
  789. CLOSE_READER(re, &s->gb);
  790. }
  791. if (count > expected) {
  792. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  793. ret = AVERROR(EINVAL);
  794. goto end;
  795. }
  796. if (s->peak.level)
  797. peak_table(coeff_data - count, &s->peak, count);
  798. if (s->difference_coding)
  799. difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
  800. bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  801. if (bytes > bytestream2_get_bytes_left(&gb)) {
  802. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  803. ret = AVERROR(EINVAL);
  804. goto end;
  805. } else
  806. bytestream2_seek(&gb, bytes, SEEK_CUR);
  807. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  808. finish:
  809. if (s->subband_num_actual != 255)
  810. s->codebook = 0;
  811. /* Copy last line of coefficients if odd height */
  812. if (highpass_height & 1) {
  813. memcpy(&coeff_data[highpass_height * highpass_stride],
  814. &coeff_data[(highpass_height - 1) * highpass_stride],
  815. highpass_stride * sizeof(*coeff_data));
  816. }
  817. }
  818. }
  819. s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  820. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  821. s->progressive = 1;
  822. s->planes = 4;
  823. }
  824. ff_thread_finish_setup(avctx);
  825. if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  826. s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
  827. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  828. ret = AVERROR(EINVAL);
  829. goto end;
  830. }
  831. if (!got_buffer) {
  832. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  833. ret = AVERROR(EINVAL);
  834. goto end;
  835. }
  836. if (s->transform_type == 0 && s->sample_type != 1) {
  837. for (plane = 0; plane < s->planes && !ret; plane++) {
  838. /* level 1 */
  839. int lowpass_height = s->plane[plane].band[0][0].height;
  840. int lowpass_width = s->plane[plane].band[0][0].width;
  841. int highpass_stride = s->plane[plane].band[0][1].stride;
  842. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  843. ptrdiff_t dst_linesize;
  844. int16_t *low, *high, *output, *dst;
  845. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  846. act_plane = 0;
  847. dst_linesize = pic->linesize[act_plane];
  848. } else {
  849. dst_linesize = pic->linesize[act_plane] / 2;
  850. }
  851. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  852. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  853. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  854. ret = AVERROR(EINVAL);
  855. goto end;
  856. }
  857. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  858. low = s->plane[plane].subband[0];
  859. high = s->plane[plane].subband[2];
  860. output = s->plane[plane].l_h[0];
  861. for (i = 0; i < lowpass_width; i++) {
  862. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  863. low++;
  864. high++;
  865. output++;
  866. }
  867. low = s->plane[plane].subband[1];
  868. high = s->plane[plane].subband[3];
  869. output = s->plane[plane].l_h[1];
  870. for (i = 0; i < lowpass_width; i++) {
  871. // note the stride of "low" is highpass_stride
  872. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  873. low++;
  874. high++;
  875. output++;
  876. }
  877. low = s->plane[plane].l_h[0];
  878. high = s->plane[plane].l_h[1];
  879. output = s->plane[plane].subband[0];
  880. for (i = 0; i < lowpass_height * 2; i++) {
  881. horiz_filter(output, low, high, lowpass_width);
  882. low += lowpass_width;
  883. high += lowpass_width;
  884. output += lowpass_width * 2;
  885. }
  886. if (s->bpc == 12) {
  887. output = s->plane[plane].subband[0];
  888. for (i = 0; i < lowpass_height * 2; i++) {
  889. for (j = 0; j < lowpass_width * 2; j++)
  890. output[j] *= 4;
  891. output += lowpass_width * 2;
  892. }
  893. }
  894. /* level 2 */
  895. lowpass_height = s->plane[plane].band[1][1].height;
  896. lowpass_width = s->plane[plane].band[1][1].width;
  897. highpass_stride = s->plane[plane].band[1][1].stride;
  898. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  899. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  900. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  901. ret = AVERROR(EINVAL);
  902. goto end;
  903. }
  904. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  905. low = s->plane[plane].subband[0];
  906. high = s->plane[plane].subband[5];
  907. output = s->plane[plane].l_h[3];
  908. for (i = 0; i < lowpass_width; i++) {
  909. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  910. low++;
  911. high++;
  912. output++;
  913. }
  914. low = s->plane[plane].subband[4];
  915. high = s->plane[plane].subband[6];
  916. output = s->plane[plane].l_h[4];
  917. for (i = 0; i < lowpass_width; i++) {
  918. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  919. low++;
  920. high++;
  921. output++;
  922. }
  923. low = s->plane[plane].l_h[3];
  924. high = s->plane[plane].l_h[4];
  925. output = s->plane[plane].subband[0];
  926. for (i = 0; i < lowpass_height * 2; i++) {
  927. horiz_filter(output, low, high, lowpass_width);
  928. low += lowpass_width;
  929. high += lowpass_width;
  930. output += lowpass_width * 2;
  931. }
  932. output = s->plane[plane].subband[0];
  933. for (i = 0; i < lowpass_height * 2; i++) {
  934. for (j = 0; j < lowpass_width * 2; j++)
  935. output[j] *= 4;
  936. output += lowpass_width * 2;
  937. }
  938. /* level 3 */
  939. lowpass_height = s->plane[plane].band[2][1].height;
  940. lowpass_width = s->plane[plane].band[2][1].width;
  941. highpass_stride = s->plane[plane].band[2][1].stride;
  942. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  943. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  944. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  945. ret = AVERROR(EINVAL);
  946. goto end;
  947. }
  948. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  949. if (s->progressive) {
  950. low = s->plane[plane].subband[0];
  951. high = s->plane[plane].subband[8];
  952. output = s->plane[plane].l_h[6];
  953. for (i = 0; i < lowpass_width; i++) {
  954. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  955. low++;
  956. high++;
  957. output++;
  958. }
  959. low = s->plane[plane].subband[7];
  960. high = s->plane[plane].subband[9];
  961. output = s->plane[plane].l_h[7];
  962. for (i = 0; i < lowpass_width; i++) {
  963. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  964. low++;
  965. high++;
  966. output++;
  967. }
  968. dst = (int16_t *)pic->data[act_plane];
  969. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  970. if (plane & 1)
  971. dst++;
  972. if (plane > 1)
  973. dst += pic->linesize[act_plane] >> 1;
  974. }
  975. low = s->plane[plane].l_h[6];
  976. high = s->plane[plane].l_h[7];
  977. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  978. (lowpass_height * 2 > avctx->coded_height / 2 ||
  979. lowpass_width * 2 > avctx->coded_width / 2 )
  980. ) {
  981. ret = AVERROR_INVALIDDATA;
  982. goto end;
  983. }
  984. for (i = 0; i < lowpass_height * 2; i++) {
  985. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
  986. horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
  987. else
  988. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  989. if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
  990. process_alpha(dst, lowpass_width * 2);
  991. low += lowpass_width;
  992. high += lowpass_width;
  993. dst += dst_linesize;
  994. }
  995. } else {
  996. av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
  997. pic->interlaced_frame = 1;
  998. low = s->plane[plane].subband[0];
  999. high = s->plane[plane].subband[7];
  1000. output = s->plane[plane].l_h[6];
  1001. for (i = 0; i < lowpass_height; i++) {
  1002. horiz_filter(output, low, high, lowpass_width);
  1003. low += lowpass_width;
  1004. high += lowpass_width;
  1005. output += lowpass_width * 2;
  1006. }
  1007. low = s->plane[plane].subband[8];
  1008. high = s->plane[plane].subband[9];
  1009. output = s->plane[plane].l_h[7];
  1010. for (i = 0; i < lowpass_height; i++) {
  1011. horiz_filter(output, low, high, lowpass_width);
  1012. low += lowpass_width;
  1013. high += lowpass_width;
  1014. output += lowpass_width * 2;
  1015. }
  1016. dst = (int16_t *)pic->data[act_plane];
  1017. low = s->plane[plane].l_h[6];
  1018. high = s->plane[plane].l_h[7];
  1019. for (i = 0; i < lowpass_height; i++) {
  1020. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  1021. low += lowpass_width * 2;
  1022. high += lowpass_width * 2;
  1023. dst += pic->linesize[act_plane];
  1024. }
  1025. }
  1026. }
  1027. } else if (s->transform_type == 2 && (avctx->internal->is_copy || s->frame_index == 1 || s->sample_type != 1)) {
  1028. for (plane = 0; plane < s->planes && !ret; plane++) {
  1029. int lowpass_height = s->plane[plane].band[0][0].height;
  1030. int lowpass_width = s->plane[plane].band[0][0].width;
  1031. int highpass_stride = s->plane[plane].band[0][1].stride;
  1032. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  1033. int16_t *low, *high, *output, *dst;
  1034. ptrdiff_t dst_linesize;
  1035. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1036. act_plane = 0;
  1037. dst_linesize = pic->linesize[act_plane];
  1038. } else {
  1039. dst_linesize = pic->linesize[act_plane] / 2;
  1040. }
  1041. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  1042. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  1043. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1044. ret = AVERROR(EINVAL);
  1045. goto end;
  1046. }
  1047. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1048. low = s->plane[plane].subband[0];
  1049. high = s->plane[plane].subband[2];
  1050. output = s->plane[plane].l_h[0];
  1051. for (i = 0; i < lowpass_width; i++) {
  1052. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1053. low++;
  1054. high++;
  1055. output++;
  1056. }
  1057. low = s->plane[plane].subband[1];
  1058. high = s->plane[plane].subband[3];
  1059. output = s->plane[plane].l_h[1];
  1060. for (i = 0; i < lowpass_width; i++) {
  1061. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1062. low++;
  1063. high++;
  1064. output++;
  1065. }
  1066. low = s->plane[plane].l_h[0];
  1067. high = s->plane[plane].l_h[1];
  1068. output = s->plane[plane].l_h[7];
  1069. for (i = 0; i < lowpass_height * 2; i++) {
  1070. horiz_filter(output, low, high, lowpass_width);
  1071. low += lowpass_width;
  1072. high += lowpass_width;
  1073. output += lowpass_width * 2;
  1074. }
  1075. if (s->bpc == 12) {
  1076. output = s->plane[plane].l_h[7];
  1077. for (i = 0; i < lowpass_height * 2; i++) {
  1078. for (j = 0; j < lowpass_width * 2; j++)
  1079. output[j] *= 4;
  1080. output += lowpass_width * 2;
  1081. }
  1082. }
  1083. lowpass_height = s->plane[plane].band[1][1].height;
  1084. lowpass_width = s->plane[plane].band[1][1].width;
  1085. highpass_stride = s->plane[plane].band[1][1].stride;
  1086. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  1087. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  1088. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1089. ret = AVERROR(EINVAL);
  1090. goto end;
  1091. }
  1092. av_log(avctx, AV_LOG_DEBUG, "Level 2 lowpass plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1093. low = s->plane[plane].l_h[7];
  1094. high = s->plane[plane].subband[5];
  1095. output = s->plane[plane].l_h[3];
  1096. for (i = 0; i < lowpass_width; i++) {
  1097. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1098. low++;
  1099. high++;
  1100. output++;
  1101. }
  1102. low = s->plane[plane].subband[4];
  1103. high = s->plane[plane].subband[6];
  1104. output = s->plane[plane].l_h[4];
  1105. for (i = 0; i < lowpass_width; i++) {
  1106. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1107. low++;
  1108. high++;
  1109. output++;
  1110. }
  1111. low = s->plane[plane].l_h[3];
  1112. high = s->plane[plane].l_h[4];
  1113. output = s->plane[plane].l_h[7];
  1114. for (i = 0; i < lowpass_height * 2; i++) {
  1115. horiz_filter(output, low, high, lowpass_width);
  1116. low += lowpass_width;
  1117. high += lowpass_width;
  1118. output += lowpass_width * 2;
  1119. }
  1120. output = s->plane[plane].l_h[7];
  1121. for (i = 0; i < lowpass_height * 2; i++) {
  1122. for (j = 0; j < lowpass_width * 2; j++)
  1123. output[j] *= 4;
  1124. output += lowpass_width * 2;
  1125. }
  1126. low = s->plane[plane].subband[7];
  1127. high = s->plane[plane].subband[9];
  1128. output = s->plane[plane].l_h[3];
  1129. for (i = 0; i < lowpass_width; i++) {
  1130. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1131. low++;
  1132. high++;
  1133. output++;
  1134. }
  1135. low = s->plane[plane].subband[8];
  1136. high = s->plane[plane].subband[10];
  1137. output = s->plane[plane].l_h[4];
  1138. for (i = 0; i < lowpass_width; i++) {
  1139. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1140. low++;
  1141. high++;
  1142. output++;
  1143. }
  1144. low = s->plane[plane].l_h[3];
  1145. high = s->plane[plane].l_h[4];
  1146. output = s->plane[plane].l_h[9];
  1147. for (i = 0; i < lowpass_height * 2; i++) {
  1148. horiz_filter(output, low, high, lowpass_width);
  1149. low += lowpass_width;
  1150. high += lowpass_width;
  1151. output += lowpass_width * 2;
  1152. }
  1153. lowpass_height = s->plane[plane].band[4][1].height;
  1154. lowpass_width = s->plane[plane].band[4][1].width;
  1155. highpass_stride = s->plane[plane].band[4][1].stride;
  1156. av_log(avctx, AV_LOG_DEBUG, "temporal level %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  1157. if (lowpass_height > s->plane[plane].band[4][1].a_height || lowpass_width > s->plane[plane].band[4][1].a_width ||
  1158. !highpass_stride || s->plane[plane].band[4][1].width > s->plane[plane].band[4][1].a_width) {
  1159. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  1160. ret = AVERROR(EINVAL);
  1161. goto end;
  1162. }
  1163. low = s->plane[plane].l_h[7];
  1164. high = s->plane[plane].l_h[9];
  1165. output = s->plane[plane].l_h[7];
  1166. for (i = 0; i < lowpass_height; i++) {
  1167. inverse_temporal_filter(output, low, high, lowpass_width);
  1168. low += lowpass_width;
  1169. high += lowpass_width;
  1170. }
  1171. if (s->progressive) {
  1172. low = s->plane[plane].l_h[7];
  1173. high = s->plane[plane].subband[15];
  1174. output = s->plane[plane].l_h[6];
  1175. for (i = 0; i < lowpass_width; i++) {
  1176. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1177. low++;
  1178. high++;
  1179. output++;
  1180. }
  1181. low = s->plane[plane].subband[14];
  1182. high = s->plane[plane].subband[16];
  1183. output = s->plane[plane].l_h[7];
  1184. for (i = 0; i < lowpass_width; i++) {
  1185. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1186. low++;
  1187. high++;
  1188. output++;
  1189. }
  1190. low = s->plane[plane].l_h[9];
  1191. high = s->plane[plane].subband[12];
  1192. output = s->plane[plane].l_h[8];
  1193. for (i = 0; i < lowpass_width; i++) {
  1194. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  1195. low++;
  1196. high++;
  1197. output++;
  1198. }
  1199. low = s->plane[plane].subband[11];
  1200. high = s->plane[plane].subband[13];
  1201. output = s->plane[plane].l_h[9];
  1202. for (i = 0; i < lowpass_width; i++) {
  1203. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  1204. low++;
  1205. high++;
  1206. output++;
  1207. }
  1208. if (s->sample_type == 1)
  1209. continue;
  1210. dst = (int16_t *)pic->data[act_plane];
  1211. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1212. if (plane & 1)
  1213. dst++;
  1214. if (plane > 1)
  1215. dst += pic->linesize[act_plane] >> 1;
  1216. }
  1217. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  1218. (lowpass_height * 2 > avctx->coded_height / 2 ||
  1219. lowpass_width * 2 > avctx->coded_width / 2 )
  1220. ) {
  1221. ret = AVERROR_INVALIDDATA;
  1222. goto end;
  1223. }
  1224. low = s->plane[plane].l_h[6];
  1225. high = s->plane[plane].l_h[7];
  1226. for (i = 0; i < lowpass_height * 2; i++) {
  1227. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
  1228. horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
  1229. else
  1230. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  1231. low += lowpass_width;
  1232. high += lowpass_width;
  1233. dst += dst_linesize;
  1234. }
  1235. } else {
  1236. pic->interlaced_frame = 1;
  1237. low = s->plane[plane].l_h[7];
  1238. high = s->plane[plane].subband[14];
  1239. output = s->plane[plane].l_h[6];
  1240. for (i = 0; i < lowpass_height; i++) {
  1241. horiz_filter(output, low, high, lowpass_width);
  1242. low += lowpass_width;
  1243. high += lowpass_width;
  1244. output += lowpass_width * 2;
  1245. }
  1246. low = s->plane[plane].subband[15];
  1247. high = s->plane[plane].subband[16];
  1248. output = s->plane[plane].l_h[7];
  1249. for (i = 0; i < lowpass_height; i++) {
  1250. horiz_filter(output, low, high, lowpass_width);
  1251. low += lowpass_width;
  1252. high += lowpass_width;
  1253. output += lowpass_width * 2;
  1254. }
  1255. low = s->plane[plane].l_h[9];
  1256. high = s->plane[plane].subband[11];
  1257. output = s->plane[plane].l_h[8];
  1258. for (i = 0; i < lowpass_height; i++) {
  1259. horiz_filter(output, low, high, lowpass_width);
  1260. low += lowpass_width;
  1261. high += lowpass_width;
  1262. output += lowpass_width * 2;
  1263. }
  1264. low = s->plane[plane].subband[12];
  1265. high = s->plane[plane].subband[13];
  1266. output = s->plane[plane].l_h[9];
  1267. for (i = 0; i < lowpass_height; i++) {
  1268. horiz_filter(output, low, high, lowpass_width);
  1269. low += lowpass_width;
  1270. high += lowpass_width;
  1271. output += lowpass_width * 2;
  1272. }
  1273. if (s->sample_type == 1)
  1274. continue;
  1275. dst = (int16_t *)pic->data[act_plane];
  1276. low = s->plane[plane].l_h[6];
  1277. high = s->plane[plane].l_h[7];
  1278. for (i = 0; i < lowpass_height; i++) {
  1279. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  1280. low += lowpass_width * 2;
  1281. high += lowpass_width * 2;
  1282. dst += pic->linesize[act_plane];
  1283. }
  1284. }
  1285. }
  1286. }
  1287. if (s->transform_type == 2 && s->sample_type == 1) {
  1288. int16_t *low, *high, *dst;
  1289. int lowpass_height, lowpass_width, highpass_stride;
  1290. ptrdiff_t dst_linesize;
  1291. for (plane = 0; plane < s->planes; plane++) {
  1292. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  1293. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1294. act_plane = 0;
  1295. dst_linesize = pic->linesize[act_plane];
  1296. } else {
  1297. dst_linesize = pic->linesize[act_plane] / 2;
  1298. }
  1299. lowpass_height = s->plane[plane].band[4][1].height;
  1300. lowpass_width = s->plane[plane].band[4][1].width;
  1301. highpass_stride = s->plane[plane].band[4][1].stride;
  1302. if (s->progressive) {
  1303. dst = (int16_t *)pic->data[act_plane];
  1304. low = s->plane[plane].l_h[8];
  1305. high = s->plane[plane].l_h[9];
  1306. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  1307. if (plane & 1)
  1308. dst++;
  1309. if (plane > 1)
  1310. dst += pic->linesize[act_plane] >> 1;
  1311. }
  1312. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  1313. (lowpass_height * 2 > avctx->coded_height / 2 ||
  1314. lowpass_width * 2 > avctx->coded_width / 2 )
  1315. ) {
  1316. ret = AVERROR_INVALIDDATA;
  1317. goto end;
  1318. }
  1319. for (i = 0; i < lowpass_height * 2; i++) {
  1320. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
  1321. horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
  1322. else
  1323. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  1324. low += lowpass_width;
  1325. high += lowpass_width;
  1326. dst += dst_linesize;
  1327. }
  1328. } else {
  1329. dst = (int16_t *)pic->data[act_plane];
  1330. low = s->plane[plane].l_h[8];
  1331. high = s->plane[plane].l_h[9];
  1332. for (i = 0; i < lowpass_height; i++) {
  1333. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  1334. low += lowpass_width * 2;
  1335. high += lowpass_width * 2;
  1336. dst += pic->linesize[act_plane];
  1337. }
  1338. }
  1339. }
  1340. }
  1341. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
  1342. process_bayer(pic, s->bpc);
  1343. end:
  1344. if (ret < 0)
  1345. return ret;
  1346. *got_frame = 1;
  1347. return avpkt->size;
  1348. }
  1349. static av_cold int cfhd_close(AVCodecContext *avctx)
  1350. {
  1351. CFHDContext *s = avctx->priv_data;
  1352. free_buffers(s);
  1353. ff_free_vlc(&s->vlc_9);
  1354. ff_free_vlc(&s->vlc_18);
  1355. return 0;
  1356. }
  1357. #if HAVE_THREADS
  1358. static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1359. {
  1360. CFHDContext *psrc = src->priv_data;
  1361. CFHDContext *pdst = dst->priv_data;
  1362. int ret;
  1363. if (dst == src || psrc->transform_type == 0)
  1364. return 0;
  1365. pdst->a_format = psrc->a_format;
  1366. pdst->a_width = psrc->a_width;
  1367. pdst->a_height = psrc->a_height;
  1368. pdst->transform_type = psrc->transform_type;
  1369. pdst->progressive = psrc->progressive;
  1370. pdst->planes = psrc->planes;
  1371. if (!pdst->plane[0].idwt_buf) {
  1372. pdst->coded_width = pdst->a_width;
  1373. pdst->coded_height = pdst->a_height;
  1374. pdst->coded_format = pdst->a_format;
  1375. ret = alloc_buffers(dst);
  1376. if (ret < 0)
  1377. return ret;
  1378. }
  1379. for (int plane = 0; plane < pdst->planes; plane++) {
  1380. memcpy(pdst->plane[plane].band, psrc->plane[plane].band, sizeof(pdst->plane[plane].band));
  1381. memcpy(pdst->plane[plane].idwt_buf, psrc->plane[plane].idwt_buf,
  1382. pdst->plane[plane].idwt_size * sizeof(int16_t));
  1383. }
  1384. return 0;
  1385. }
  1386. #endif
  1387. AVCodec ff_cfhd_decoder = {
  1388. .name = "cfhd",
  1389. .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
  1390. .type = AVMEDIA_TYPE_VIDEO,
  1391. .id = AV_CODEC_ID_CFHD,
  1392. .priv_data_size = sizeof(CFHDContext),
  1393. .init = cfhd_init,
  1394. .close = cfhd_close,
  1395. .decode = cfhd_decode,
  1396. .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
  1397. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  1398. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  1399. };