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.

803 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. if (data < 1 || data > 31) {
  225. av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
  226. ret = AVERROR(EINVAL);
  227. break;
  228. }
  229. s->bpc = data;
  230. } else if (tag == 12) {
  231. av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
  232. s->channel_cnt = data;
  233. if (data > 4) {
  234. av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
  235. ret = AVERROR_PATCHWELCOME;
  236. break;
  237. }
  238. } else if (tag == 14) {
  239. av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
  240. if (data != SUBBAND_COUNT) {
  241. av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
  242. ret = AVERROR_PATCHWELCOME;
  243. break;
  244. }
  245. } else if (tag == 62) {
  246. s->channel_num = data;
  247. av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
  248. if (s->channel_num >= planes) {
  249. av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
  250. ret = AVERROR(EINVAL);
  251. break;
  252. }
  253. init_plane_defaults(s);
  254. } else if (tag == 48) {
  255. if (s->subband_num != 0 && data == 1) // hack
  256. s->level++;
  257. av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
  258. s->subband_num = data;
  259. if (s->level >= DWT_LEVELS) {
  260. av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
  261. ret = AVERROR(EINVAL);
  262. break;
  263. }
  264. if (s->subband_num > 3) {
  265. av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
  266. ret = AVERROR(EINVAL);
  267. break;
  268. }
  269. } else if (tag == 51) {
  270. av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
  271. s->subband_num_actual = data;
  272. if (s->subband_num_actual >= 10) {
  273. av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
  274. ret = AVERROR(EINVAL);
  275. break;
  276. }
  277. } else if (tag == 35)
  278. av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
  279. else if (tag == 53) {
  280. s->quantisation = data;
  281. av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
  282. } else if (tag == 109) {
  283. s->prescale_shift[0] = (data >> 0) & 0x7;
  284. s->prescale_shift[1] = (data >> 3) & 0x7;
  285. s->prescale_shift[2] = (data >> 6) & 0x7;
  286. av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
  287. } else if (tag == 27) {
  288. av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
  289. if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
  290. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
  291. ret = AVERROR(EINVAL);
  292. break;
  293. }
  294. s->plane[s->channel_num].band[0][0].width = data;
  295. s->plane[s->channel_num].band[0][0].stride = data;
  296. } else if (tag == 28) {
  297. av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
  298. if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
  299. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
  300. ret = AVERROR(EINVAL);
  301. break;
  302. }
  303. s->plane[s->channel_num].band[0][0].height = data;
  304. } else if (tag == 1)
  305. av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
  306. else if (tag == 10) {
  307. if (data != 0) {
  308. avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
  309. ret = AVERROR_PATCHWELCOME;
  310. break;
  311. }
  312. av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
  313. } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
  314. av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
  315. bytestream2_skipu(&gb, data * 4);
  316. } else if (tag == 23) {
  317. av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
  318. avpriv_report_missing_feature(avctx, "Skip frame");
  319. ret = AVERROR_PATCHWELCOME;
  320. break;
  321. } else if (tag == 2) {
  322. av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
  323. if (data > bytestream2_get_bytes_left(&gb) / 4) {
  324. av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
  325. ret = AVERROR_INVALIDDATA;
  326. break;
  327. }
  328. for (i = 0; i < data; i++) {
  329. uint16_t tag2 = bytestream2_get_be16(&gb);
  330. uint16_t val2 = bytestream2_get_be16(&gb);
  331. av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
  332. }
  333. } else if (tag == 41) {
  334. 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);
  335. if (data < 3) {
  336. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
  337. ret = AVERROR(EINVAL);
  338. break;
  339. }
  340. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  341. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  342. } else if (tag == 42) {
  343. av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
  344. if (data < 3) {
  345. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
  346. ret = AVERROR(EINVAL);
  347. break;
  348. }
  349. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  350. } else if (tag == 49) {
  351. av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
  352. if (data < 3) {
  353. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
  354. ret = AVERROR(EINVAL);
  355. break;
  356. }
  357. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  358. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  359. } else if (tag == 50) {
  360. av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
  361. if (data < 3) {
  362. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
  363. ret = AVERROR(EINVAL);
  364. break;
  365. }
  366. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  367. } else if (tag == 71) {
  368. s->codebook = data;
  369. av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
  370. } else if (tag == 72) {
  371. s->codebook = data;
  372. av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
  373. } else if (tag == 70) {
  374. av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
  375. if (!(data == 10 || data == 12)) {
  376. av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
  377. ret = AVERROR(EINVAL);
  378. break;
  379. }
  380. s->bpc = data;
  381. } else if (tag == 84) {
  382. av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
  383. if (data == 1)
  384. s->coded_format = AV_PIX_FMT_YUV422P10;
  385. else if (data == 3)
  386. s->coded_format = AV_PIX_FMT_GBRP12;
  387. else if (data == 4)
  388. s->coded_format = AV_PIX_FMT_GBRAP12;
  389. else {
  390. avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
  391. ret = AVERROR_PATCHWELCOME;
  392. break;
  393. }
  394. planes = av_pix_fmt_count_planes(s->coded_format);
  395. } else
  396. av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
  397. /* Some kind of end of header tag */
  398. if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
  399. s->coded_format != AV_PIX_FMT_NONE) {
  400. if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
  401. s->a_format != s->coded_format) {
  402. free_buffers(avctx);
  403. if ((ret = alloc_buffers(avctx)) < 0) {
  404. free_buffers(avctx);
  405. return ret;
  406. }
  407. }
  408. ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  409. if (ret < 0)
  410. return ret;
  411. frame.f->width =
  412. frame.f->height = 0;
  413. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  414. return ret;
  415. s->coded_width = 0;
  416. s->coded_height = 0;
  417. s->coded_format = AV_PIX_FMT_NONE;
  418. got_buffer = 1;
  419. }
  420. coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  421. /* Lowpass coefficients */
  422. if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
  423. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  424. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  425. int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  426. int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
  427. if (!got_buffer) {
  428. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  429. ret = AVERROR(EINVAL);
  430. goto end;
  431. }
  432. if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  433. lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  434. av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  435. ret = AVERROR(EINVAL);
  436. goto end;
  437. }
  438. av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  439. for (i = 0; i < lowpass_height; i++) {
  440. for (j = 0; j < lowpass_width; j++)
  441. coeff_data[j] = bytestream2_get_be16u(&gb);
  442. coeff_data += lowpass_width;
  443. }
  444. /* Align to mod-4 position to continue reading tags */
  445. bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
  446. /* Copy last line of coefficients if odd height */
  447. if (lowpass_height & 1) {
  448. memcpy(&coeff_data[lowpass_height * lowpass_width],
  449. &coeff_data[(lowpass_height - 1) * lowpass_width],
  450. lowpass_width * sizeof(*coeff_data));
  451. }
  452. av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
  453. }
  454. if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
  455. int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  456. int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  457. int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  458. int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  459. int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  460. int expected = highpass_height * highpass_stride;
  461. int a_expected = highpass_a_height * highpass_a_width;
  462. int level, run, coeff;
  463. int count = 0, bytes;
  464. if (!got_buffer) {
  465. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  466. ret = AVERROR(EINVAL);
  467. goto end;
  468. }
  469. if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < expected) {
  470. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficents\n");
  471. ret = AVERROR(EINVAL);
  472. goto end;
  473. }
  474. 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);
  475. init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
  476. {
  477. OPEN_READER(re, &s->gb);
  478. if (!s->codebook) {
  479. while (1) {
  480. UPDATE_CACHE(re, &s->gb);
  481. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  482. VLC_BITS, 3, 1);
  483. /* escape */
  484. if (level == 64)
  485. break;
  486. count += run;
  487. if (count > expected)
  488. break;
  489. coeff = dequant_and_decompand(level, s->quantisation);
  490. for (i = 0; i < run; i++)
  491. *coeff_data++ = coeff;
  492. }
  493. } else {
  494. while (1) {
  495. UPDATE_CACHE(re, &s->gb);
  496. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  497. VLC_BITS, 3, 1);
  498. /* escape */
  499. if (level == 255 && run == 2)
  500. break;
  501. count += run;
  502. if (count > expected)
  503. break;
  504. coeff = dequant_and_decompand(level, s->quantisation);
  505. for (i = 0; i < run; i++)
  506. *coeff_data++ = coeff;
  507. }
  508. }
  509. CLOSE_READER(re, &s->gb);
  510. }
  511. if (count > expected) {
  512. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  513. ret = AVERROR(EINVAL);
  514. goto end;
  515. }
  516. bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  517. if (bytes > bytestream2_get_bytes_left(&gb)) {
  518. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  519. ret = AVERROR(EINVAL);
  520. goto end;
  521. } else
  522. bytestream2_seek(&gb, bytes, SEEK_CUR);
  523. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  524. s->codebook = 0;
  525. /* Copy last line of coefficients if odd height */
  526. if (highpass_height & 1) {
  527. memcpy(&coeff_data[highpass_height * highpass_stride],
  528. &coeff_data[(highpass_height - 1) * highpass_stride],
  529. highpass_stride * sizeof(*coeff_data));
  530. }
  531. }
  532. }
  533. if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  534. s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
  535. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  536. ret = AVERROR(EINVAL);
  537. goto end;
  538. }
  539. if (!got_buffer) {
  540. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  541. ret = AVERROR(EINVAL);
  542. goto end;
  543. }
  544. planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  545. for (plane = 0; plane < planes && !ret; plane++) {
  546. /* level 1 */
  547. int lowpass_height = s->plane[plane].band[0][0].height;
  548. int lowpass_width = s->plane[plane].band[0][0].width;
  549. int highpass_stride = s->plane[plane].band[0][1].stride;
  550. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  551. int16_t *low, *high, *output, *dst;
  552. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  553. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  554. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  555. ret = AVERROR(EINVAL);
  556. goto end;
  557. }
  558. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  559. low = s->plane[plane].subband[0];
  560. high = s->plane[plane].subband[2];
  561. output = s->plane[plane].l_h[0];
  562. for (i = 0; i < lowpass_width; i++) {
  563. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  564. low++;
  565. high++;
  566. output++;
  567. }
  568. low = s->plane[plane].subband[1];
  569. high = s->plane[plane].subband[3];
  570. output = s->plane[plane].l_h[1];
  571. for (i = 0; i < lowpass_width; i++) {
  572. // note the stride of "low" is highpass_stride
  573. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  574. low++;
  575. high++;
  576. output++;
  577. }
  578. low = s->plane[plane].l_h[0];
  579. high = s->plane[plane].l_h[1];
  580. output = s->plane[plane].subband[0];
  581. for (i = 0; i < lowpass_height * 2; i++) {
  582. horiz_filter(output, low, high, lowpass_width);
  583. low += lowpass_width;
  584. high += lowpass_width;
  585. output += lowpass_width * 2;
  586. }
  587. if (s->bpc == 12) {
  588. output = s->plane[plane].subband[0];
  589. for (i = 0; i < lowpass_height * 2; i++) {
  590. for (j = 0; j < lowpass_width * 2; j++)
  591. output[j] *= 4;
  592. output += lowpass_width * 2;
  593. }
  594. }
  595. /* level 2 */
  596. lowpass_height = s->plane[plane].band[1][1].height;
  597. lowpass_width = s->plane[plane].band[1][1].width;
  598. highpass_stride = s->plane[plane].band[1][1].stride;
  599. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  600. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  601. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  602. ret = AVERROR(EINVAL);
  603. goto end;
  604. }
  605. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  606. low = s->plane[plane].subband[0];
  607. high = s->plane[plane].subband[5];
  608. output = s->plane[plane].l_h[3];
  609. for (i = 0; i < lowpass_width; i++) {
  610. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  611. low++;
  612. high++;
  613. output++;
  614. }
  615. low = s->plane[plane].subband[4];
  616. high = s->plane[plane].subband[6];
  617. output = s->plane[plane].l_h[4];
  618. for (i = 0; i < lowpass_width; i++) {
  619. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  620. low++;
  621. high++;
  622. output++;
  623. }
  624. low = s->plane[plane].l_h[3];
  625. high = s->plane[plane].l_h[4];
  626. output = s->plane[plane].subband[0];
  627. for (i = 0; i < lowpass_height * 2; i++) {
  628. horiz_filter(output, low, high, lowpass_width);
  629. low += lowpass_width;
  630. high += lowpass_width;
  631. output += lowpass_width * 2;
  632. }
  633. output = s->plane[plane].subband[0];
  634. for (i = 0; i < lowpass_height * 2; i++) {
  635. for (j = 0; j < lowpass_width * 2; j++)
  636. output[j] *= 4;
  637. output += lowpass_width * 2;
  638. }
  639. /* level 3 */
  640. lowpass_height = s->plane[plane].band[2][1].height;
  641. lowpass_width = s->plane[plane].band[2][1].width;
  642. highpass_stride = s->plane[plane].band[2][1].stride;
  643. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  644. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  645. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  646. ret = AVERROR(EINVAL);
  647. goto end;
  648. }
  649. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  650. low = s->plane[plane].subband[0];
  651. high = s->plane[plane].subband[8];
  652. output = s->plane[plane].l_h[6];
  653. for (i = 0; i < lowpass_width; i++) {
  654. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  655. low++;
  656. high++;
  657. output++;
  658. }
  659. low = s->plane[plane].subband[7];
  660. high = s->plane[plane].subband[9];
  661. output = s->plane[plane].l_h[7];
  662. for (i = 0; i < lowpass_width; i++) {
  663. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  664. low++;
  665. high++;
  666. output++;
  667. }
  668. dst = (int16_t *)pic->data[act_plane];
  669. low = s->plane[plane].l_h[6];
  670. high = s->plane[plane].l_h[7];
  671. for (i = 0; i < lowpass_height * 2; i++) {
  672. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  673. low += lowpass_width;
  674. high += lowpass_width;
  675. dst += pic->linesize[act_plane] / 2;
  676. }
  677. }
  678. end:
  679. if (ret < 0)
  680. return ret;
  681. *got_frame = 1;
  682. return avpkt->size;
  683. }
  684. static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
  685. {
  686. CFHDContext *s = avctx->priv_data;
  687. free_buffers(avctx);
  688. if (!avctx->internal->is_copy) {
  689. ff_free_vlc(&s->vlc_9);
  690. ff_free_vlc(&s->vlc_18);
  691. }
  692. return 0;
  693. }
  694. AVCodec ff_cfhd_decoder = {
  695. .name = "cfhd",
  696. .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
  697. .type = AVMEDIA_TYPE_VIDEO,
  698. .id = AV_CODEC_ID_CFHD,
  699. .priv_data_size = sizeof(CFHDContext),
  700. .init = cfhd_decode_init,
  701. .close = cfhd_close_decoder,
  702. .decode = cfhd_decode,
  703. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  704. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  705. };