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.

856 lines
33KB

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