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.

799 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. av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
  284. if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
  285. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
  286. ret = AVERROR(EINVAL);
  287. break;
  288. }
  289. s->plane[s->channel_num].band[0][0].width = data;
  290. s->plane[s->channel_num].band[0][0].stride = data;
  291. } else if (tag == 28) {
  292. av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
  293. if (data < 3 || data > s->plane[s->channel_num].band[0][0].height) {
  294. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
  295. ret = AVERROR(EINVAL);
  296. break;
  297. }
  298. s->plane[s->channel_num].band[0][0].height = data;
  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. 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);
  330. if (data < 3) {
  331. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
  332. ret = AVERROR(EINVAL);
  333. break;
  334. }
  335. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  336. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  337. } else if (tag == 42) {
  338. av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
  339. if (data < 3) {
  340. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
  341. ret = AVERROR(EINVAL);
  342. break;
  343. }
  344. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  345. } else if (tag == 49) {
  346. av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
  347. if (data < 3) {
  348. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
  349. ret = AVERROR(EINVAL);
  350. break;
  351. }
  352. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  353. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  354. } else if (tag == 50) {
  355. av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
  356. if (data < 3) {
  357. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
  358. ret = AVERROR(EINVAL);
  359. break;
  360. }
  361. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  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. ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  404. if (ret < 0)
  405. return ret;
  406. frame.f->width =
  407. frame.f->height = 0;
  408. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  409. return ret;
  410. s->coded_width = 0;
  411. s->coded_height = 0;
  412. s->coded_format = AV_PIX_FMT_NONE;
  413. got_buffer = 1;
  414. }
  415. coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  416. /* Lowpass coefficients */
  417. if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
  418. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  419. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  420. int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  421. int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
  422. if (!got_buffer) {
  423. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  424. ret = AVERROR(EINVAL);
  425. goto end;
  426. }
  427. if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  428. lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  429. av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  430. ret = AVERROR(EINVAL);
  431. goto end;
  432. }
  433. av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  434. for (i = 0; i < lowpass_height; i++) {
  435. for (j = 0; j < lowpass_width; j++)
  436. coeff_data[j] = bytestream2_get_be16u(&gb);
  437. coeff_data += lowpass_width;
  438. }
  439. /* Align to mod-4 position to continue reading tags */
  440. bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
  441. /* Copy last line of coefficients if odd height */
  442. if (lowpass_height & 1) {
  443. memcpy(&coeff_data[lowpass_height * lowpass_width],
  444. &coeff_data[(lowpass_height - 1) * lowpass_width],
  445. lowpass_width * sizeof(*coeff_data));
  446. }
  447. av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
  448. }
  449. if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
  450. int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  451. int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  452. int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  453. int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  454. int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  455. int expected;
  456. int a_expected = highpass_a_height * highpass_a_width;
  457. int level, run, coeff;
  458. int count = 0, bytes;
  459. if (!got_buffer) {
  460. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  461. ret = AVERROR(EINVAL);
  462. goto end;
  463. }
  464. if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
  465. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
  466. ret = AVERROR(EINVAL);
  467. goto end;
  468. }
  469. expected = highpass_height * highpass_stride;
  470. 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);
  471. init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
  472. {
  473. OPEN_READER(re, &s->gb);
  474. if (!s->codebook) {
  475. while (1) {
  476. UPDATE_CACHE(re, &s->gb);
  477. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  478. VLC_BITS, 3, 1);
  479. /* escape */
  480. if (level == 64)
  481. break;
  482. count += run;
  483. if (count > expected)
  484. break;
  485. coeff = dequant_and_decompand(level, s->quantisation);
  486. for (i = 0; i < run; i++)
  487. *coeff_data++ = coeff;
  488. }
  489. } else {
  490. while (1) {
  491. UPDATE_CACHE(re, &s->gb);
  492. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  493. VLC_BITS, 3, 1);
  494. /* escape */
  495. if (level == 255 && run == 2)
  496. break;
  497. count += run;
  498. if (count > expected)
  499. break;
  500. coeff = dequant_and_decompand(level, s->quantisation);
  501. for (i = 0; i < run; i++)
  502. *coeff_data++ = coeff;
  503. }
  504. }
  505. CLOSE_READER(re, &s->gb);
  506. }
  507. if (count > expected) {
  508. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  509. ret = AVERROR(EINVAL);
  510. goto end;
  511. }
  512. bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  513. if (bytes > bytestream2_get_bytes_left(&gb)) {
  514. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  515. ret = AVERROR(EINVAL);
  516. goto end;
  517. } else
  518. bytestream2_seek(&gb, bytes, SEEK_CUR);
  519. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  520. s->codebook = 0;
  521. /* Copy last line of coefficients if odd height */
  522. if (highpass_height & 1) {
  523. memcpy(&coeff_data[highpass_height * highpass_stride],
  524. &coeff_data[(highpass_height - 1) * highpass_stride],
  525. highpass_stride * sizeof(*coeff_data));
  526. }
  527. }
  528. }
  529. if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  530. s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
  531. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  532. ret = AVERROR(EINVAL);
  533. goto end;
  534. }
  535. if (!got_buffer) {
  536. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  537. ret = AVERROR(EINVAL);
  538. goto end;
  539. }
  540. planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  541. for (plane = 0; plane < planes && !ret; plane++) {
  542. /* level 1 */
  543. int lowpass_height = s->plane[plane].band[0][0].height;
  544. int lowpass_width = s->plane[plane].band[0][0].width;
  545. int highpass_stride = s->plane[plane].band[0][1].stride;
  546. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  547. int16_t *low, *high, *output, *dst;
  548. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  549. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  550. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  551. ret = AVERROR(EINVAL);
  552. goto end;
  553. }
  554. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  555. low = s->plane[plane].subband[0];
  556. high = s->plane[plane].subband[2];
  557. output = s->plane[plane].l_h[0];
  558. for (i = 0; i < lowpass_width; i++) {
  559. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  560. low++;
  561. high++;
  562. output++;
  563. }
  564. low = s->plane[plane].subband[1];
  565. high = s->plane[plane].subband[3];
  566. output = s->plane[plane].l_h[1];
  567. for (i = 0; i < lowpass_width; i++) {
  568. // note the stride of "low" is highpass_stride
  569. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  570. low++;
  571. high++;
  572. output++;
  573. }
  574. low = s->plane[plane].l_h[0];
  575. high = s->plane[plane].l_h[1];
  576. output = s->plane[plane].subband[0];
  577. for (i = 0; i < lowpass_height * 2; i++) {
  578. horiz_filter(output, low, high, lowpass_width);
  579. low += lowpass_width;
  580. high += lowpass_width;
  581. output += lowpass_width * 2;
  582. }
  583. if (s->bpc == 12) {
  584. output = s->plane[plane].subband[0];
  585. for (i = 0; i < lowpass_height * 2; i++) {
  586. for (j = 0; j < lowpass_width * 2; j++)
  587. output[j] <<= 2;
  588. output += lowpass_width * 2;
  589. }
  590. }
  591. /* level 2 */
  592. lowpass_height = s->plane[plane].band[1][1].height;
  593. lowpass_width = s->plane[plane].band[1][1].width;
  594. highpass_stride = s->plane[plane].band[1][1].stride;
  595. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  596. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  597. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  598. ret = AVERROR(EINVAL);
  599. goto end;
  600. }
  601. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  602. low = s->plane[plane].subband[0];
  603. high = s->plane[plane].subband[5];
  604. output = s->plane[plane].l_h[3];
  605. for (i = 0; i < lowpass_width; i++) {
  606. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  607. low++;
  608. high++;
  609. output++;
  610. }
  611. low = s->plane[plane].subband[4];
  612. high = s->plane[plane].subband[6];
  613. output = s->plane[plane].l_h[4];
  614. for (i = 0; i < lowpass_width; i++) {
  615. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  616. low++;
  617. high++;
  618. output++;
  619. }
  620. low = s->plane[plane].l_h[3];
  621. high = s->plane[plane].l_h[4];
  622. output = s->plane[plane].subband[0];
  623. for (i = 0; i < lowpass_height * 2; i++) {
  624. horiz_filter(output, low, high, lowpass_width);
  625. low += lowpass_width;
  626. high += lowpass_width;
  627. output += lowpass_width * 2;
  628. }
  629. output = s->plane[plane].subband[0];
  630. for (i = 0; i < lowpass_height * 2; i++) {
  631. for (j = 0; j < lowpass_width * 2; j++)
  632. output[j] <<= 2;
  633. output += lowpass_width * 2;
  634. }
  635. /* level 3 */
  636. lowpass_height = s->plane[plane].band[2][1].height;
  637. lowpass_width = s->plane[plane].band[2][1].width;
  638. highpass_stride = s->plane[plane].band[2][1].stride;
  639. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  640. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  641. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  642. ret = AVERROR(EINVAL);
  643. goto end;
  644. }
  645. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  646. low = s->plane[plane].subband[0];
  647. high = s->plane[plane].subband[8];
  648. output = s->plane[plane].l_h[6];
  649. for (i = 0; i < lowpass_width; i++) {
  650. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  651. low++;
  652. high++;
  653. output++;
  654. }
  655. low = s->plane[plane].subband[7];
  656. high = s->plane[plane].subband[9];
  657. output = s->plane[plane].l_h[7];
  658. for (i = 0; i < lowpass_width; i++) {
  659. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  660. low++;
  661. high++;
  662. output++;
  663. }
  664. dst = (int16_t *)pic->data[act_plane];
  665. low = s->plane[plane].l_h[6];
  666. high = s->plane[plane].l_h[7];
  667. for (i = 0; i < lowpass_height * 2; i++) {
  668. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  669. low += lowpass_width;
  670. high += lowpass_width;
  671. dst += pic->linesize[act_plane] / 2;
  672. }
  673. }
  674. end:
  675. if (ret < 0)
  676. return ret;
  677. *got_frame = 1;
  678. return avpkt->size;
  679. }
  680. static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
  681. {
  682. CFHDContext *s = avctx->priv_data;
  683. free_buffers(avctx);
  684. if (!avctx->internal->is_copy) {
  685. ff_free_vlc(&s->vlc_9);
  686. ff_free_vlc(&s->vlc_18);
  687. }
  688. return 0;
  689. }
  690. AVCodec ff_cfhd_decoder = {
  691. .name = "cfhd",
  692. .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
  693. .type = AVMEDIA_TYPE_VIDEO,
  694. .id = AV_CODEC_ID_CFHD,
  695. .priv_data_size = sizeof(CFHDContext),
  696. .init = cfhd_decode_init,
  697. .close = cfhd_close_decoder,
  698. .decode = cfhd_decode,
  699. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  700. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  701. };