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.

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