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.

798 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 = highpass_height * highpass_stride;
  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 < expected) {
  465. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficents\n");
  466. ret = AVERROR(EINVAL);
  467. goto end;
  468. }
  469. 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);
  470. init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
  471. {
  472. OPEN_READER(re, &s->gb);
  473. if (!s->codebook) {
  474. while (1) {
  475. UPDATE_CACHE(re, &s->gb);
  476. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  477. VLC_BITS, 3, 1);
  478. /* escape */
  479. if (level == 64)
  480. break;
  481. count += run;
  482. if (count > expected)
  483. break;
  484. coeff = dequant_and_decompand(level, s->quantisation);
  485. for (i = 0; i < run; i++)
  486. *coeff_data++ = coeff;
  487. }
  488. } else {
  489. while (1) {
  490. UPDATE_CACHE(re, &s->gb);
  491. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  492. VLC_BITS, 3, 1);
  493. /* escape */
  494. if (level == 255 && run == 2)
  495. break;
  496. count += run;
  497. if (count > expected)
  498. break;
  499. coeff = dequant_and_decompand(level, s->quantisation);
  500. for (i = 0; i < run; i++)
  501. *coeff_data++ = coeff;
  502. }
  503. }
  504. CLOSE_READER(re, &s->gb);
  505. }
  506. if (count > expected) {
  507. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  508. ret = AVERROR(EINVAL);
  509. goto end;
  510. }
  511. bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  512. if (bytes > bytestream2_get_bytes_left(&gb)) {
  513. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  514. ret = AVERROR(EINVAL);
  515. goto end;
  516. } else
  517. bytestream2_seek(&gb, bytes, SEEK_CUR);
  518. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  519. s->codebook = 0;
  520. /* Copy last line of coefficients if odd height */
  521. if (highpass_height & 1) {
  522. memcpy(&coeff_data[highpass_height * highpass_stride],
  523. &coeff_data[(highpass_height - 1) * highpass_stride],
  524. highpass_stride * sizeof(*coeff_data));
  525. }
  526. }
  527. }
  528. if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  529. s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
  530. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  531. ret = AVERROR(EINVAL);
  532. goto end;
  533. }
  534. if (!got_buffer) {
  535. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  536. ret = AVERROR(EINVAL);
  537. goto end;
  538. }
  539. planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  540. for (plane = 0; plane < planes && !ret; plane++) {
  541. /* level 1 */
  542. int lowpass_height = s->plane[plane].band[0][0].height;
  543. int lowpass_width = s->plane[plane].band[0][0].width;
  544. int highpass_stride = s->plane[plane].band[0][1].stride;
  545. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  546. int16_t *low, *high, *output, *dst;
  547. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  548. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  549. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  550. ret = AVERROR(EINVAL);
  551. goto end;
  552. }
  553. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  554. low = s->plane[plane].subband[0];
  555. high = s->plane[plane].subband[2];
  556. output = s->plane[plane].l_h[0];
  557. for (i = 0; i < lowpass_width; i++) {
  558. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  559. low++;
  560. high++;
  561. output++;
  562. }
  563. low = s->plane[plane].subband[1];
  564. high = s->plane[plane].subband[3];
  565. output = s->plane[plane].l_h[1];
  566. for (i = 0; i < lowpass_width; i++) {
  567. // note the stride of "low" is highpass_stride
  568. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  569. low++;
  570. high++;
  571. output++;
  572. }
  573. low = s->plane[plane].l_h[0];
  574. high = s->plane[plane].l_h[1];
  575. output = s->plane[plane].subband[0];
  576. for (i = 0; i < lowpass_height * 2; i++) {
  577. horiz_filter(output, low, high, lowpass_width);
  578. low += lowpass_width;
  579. high += lowpass_width;
  580. output += lowpass_width * 2;
  581. }
  582. if (s->bpc == 12) {
  583. output = s->plane[plane].subband[0];
  584. for (i = 0; i < lowpass_height * 2; i++) {
  585. for (j = 0; j < lowpass_width * 2; j++)
  586. output[j] <<= 2;
  587. output += lowpass_width * 2;
  588. }
  589. }
  590. /* level 2 */
  591. lowpass_height = s->plane[plane].band[1][1].height;
  592. lowpass_width = s->plane[plane].band[1][1].width;
  593. highpass_stride = s->plane[plane].band[1][1].stride;
  594. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  595. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  596. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  597. ret = AVERROR(EINVAL);
  598. goto end;
  599. }
  600. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  601. low = s->plane[plane].subband[0];
  602. high = s->plane[plane].subband[5];
  603. output = s->plane[plane].l_h[3];
  604. for (i = 0; i < lowpass_width; i++) {
  605. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  606. low++;
  607. high++;
  608. output++;
  609. }
  610. low = s->plane[plane].subband[4];
  611. high = s->plane[plane].subband[6];
  612. output = s->plane[plane].l_h[4];
  613. for (i = 0; i < lowpass_width; i++) {
  614. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  615. low++;
  616. high++;
  617. output++;
  618. }
  619. low = s->plane[plane].l_h[3];
  620. high = s->plane[plane].l_h[4];
  621. output = s->plane[plane].subband[0];
  622. for (i = 0; i < lowpass_height * 2; i++) {
  623. horiz_filter(output, low, high, lowpass_width);
  624. low += lowpass_width;
  625. high += lowpass_width;
  626. output += lowpass_width * 2;
  627. }
  628. output = s->plane[plane].subband[0];
  629. for (i = 0; i < lowpass_height * 2; i++) {
  630. for (j = 0; j < lowpass_width * 2; j++)
  631. output[j] <<= 2;
  632. output += lowpass_width * 2;
  633. }
  634. /* level 3 */
  635. lowpass_height = s->plane[plane].band[2][1].height;
  636. lowpass_width = s->plane[plane].band[2][1].width;
  637. highpass_stride = s->plane[plane].band[2][1].stride;
  638. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  639. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  640. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  641. ret = AVERROR(EINVAL);
  642. goto end;
  643. }
  644. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  645. low = s->plane[plane].subband[0];
  646. high = s->plane[plane].subband[8];
  647. output = s->plane[plane].l_h[6];
  648. for (i = 0; i < lowpass_width; i++) {
  649. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  650. low++;
  651. high++;
  652. output++;
  653. }
  654. low = s->plane[plane].subband[7];
  655. high = s->plane[plane].subband[9];
  656. output = s->plane[plane].l_h[7];
  657. for (i = 0; i < lowpass_width; i++) {
  658. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  659. low++;
  660. high++;
  661. output++;
  662. }
  663. dst = (int16_t *)pic->data[act_plane];
  664. low = s->plane[plane].l_h[6];
  665. high = s->plane[plane].l_h[7];
  666. for (i = 0; i < lowpass_height * 2; i++) {
  667. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  668. low += lowpass_width;
  669. high += lowpass_width;
  670. dst += pic->linesize[act_plane] / 2;
  671. }
  672. }
  673. end:
  674. if (ret < 0)
  675. return ret;
  676. *got_frame = 1;
  677. return avpkt->size;
  678. }
  679. static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
  680. {
  681. CFHDContext *s = avctx->priv_data;
  682. free_buffers(avctx);
  683. if (!avctx->internal->is_copy) {
  684. ff_free_vlc(&s->vlc_9);
  685. ff_free_vlc(&s->vlc_18);
  686. }
  687. return 0;
  688. }
  689. AVCodec ff_cfhd_decoder = {
  690. .name = "cfhd",
  691. .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
  692. .type = AVMEDIA_TYPE_VIDEO,
  693. .id = AV_CODEC_ID_CFHD,
  694. .priv_data_size = sizeof(CFHDContext),
  695. .init = cfhd_decode_init,
  696. .close = cfhd_close_decoder,
  697. .decode = cfhd_decode,
  698. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  699. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  700. };