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.

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