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.

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