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.

1040 lines
39KB

  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. avctx->bits_per_raw_sample = 10;
  42. s->avctx = avctx;
  43. return ff_cfhd_init_vlcs(s);
  44. }
  45. static void init_plane_defaults(CFHDContext *s)
  46. {
  47. s->subband_num = 0;
  48. s->level = 0;
  49. s->subband_num_actual = 0;
  50. }
  51. static void init_peak_table_defaults(CFHDContext *s)
  52. {
  53. s->peak.level = 0;
  54. s->peak.offset = 0;
  55. memset(&s->peak.base, 0, sizeof(s->peak.base));
  56. }
  57. static void init_frame_defaults(CFHDContext *s)
  58. {
  59. s->coded_width = 0;
  60. s->coded_height = 0;
  61. s->cropped_height = 0;
  62. s->bpc = 10;
  63. s->channel_cnt = 4;
  64. s->subband_cnt = SUBBAND_COUNT;
  65. s->channel_num = 0;
  66. s->lowpass_precision = 16;
  67. s->quantisation = 1;
  68. s->wavelet_depth = 3;
  69. s->pshift = 1;
  70. s->codebook = 0;
  71. s->difference_coding = 0;
  72. s->progressive = 0;
  73. init_plane_defaults(s);
  74. init_peak_table_defaults(s);
  75. }
  76. /* TODO: merge with VLC tables or use LUT */
  77. static inline int dequant_and_decompand(int level, int quantisation, int codebook)
  78. {
  79. if (codebook == 0 || codebook == 1) {
  80. int64_t abslevel = abs(level);
  81. if (abslevel < 256)
  82. return (abslevel + ((768 * abslevel * abslevel * abslevel) / (256 * 256 * 256))) *
  83. FFSIGN(level) * quantisation;
  84. else
  85. return level * quantisation;
  86. } else
  87. return level * quantisation;
  88. }
  89. static inline void difference_coding(int16_t *band, int width, int height)
  90. {
  91. int i,j;
  92. for (i = 0; i < height; i++) {
  93. for (j = 1; j < width; j++) {
  94. band[j] += band[j-1];
  95. }
  96. band += width;
  97. }
  98. }
  99. static inline void peak_table(int16_t *band, Peak *peak, int length)
  100. {
  101. int i;
  102. for (i = 0; i < length; i++)
  103. if (abs(band[i]) > peak->level)
  104. band[i] = bytestream2_get_le16(&peak->base);
  105. }
  106. static inline void process_alpha(int16_t *alpha, int width)
  107. {
  108. int i, channel;
  109. for (i = 0; i < width; i++) {
  110. channel = alpha[i];
  111. channel -= ALPHA_COMPAND_DC_OFFSET;
  112. channel <<= 3;
  113. channel *= ALPHA_COMPAND_GAIN;
  114. channel >>= 16;
  115. channel = av_clip_uintp2(channel, 12);
  116. alpha[i] = channel;
  117. }
  118. }
  119. static inline void process_bayer(AVFrame *frame)
  120. {
  121. const int linesize = frame->linesize[0];
  122. uint16_t *r = (uint16_t *)frame->data[0];
  123. uint16_t *g1 = (uint16_t *)(frame->data[0] + 2);
  124. uint16_t *g2 = (uint16_t *)(frame->data[0] + frame->linesize[0]);
  125. uint16_t *b = (uint16_t *)(frame->data[0] + frame->linesize[0] + 2);
  126. const int mid = 2048;
  127. for (int y = 0; y < frame->height >> 1; y++) {
  128. for (int x = 0; x < frame->width; x += 2) {
  129. int R, G1, G2, B;
  130. int g, rg, bg, gd;
  131. g = r[x];
  132. rg = g1[x];
  133. bg = g2[x];
  134. gd = b[x];
  135. gd -= mid;
  136. R = (rg - mid) * 2 + g;
  137. G1 = g + gd;
  138. G2 = g - gd;
  139. B = (bg - mid) * 2 + g;
  140. R = av_clip_uintp2(R * 16, 16);
  141. G1 = av_clip_uintp2(G1 * 16, 16);
  142. G2 = av_clip_uintp2(G2 * 16, 16);
  143. B = av_clip_uintp2(B * 16, 16);
  144. r[x] = R;
  145. g1[x] = G1;
  146. g2[x] = G2;
  147. b[x] = B;
  148. }
  149. r += linesize;
  150. g1 += linesize;
  151. g2 += linesize;
  152. b += linesize;
  153. }
  154. }
  155. static inline void filter(int16_t *output, ptrdiff_t out_stride,
  156. int16_t *low, ptrdiff_t low_stride,
  157. int16_t *high, ptrdiff_t high_stride,
  158. int len, int clip)
  159. {
  160. int16_t tmp;
  161. int i;
  162. tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
  163. output[(2*0+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
  164. if (clip)
  165. output[(2*0+0)*out_stride] = av_clip_uintp2_c(output[(2*0+0)*out_stride], clip);
  166. tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
  167. output[(2*0+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
  168. if (clip)
  169. output[(2*0+1)*out_stride] = av_clip_uintp2_c(output[(2*0+1)*out_stride], clip);
  170. for (i = 1; i < len - 1; i++) {
  171. tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
  172. output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
  173. if (clip)
  174. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  175. tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
  176. output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
  177. if (clip)
  178. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  179. }
  180. tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
  181. output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
  182. if (clip)
  183. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  184. tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
  185. output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
  186. if (clip)
  187. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  188. }
  189. static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
  190. int width, int linesize, int plane)
  191. {
  192. int i;
  193. int16_t even, odd;
  194. for (i = 0; i < width; i++) {
  195. even = (low[i] - high[i])/2;
  196. odd = (low[i] + high[i])/2;
  197. output[i] = av_clip_uintp2(even, 10);
  198. output[i + linesize] = av_clip_uintp2(odd, 10);
  199. }
  200. }
  201. static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
  202. int width)
  203. {
  204. filter(output, 1, low, 1, high, 1, width, 0);
  205. }
  206. static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
  207. int width, int clip)
  208. {
  209. filter(output, 1, low, 1, high, 1, width, clip);
  210. }
  211. static void horiz_filter_clip_bayer(int16_t *output, int16_t *low, int16_t *high,
  212. int width, int clip)
  213. {
  214. filter(output, 2, low, 1, high, 1, width, clip);
  215. }
  216. static void vert_filter(int16_t *output, ptrdiff_t out_stride,
  217. int16_t *low, ptrdiff_t low_stride,
  218. int16_t *high, ptrdiff_t high_stride, int len)
  219. {
  220. filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
  221. }
  222. static void free_buffers(CFHDContext *s)
  223. {
  224. int i, j;
  225. for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
  226. av_freep(&s->plane[i].idwt_buf);
  227. av_freep(&s->plane[i].idwt_tmp);
  228. for (j = 0; j < 9; j++)
  229. s->plane[i].subband[j] = NULL;
  230. for (j = 0; j < 8; j++)
  231. s->plane[i].l_h[j] = NULL;
  232. }
  233. s->a_height = 0;
  234. s->a_width = 0;
  235. }
  236. static int alloc_buffers(AVCodecContext *avctx)
  237. {
  238. CFHDContext *s = avctx->priv_data;
  239. int i, j, ret, planes;
  240. int chroma_x_shift, chroma_y_shift;
  241. unsigned k;
  242. if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
  243. s->coded_width *= 2;
  244. s->coded_height *= 2;
  245. }
  246. if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
  247. return ret;
  248. avctx->pix_fmt = s->coded_format;
  249. if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
  250. &chroma_x_shift,
  251. &chroma_y_shift)) < 0)
  252. return ret;
  253. planes = av_pix_fmt_count_planes(s->coded_format);
  254. if (s->coded_format == AV_PIX_FMT_BAYER_RGGB16) {
  255. planes = 4;
  256. chroma_x_shift = 1;
  257. chroma_y_shift = 1;
  258. }
  259. for (i = 0; i < planes; i++) {
  260. int w8, h8, w4, h4, w2, h2;
  261. int width = i ? avctx->width >> chroma_x_shift : avctx->width;
  262. int height = i ? avctx->height >> chroma_y_shift : avctx->height;
  263. ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
  264. if (chroma_y_shift)
  265. height = FFALIGN(height / 8, 2) * 8;
  266. s->plane[i].width = width;
  267. s->plane[i].height = height;
  268. s->plane[i].stride = stride;
  269. w8 = FFALIGN(s->plane[i].width / 8, 8);
  270. h8 = FFALIGN(height, 8) / 8;
  271. w4 = w8 * 2;
  272. h4 = h8 * 2;
  273. w2 = w4 * 2;
  274. h2 = h4 * 2;
  275. s->plane[i].idwt_buf =
  276. av_mallocz_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_buf));
  277. s->plane[i].idwt_tmp =
  278. av_malloc_array(FFALIGN(height, 8) * stride, sizeof(*s->plane[i].idwt_tmp));
  279. if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
  280. return AVERROR(ENOMEM);
  281. s->plane[i].subband[0] = s->plane[i].idwt_buf;
  282. s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
  283. s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
  284. s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
  285. s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
  286. s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
  287. s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
  288. s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
  289. s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
  290. s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
  291. for (j = 0; j < DWT_LEVELS; j++) {
  292. for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  293. s->plane[i].band[j][k].a_width = w8 << j;
  294. s->plane[i].band[j][k].a_height = h8 << j;
  295. }
  296. }
  297. /* ll2 and ll1 commented out because they are done in-place */
  298. s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
  299. s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
  300. // s->plane[i].l_h[2] = ll2;
  301. s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
  302. s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
  303. // s->plane[i].l_h[5] = ll1;
  304. s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
  305. s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
  306. }
  307. s->a_height = s->coded_height;
  308. s->a_width = s->coded_width;
  309. s->a_format = s->coded_format;
  310. return 0;
  311. }
  312. static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
  313. AVPacket *avpkt)
  314. {
  315. CFHDContext *s = avctx->priv_data;
  316. GetByteContext gb;
  317. ThreadFrame frame = { .f = data };
  318. AVFrame *pic = data;
  319. int ret = 0, i, j, planes, plane, got_buffer = 0;
  320. int16_t *coeff_data;
  321. s->coded_format = AV_PIX_FMT_YUV422P10;
  322. init_frame_defaults(s);
  323. planes = av_pix_fmt_count_planes(s->coded_format);
  324. bytestream2_init(&gb, avpkt->data, avpkt->size);
  325. while (bytestream2_get_bytes_left(&gb) > 4) {
  326. /* Bit weird but implement the tag parsing as the spec says */
  327. uint16_t tagu = bytestream2_get_be16(&gb);
  328. int16_t tag = (int16_t)tagu;
  329. int8_t tag8 = (int8_t)(tagu >> 8);
  330. uint16_t abstag = abs(tag);
  331. int8_t abs_tag8 = abs(tag8);
  332. uint16_t data = bytestream2_get_be16(&gb);
  333. if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
  334. av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
  335. } else if (tag == SampleFlags) {
  336. av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
  337. s->progressive = data & 0x0001;
  338. } else if (tag == ImageWidth) {
  339. av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
  340. s->coded_width = data;
  341. } else if (tag == ImageHeight) {
  342. av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
  343. s->coded_height = data;
  344. } else if (tag == 101) {
  345. av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
  346. if (data < 1 || data > 31) {
  347. av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
  348. ret = AVERROR(EINVAL);
  349. break;
  350. }
  351. s->bpc = data;
  352. } else if (tag == ChannelCount) {
  353. av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
  354. s->channel_cnt = data;
  355. if (data > 4) {
  356. av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
  357. ret = AVERROR_PATCHWELCOME;
  358. break;
  359. }
  360. } else if (tag == SubbandCount) {
  361. av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
  362. if (data != SUBBAND_COUNT) {
  363. av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
  364. ret = AVERROR_PATCHWELCOME;
  365. break;
  366. }
  367. } else if (tag == ChannelNumber) {
  368. s->channel_num = data;
  369. av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
  370. if (s->channel_num >= planes) {
  371. av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
  372. ret = AVERROR(EINVAL);
  373. break;
  374. }
  375. init_plane_defaults(s);
  376. } else if (tag == SubbandNumber) {
  377. if (s->subband_num != 0 && data == 1) // hack
  378. s->level++;
  379. av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
  380. s->subband_num = data;
  381. if (s->level >= DWT_LEVELS) {
  382. av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
  383. ret = AVERROR(EINVAL);
  384. break;
  385. }
  386. if (s->subband_num > 3) {
  387. av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
  388. ret = AVERROR(EINVAL);
  389. break;
  390. }
  391. } else if (tag == 51) {
  392. av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
  393. s->subband_num_actual = data;
  394. if (s->subband_num_actual >= 10) {
  395. av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
  396. ret = AVERROR(EINVAL);
  397. break;
  398. }
  399. } else if (tag == LowpassPrecision)
  400. av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
  401. else if (tag == Quantization) {
  402. s->quantisation = data;
  403. av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
  404. } else if (tag == PrescaleShift) {
  405. s->prescale_shift[0] = (data >> 0) & 0x7;
  406. s->prescale_shift[1] = (data >> 3) & 0x7;
  407. s->prescale_shift[2] = (data >> 6) & 0x7;
  408. av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
  409. } else if (tag == LowpassWidth) {
  410. av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
  411. if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
  412. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
  413. ret = AVERROR(EINVAL);
  414. break;
  415. }
  416. s->plane[s->channel_num].band[0][0].width = data;
  417. s->plane[s->channel_num].band[0][0].stride = data;
  418. } else if (tag == LowpassHeight) {
  419. av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
  420. if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
  421. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
  422. ret = AVERROR(EINVAL);
  423. break;
  424. }
  425. s->plane[s->channel_num].band[0][0].height = data;
  426. } else if (tag == SampleType)
  427. av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
  428. else if (tag == TransformType) {
  429. if (data != 0) {
  430. avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
  431. ret = AVERROR_PATCHWELCOME;
  432. break;
  433. }
  434. av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
  435. } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
  436. if (abstag == 0x4001)
  437. s->peak.level = 0;
  438. av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
  439. bytestream2_skipu(&gb, data * 4);
  440. } else if (tag == 23) {
  441. av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
  442. avpriv_report_missing_feature(avctx, "Skip frame");
  443. ret = AVERROR_PATCHWELCOME;
  444. break;
  445. } else if (tag == SampleIndexTable) {
  446. av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
  447. if (data > bytestream2_get_bytes_left(&gb) / 4) {
  448. av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
  449. ret = AVERROR_INVALIDDATA;
  450. break;
  451. }
  452. for (i = 0; i < data; i++) {
  453. uint16_t tag2 = bytestream2_get_be16(&gb);
  454. uint16_t val2 = bytestream2_get_be16(&gb);
  455. av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
  456. }
  457. } else if (tag == HighpassWidth) {
  458. 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);
  459. if (data < 3) {
  460. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
  461. ret = AVERROR(EINVAL);
  462. break;
  463. }
  464. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  465. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  466. } else if (tag == HighpassHeight) {
  467. av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
  468. if (data < 3) {
  469. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
  470. ret = AVERROR(EINVAL);
  471. break;
  472. }
  473. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  474. } else if (tag == 49) {
  475. av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
  476. if (data < 3) {
  477. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
  478. ret = AVERROR(EINVAL);
  479. break;
  480. }
  481. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  482. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  483. } else if (tag == 50) {
  484. av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
  485. if (data < 3) {
  486. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
  487. ret = AVERROR(EINVAL);
  488. break;
  489. }
  490. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  491. } else if (tag == 71) {
  492. s->codebook = data;
  493. av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
  494. } else if (tag == 72) {
  495. s->codebook = data & 0xf;
  496. s->difference_coding = (data >> 4) & 1;
  497. av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
  498. } else if (tag == 70) {
  499. av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
  500. if (!(data == 10 || data == 12)) {
  501. av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
  502. ret = AVERROR(EINVAL);
  503. break;
  504. }
  505. s->bpc = data;
  506. } else if (tag == EncodedFormat) {
  507. av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
  508. if (data == 1) {
  509. s->coded_format = AV_PIX_FMT_YUV422P10;
  510. } else if (data == 2) {
  511. s->coded_format = AV_PIX_FMT_BAYER_RGGB16;
  512. } else if (data == 3) {
  513. s->coded_format = AV_PIX_FMT_GBRP12;
  514. } else if (data == 4) {
  515. s->coded_format = AV_PIX_FMT_GBRAP12;
  516. } else {
  517. avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
  518. ret = AVERROR_PATCHWELCOME;
  519. break;
  520. }
  521. planes = data == 2 ? 4 : av_pix_fmt_count_planes(s->coded_format);
  522. } else if (tag == -85) {
  523. av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
  524. s->cropped_height = data;
  525. } else if (tag == -75) {
  526. s->peak.offset &= ~0xffff;
  527. s->peak.offset |= (data & 0xffff);
  528. s->peak.base = gb;
  529. s->peak.level = 0;
  530. } else if (tag == -76) {
  531. s->peak.offset &= 0xffff;
  532. s->peak.offset |= (data & 0xffffU)<<16;
  533. s->peak.base = gb;
  534. s->peak.level = 0;
  535. } else if (tag == -74 && s->peak.offset) {
  536. s->peak.level = data;
  537. bytestream2_seek(&s->peak.base, s->peak.offset - 4, SEEK_CUR);
  538. } else
  539. av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
  540. /* Some kind of end of header tag */
  541. if (tag == BitstreamMarker && data == 0x1a4a && s->coded_width && s->coded_height &&
  542. s->coded_format != AV_PIX_FMT_NONE) {
  543. if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
  544. s->a_format != s->coded_format) {
  545. free_buffers(s);
  546. if ((ret = alloc_buffers(avctx)) < 0) {
  547. free_buffers(s);
  548. return ret;
  549. }
  550. }
  551. ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  552. if (ret < 0)
  553. return ret;
  554. if (s->cropped_height) {
  555. unsigned height = s->cropped_height << (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16);
  556. if (avctx->height < height)
  557. return AVERROR_INVALIDDATA;
  558. avctx->height = height;
  559. }
  560. frame.f->width =
  561. frame.f->height = 0;
  562. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  563. return ret;
  564. s->coded_width = 0;
  565. s->coded_height = 0;
  566. s->coded_format = AV_PIX_FMT_NONE;
  567. got_buffer = 1;
  568. }
  569. coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  570. /* Lowpass coefficients */
  571. if (tag == BitstreamMarker && data == 0xf0f && s->a_width && s->a_height) {
  572. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  573. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  574. int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  575. int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
  576. if (!got_buffer) {
  577. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  578. ret = AVERROR(EINVAL);
  579. goto end;
  580. }
  581. if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  582. lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  583. av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  584. ret = AVERROR(EINVAL);
  585. goto end;
  586. }
  587. av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  588. for (i = 0; i < lowpass_height; i++) {
  589. for (j = 0; j < lowpass_width; j++)
  590. coeff_data[j] = bytestream2_get_be16u(&gb);
  591. coeff_data += lowpass_width;
  592. }
  593. /* Align to mod-4 position to continue reading tags */
  594. bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
  595. /* Copy last line of coefficients if odd height */
  596. if (lowpass_height & 1) {
  597. memcpy(&coeff_data[lowpass_height * lowpass_width],
  598. &coeff_data[(lowpass_height - 1) * lowpass_width],
  599. lowpass_width * sizeof(*coeff_data));
  600. }
  601. av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
  602. }
  603. if (tag == BandHeader && s->subband_num_actual != 255 && s->a_width && s->a_height) {
  604. int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  605. int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  606. int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  607. int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  608. int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  609. int expected;
  610. int a_expected = highpass_a_height * highpass_a_width;
  611. int level, run, coeff;
  612. int count = 0, bytes;
  613. if (!got_buffer) {
  614. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  615. ret = AVERROR(EINVAL);
  616. goto end;
  617. }
  618. if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
  619. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
  620. ret = AVERROR(EINVAL);
  621. goto end;
  622. }
  623. expected = highpass_height * highpass_stride;
  624. 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);
  625. init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
  626. {
  627. OPEN_READER(re, &s->gb);
  628. if (!s->codebook) {
  629. while (1) {
  630. UPDATE_CACHE(re, &s->gb);
  631. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  632. VLC_BITS, 3, 1);
  633. /* escape */
  634. if (level == 64)
  635. break;
  636. count += run;
  637. if (count > expected)
  638. break;
  639. coeff = dequant_and_decompand(level, s->quantisation, 0);
  640. for (i = 0; i < run; i++)
  641. *coeff_data++ = coeff;
  642. }
  643. } else {
  644. while (1) {
  645. UPDATE_CACHE(re, &s->gb);
  646. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  647. VLC_BITS, 3, 1);
  648. /* escape */
  649. if (level == 255 && run == 2)
  650. break;
  651. count += run;
  652. if (count > expected)
  653. break;
  654. coeff = dequant_and_decompand(level, s->quantisation, s->codebook);
  655. for (i = 0; i < run; i++)
  656. *coeff_data++ = coeff;
  657. }
  658. }
  659. CLOSE_READER(re, &s->gb);
  660. }
  661. if (count > expected) {
  662. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  663. ret = AVERROR(EINVAL);
  664. goto end;
  665. }
  666. if (s->peak.level)
  667. peak_table(coeff_data - count, &s->peak, count);
  668. if (s->difference_coding)
  669. difference_coding(s->plane[s->channel_num].subband[s->subband_num_actual], highpass_width, highpass_height);
  670. bytes = FFALIGN(AV_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  671. if (bytes > bytestream2_get_bytes_left(&gb)) {
  672. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  673. ret = AVERROR(EINVAL);
  674. goto end;
  675. } else
  676. bytestream2_seek(&gb, bytes, SEEK_CUR);
  677. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  678. s->codebook = 0;
  679. /* Copy last line of coefficients if odd height */
  680. if (highpass_height & 1) {
  681. memcpy(&coeff_data[highpass_height * highpass_stride],
  682. &coeff_data[(highpass_height - 1) * highpass_stride],
  683. highpass_stride * sizeof(*coeff_data));
  684. }
  685. }
  686. }
  687. if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  688. s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
  689. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  690. ret = AVERROR(EINVAL);
  691. goto end;
  692. }
  693. if (!got_buffer) {
  694. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  695. ret = AVERROR(EINVAL);
  696. goto end;
  697. }
  698. planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  699. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  700. if (!s->progressive)
  701. return AVERROR_INVALIDDATA;
  702. planes = 4;
  703. }
  704. for (plane = 0; plane < planes && !ret; plane++) {
  705. /* level 1 */
  706. int lowpass_height = s->plane[plane].band[0][0].height;
  707. int lowpass_width = s->plane[plane].band[0][0].width;
  708. int highpass_stride = s->plane[plane].band[0][1].stride;
  709. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  710. ptrdiff_t dst_linesize;
  711. int16_t *low, *high, *output, *dst;
  712. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  713. act_plane = 0;
  714. dst_linesize = pic->linesize[act_plane];
  715. } else {
  716. dst_linesize = pic->linesize[act_plane] / 2;
  717. }
  718. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  719. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  720. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  721. ret = AVERROR(EINVAL);
  722. goto end;
  723. }
  724. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  725. low = s->plane[plane].subband[0];
  726. high = s->plane[plane].subband[2];
  727. output = s->plane[plane].l_h[0];
  728. for (i = 0; i < lowpass_width; i++) {
  729. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  730. low++;
  731. high++;
  732. output++;
  733. }
  734. low = s->plane[plane].subband[1];
  735. high = s->plane[plane].subband[3];
  736. output = s->plane[plane].l_h[1];
  737. for (i = 0; i < lowpass_width; i++) {
  738. // note the stride of "low" is highpass_stride
  739. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  740. low++;
  741. high++;
  742. output++;
  743. }
  744. low = s->plane[plane].l_h[0];
  745. high = s->plane[plane].l_h[1];
  746. output = s->plane[plane].subband[0];
  747. for (i = 0; i < lowpass_height * 2; i++) {
  748. horiz_filter(output, low, high, lowpass_width);
  749. low += lowpass_width;
  750. high += lowpass_width;
  751. output += lowpass_width * 2;
  752. }
  753. if (s->bpc == 12) {
  754. output = s->plane[plane].subband[0];
  755. for (i = 0; i < lowpass_height * 2; i++) {
  756. for (j = 0; j < lowpass_width * 2; j++)
  757. output[j] *= 4;
  758. output += lowpass_width * 2;
  759. }
  760. }
  761. /* level 2 */
  762. lowpass_height = s->plane[plane].band[1][1].height;
  763. lowpass_width = s->plane[plane].band[1][1].width;
  764. highpass_stride = s->plane[plane].band[1][1].stride;
  765. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  766. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  767. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  768. ret = AVERROR(EINVAL);
  769. goto end;
  770. }
  771. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  772. low = s->plane[plane].subband[0];
  773. high = s->plane[plane].subband[5];
  774. output = s->plane[plane].l_h[3];
  775. for (i = 0; i < lowpass_width; i++) {
  776. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  777. low++;
  778. high++;
  779. output++;
  780. }
  781. low = s->plane[plane].subband[4];
  782. high = s->plane[plane].subband[6];
  783. output = s->plane[plane].l_h[4];
  784. for (i = 0; i < lowpass_width; i++) {
  785. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  786. low++;
  787. high++;
  788. output++;
  789. }
  790. low = s->plane[plane].l_h[3];
  791. high = s->plane[plane].l_h[4];
  792. output = s->plane[plane].subband[0];
  793. for (i = 0; i < lowpass_height * 2; i++) {
  794. horiz_filter(output, low, high, lowpass_width);
  795. low += lowpass_width;
  796. high += lowpass_width;
  797. output += lowpass_width * 2;
  798. }
  799. output = s->plane[plane].subband[0];
  800. for (i = 0; i < lowpass_height * 2; i++) {
  801. for (j = 0; j < lowpass_width * 2; j++)
  802. output[j] *= 4;
  803. output += lowpass_width * 2;
  804. }
  805. /* level 3 */
  806. lowpass_height = s->plane[plane].band[2][1].height;
  807. lowpass_width = s->plane[plane].band[2][1].width;
  808. highpass_stride = s->plane[plane].band[2][1].stride;
  809. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  810. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  811. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  812. ret = AVERROR(EINVAL);
  813. goto end;
  814. }
  815. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  816. if (s->progressive) {
  817. low = s->plane[plane].subband[0];
  818. high = s->plane[plane].subband[8];
  819. output = s->plane[plane].l_h[6];
  820. for (i = 0; i < lowpass_width; i++) {
  821. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  822. low++;
  823. high++;
  824. output++;
  825. }
  826. low = s->plane[plane].subband[7];
  827. high = s->plane[plane].subband[9];
  828. output = s->plane[plane].l_h[7];
  829. for (i = 0; i < lowpass_width; i++) {
  830. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  831. low++;
  832. high++;
  833. output++;
  834. }
  835. dst = (int16_t *)pic->data[act_plane];
  836. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16) {
  837. if (plane & 1)
  838. dst++;
  839. if (plane > 1)
  840. dst += pic->linesize[act_plane] >> 1;
  841. }
  842. low = s->plane[plane].l_h[6];
  843. high = s->plane[plane].l_h[7];
  844. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16 &&
  845. (lowpass_height * 2 > avctx->coded_height / 2 ||
  846. lowpass_width * 2 > avctx->coded_width / 2 )
  847. ) {
  848. ret = AVERROR_INVALIDDATA;
  849. goto end;
  850. }
  851. for (i = 0; i < lowpass_height * 2; i++) {
  852. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
  853. horiz_filter_clip_bayer(dst, low, high, lowpass_width, s->bpc);
  854. else
  855. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  856. if (avctx->pix_fmt == AV_PIX_FMT_GBRAP12 && act_plane == 3)
  857. process_alpha(dst, lowpass_width * 2);
  858. low += lowpass_width;
  859. high += lowpass_width;
  860. dst += dst_linesize;
  861. }
  862. } else {
  863. av_log(avctx, AV_LOG_DEBUG, "interlaced frame ? %d", pic->interlaced_frame);
  864. pic->interlaced_frame = 1;
  865. low = s->plane[plane].subband[0];
  866. high = s->plane[plane].subband[7];
  867. output = s->plane[plane].l_h[6];
  868. for (i = 0; i < lowpass_height; i++) {
  869. horiz_filter(output, low, high, lowpass_width);
  870. low += lowpass_width;
  871. high += lowpass_width;
  872. output += lowpass_width * 2;
  873. }
  874. low = s->plane[plane].subband[8];
  875. high = s->plane[plane].subband[9];
  876. output = s->plane[plane].l_h[7];
  877. for (i = 0; i < lowpass_height; i++) {
  878. horiz_filter(output, low, high, lowpass_width);
  879. low += lowpass_width;
  880. high += lowpass_width;
  881. output += lowpass_width * 2;
  882. }
  883. dst = (int16_t *)pic->data[act_plane];
  884. low = s->plane[plane].l_h[6];
  885. high = s->plane[plane].l_h[7];
  886. for (i = 0; i < lowpass_height; i++) {
  887. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2, act_plane);
  888. low += lowpass_width * 2;
  889. high += lowpass_width * 2;
  890. dst += pic->linesize[act_plane];
  891. }
  892. }
  893. }
  894. if (avctx->pix_fmt == AV_PIX_FMT_BAYER_RGGB16)
  895. process_bayer(pic);
  896. end:
  897. if (ret < 0)
  898. return ret;
  899. *got_frame = 1;
  900. return avpkt->size;
  901. }
  902. static av_cold int cfhd_close(AVCodecContext *avctx)
  903. {
  904. CFHDContext *s = avctx->priv_data;
  905. free_buffers(s);
  906. ff_free_vlc(&s->vlc_9);
  907. ff_free_vlc(&s->vlc_18);
  908. return 0;
  909. }
  910. AVCodec ff_cfhd_decoder = {
  911. .name = "cfhd",
  912. .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
  913. .type = AVMEDIA_TYPE_VIDEO,
  914. .id = AV_CODEC_ID_CFHD,
  915. .priv_data_size = sizeof(CFHDContext),
  916. .init = cfhd_init,
  917. .close = cfhd_close,
  918. .decode = cfhd_decode,
  919. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  920. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  921. };