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.

1068 lines
40KB

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