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.

793 lines
31KB

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