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.

772 lines
30KB

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