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.

762 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. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  393. return ret;
  394. s->coded_width = 0;
  395. s->coded_height = 0;
  396. got_buffer = 1;
  397. }
  398. coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  399. /* Lowpass coefficients */
  400. if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
  401. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  402. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  403. int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  404. int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
  405. if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  406. lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  407. av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  408. ret = AVERROR(EINVAL);
  409. goto end;
  410. }
  411. av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %"PRIu16" height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  412. for (i = 0; i < lowpass_height; i++) {
  413. for (j = 0; j < lowpass_width; j++)
  414. coeff_data[j] = bytestream2_get_be16u(&gb);
  415. coeff_data += lowpass_width;
  416. }
  417. /* Copy last line of coefficients if odd height */
  418. if (lowpass_height & 1) {
  419. memcpy(&coeff_data[lowpass_height * lowpass_width],
  420. &coeff_data[(lowpass_height - 1) * lowpass_width],
  421. lowpass_width * sizeof(*coeff_data));
  422. }
  423. av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %"PRIu16"\n", lowpass_width * lowpass_height);
  424. }
  425. if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
  426. int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  427. int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  428. int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  429. int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  430. int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  431. int expected = highpass_height * highpass_stride;
  432. int a_expected = highpass_a_height * highpass_a_width;
  433. int level, run, coeff;
  434. int count = 0, bytes;
  435. if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < expected) {
  436. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficents\n");
  437. ret = AVERROR(EINVAL);
  438. goto end;
  439. }
  440. 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);
  441. init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
  442. {
  443. OPEN_READER(re, &s->gb);
  444. if (!s->codebook) {
  445. while (1) {
  446. UPDATE_CACHE(re, &s->gb);
  447. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  448. VLC_BITS, 3, 1);
  449. /* escape */
  450. if (level == 64)
  451. break;
  452. count += run;
  453. if (count > expected)
  454. break;
  455. coeff = dequant_and_decompand(level, s->quantisation);
  456. for (i = 0; i < run; i++)
  457. *coeff_data++ = coeff;
  458. }
  459. } else {
  460. while (1) {
  461. UPDATE_CACHE(re, &s->gb);
  462. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  463. VLC_BITS, 3, 1);
  464. /* escape */
  465. if (level == 255 && run == 2)
  466. break;
  467. count += run;
  468. if (count > expected)
  469. break;
  470. coeff = dequant_and_decompand(level, s->quantisation);
  471. for (i = 0; i < run; i++)
  472. *coeff_data++ = coeff;
  473. }
  474. }
  475. CLOSE_READER(re, &s->gb);
  476. }
  477. if (count > expected) {
  478. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  479. ret = AVERROR(EINVAL);
  480. goto end;
  481. }
  482. bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  483. if (bytes > bytestream2_get_bytes_left(&gb)) {
  484. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  485. ret = AVERROR(EINVAL);
  486. goto end;
  487. } else
  488. bytestream2_seek(&gb, bytes, SEEK_CUR);
  489. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  490. s->codebook = 0;
  491. /* Copy last line of coefficients if odd height */
  492. if (highpass_height & 1) {
  493. memcpy(&coeff_data[highpass_height * highpass_stride],
  494. &coeff_data[(highpass_height - 1) * highpass_stride],
  495. highpass_stride * sizeof(*coeff_data));
  496. }
  497. }
  498. }
  499. if (!s->a_width || !s->a_height || s->coded_width || s->coded_height) {
  500. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  501. ret = AVERROR(EINVAL);
  502. goto end;
  503. }
  504. if (!got_buffer) {
  505. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  506. ret = AVERROR(EINVAL);
  507. goto end;
  508. }
  509. for (plane = 0; plane < 3 && !ret; plane++) {
  510. /* level 1 */
  511. int lowpass_height = s->plane[plane].band[0][0].height;
  512. int lowpass_width = s->plane[plane].band[0][0].width;
  513. int highpass_stride = s->plane[plane].band[0][1].stride;
  514. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : 0;
  515. int16_t *low, *high, *output, *dst;
  516. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  517. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  518. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  519. ret = AVERROR(EINVAL);
  520. goto end;
  521. }
  522. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  523. low = s->plane[plane].subband[0];
  524. high = s->plane[plane].subband[2];
  525. output = s->plane[plane].l_h[0];
  526. for (i = 0; i < lowpass_width; i++) {
  527. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  528. low++;
  529. high++;
  530. output++;
  531. }
  532. low = s->plane[plane].subband[1];
  533. high = s->plane[plane].subband[3];
  534. output = s->plane[plane].l_h[1];
  535. for (i = 0; i < lowpass_width; i++) {
  536. // note the stride of "low" is highpass_stride
  537. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  538. low++;
  539. high++;
  540. output++;
  541. }
  542. low = s->plane[plane].l_h[0];
  543. high = s->plane[plane].l_h[1];
  544. output = s->plane[plane].subband[0];
  545. for (i = 0; i < lowpass_height * 2; i++) {
  546. horiz_filter(output, low, high, lowpass_width);
  547. low += lowpass_width;
  548. high += lowpass_width;
  549. output += lowpass_width * 2;
  550. }
  551. if (avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
  552. output = s->plane[plane].subband[0];
  553. for (i = 0; i < lowpass_height * 2; i++) {
  554. for (j = 0; j < lowpass_width * 2; j++)
  555. output[j] <<= 2;
  556. output += lowpass_width * 2;
  557. }
  558. }
  559. /* level 2 */
  560. lowpass_height = s->plane[plane].band[1][1].height;
  561. lowpass_width = s->plane[plane].band[1][1].width;
  562. highpass_stride = s->plane[plane].band[1][1].stride;
  563. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  564. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  565. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  566. ret = AVERROR(EINVAL);
  567. goto end;
  568. }
  569. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  570. low = s->plane[plane].subband[0];
  571. high = s->plane[plane].subband[5];
  572. output = s->plane[plane].l_h[3];
  573. for (i = 0; i < lowpass_width; i++) {
  574. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  575. low++;
  576. high++;
  577. output++;
  578. }
  579. low = s->plane[plane].subband[4];
  580. high = s->plane[plane].subband[6];
  581. output = s->plane[plane].l_h[4];
  582. for (i = 0; i < lowpass_width; i++) {
  583. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  584. low++;
  585. high++;
  586. output++;
  587. }
  588. low = s->plane[plane].l_h[3];
  589. high = s->plane[plane].l_h[4];
  590. output = s->plane[plane].subband[0];
  591. for (i = 0; i < lowpass_height * 2; i++) {
  592. horiz_filter(output, low, high, lowpass_width);
  593. low += lowpass_width;
  594. high += lowpass_width;
  595. output += lowpass_width * 2;
  596. }
  597. output = s->plane[plane].subband[0];
  598. for (i = 0; i < lowpass_height * 2; i++) {
  599. for (j = 0; j < lowpass_width * 2; j++)
  600. output[j] <<= 2;
  601. output += lowpass_width * 2;
  602. }
  603. /* level 3 */
  604. lowpass_height = s->plane[plane].band[2][1].height;
  605. lowpass_width = s->plane[plane].band[2][1].width;
  606. highpass_stride = s->plane[plane].band[2][1].stride;
  607. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  608. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  609. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  610. ret = AVERROR(EINVAL);
  611. goto end;
  612. }
  613. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  614. low = s->plane[plane].subband[0];
  615. high = s->plane[plane].subband[8];
  616. output = s->plane[plane].l_h[6];
  617. for (i = 0; i < lowpass_width; i++) {
  618. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  619. low++;
  620. high++;
  621. output++;
  622. }
  623. low = s->plane[plane].subband[7];
  624. high = s->plane[plane].subband[9];
  625. output = s->plane[plane].l_h[7];
  626. for (i = 0; i < lowpass_width; i++) {
  627. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  628. low++;
  629. high++;
  630. output++;
  631. }
  632. dst = (int16_t *)pic->data[act_plane];
  633. low = s->plane[plane].l_h[6];
  634. high = s->plane[plane].l_h[7];
  635. for (i = 0; i < lowpass_height * 2; i++) {
  636. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  637. low += lowpass_width;
  638. high += lowpass_width;
  639. dst += pic->linesize[act_plane] / 2;
  640. }
  641. }
  642. end:
  643. if (ret < 0)
  644. return ret;
  645. *got_frame = 1;
  646. return avpkt->size;
  647. }
  648. static av_cold int cfhd_close_decoder(AVCodecContext *avctx)
  649. {
  650. CFHDContext *s = avctx->priv_data;
  651. free_buffers(avctx);
  652. if (!avctx->internal->is_copy) {
  653. ff_free_vlc(&s->vlc_9);
  654. ff_free_vlc(&s->vlc_18);
  655. }
  656. return 0;
  657. }
  658. AVCodec ff_cfhd_decoder = {
  659. .name = "cfhd",
  660. .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
  661. .type = AVMEDIA_TYPE_VIDEO,
  662. .id = AV_CODEC_ID_CFHD,
  663. .priv_data_size = sizeof(CFHDContext),
  664. .init = cfhd_decode_init,
  665. .close = cfhd_close_decoder,
  666. .decode = cfhd_decode,
  667. .capabilities = AV_CODEC_CAP_DR1,
  668. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  669. };