Guest User

Untitled

a guest
Mar 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.14 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2015-2016 Kieran Kunhya <[email protected]>
  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. /**
  22. * @file
  23. * Cineform HD video decoder
  24. */
  25.  
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/buffer.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/opt.h"
  32.  
  33. #include "avcodec.h"
  34. #include "bytestream.h"
  35. #include "get_bits.h"
  36. #include "internal.h"
  37. #include "thread.h"
  38. #include "cfhd.h"
  39.  
  40. enum CFHDParam {
  41. ChannelCount = 12,
  42. SubbandCount = 14,
  43. ImageWidth = 20,
  44. ImageHeight = 21,
  45. LowpassPrecision = 35,
  46. SubbandNumber = 48,
  47. Quantization = 53,
  48. ChannelNumber = 62,
  49. BitsPerComponent = 101,
  50. ChannelWidth = 104,
  51. ChannelHeight = 105,
  52. PrescaleShift = 109,
  53. Progressive = 68,
  54. };
  55.  
  56.  
  57.  
  58. static av_cold int cfhd_init(AVCodecContext *avctx)
  59. {
  60. CFHDContext *s = avctx->priv_data;
  61.  
  62. avctx->bits_per_raw_sample = 10;
  63. s->avctx = avctx;
  64.  
  65. return ff_cfhd_init_vlcs(s);
  66. }
  67.  
  68. static void init_plane_defaults(CFHDContext *s)
  69. {
  70. s->subband_num = 0;
  71. s->level = 0;
  72. s->subband_num_actual = 0;
  73. }
  74.  
  75. static void init_frame_defaults(CFHDContext *s)
  76. {
  77. s->coded_width = 0;
  78. s->coded_height = 0;
  79. s->cropped_height = 0;
  80. s->bpc = 10;
  81. s->channel_cnt = 4;
  82. s->subband_cnt = SUBBAND_COUNT;
  83. s->channel_num = 0;
  84. s->lowpass_precision = 16;
  85. s->quantisation = 1;
  86. s->wavelet_depth = 3;
  87. s->pshift = 1;
  88. s->codebook = 0;
  89. s->progressive = 0;
  90. init_plane_defaults(s);
  91. }
  92.  
  93. /* TODO: merge with VLC tables or use LUT */
  94. static inline int dequant_and_decompand(int level, int quantisation)
  95. {
  96. int64_t abslevel = abs(level);
  97. return (abslevel + ((768 * abslevel * abslevel * abslevel) / (255 * 255 * 255))) *
  98. FFSIGN(level) * quantisation;
  99. }
  100.  
  101. static inline void filter(int16_t *output, ptrdiff_t out_stride,
  102. int16_t *low, ptrdiff_t low_stride,
  103. int16_t *high, ptrdiff_t high_stride,
  104. int len, int clip)
  105. {
  106. int16_t tmp;
  107. int i;
  108.  
  109. for (i = 0; i < len; i++) {
  110. if (i == 0) {
  111. tmp = (11*low[0*low_stride] - 4*low[1*low_stride] + low[2*low_stride] + 4) >> 3;
  112. output[(2*i+0)*out_stride] = (tmp + high[0*high_stride]) >> 1;
  113. if (clip)
  114. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  115.  
  116. tmp = ( 5*low[0*low_stride] + 4*low[1*low_stride] - low[2*low_stride] + 4) >> 3;
  117. output[(2*i+1)*out_stride] = (tmp - high[0*high_stride]) >> 1;
  118. if (clip)
  119. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  120. } else if (i == len-1) {
  121. tmp = ( 5*low[i*low_stride] + 4*low[(i-1)*low_stride] - low[(i-2)*low_stride] + 4) >> 3;
  122. output[(2*i+0)*out_stride] = (tmp + high[i*high_stride]) >> 1;
  123. if (clip)
  124. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  125.  
  126. tmp = (11*low[i*low_stride] - 4*low[(i-1)*low_stride] + low[(i-2)*low_stride] + 4) >> 3;
  127. output[(2*i+1)*out_stride] = (tmp - high[i*high_stride]) >> 1;
  128. if (clip)
  129. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  130. } else {
  131. tmp = (low[(i-1)*low_stride] - low[(i+1)*low_stride] + 4) >> 3;
  132. output[(2*i+0)*out_stride] = (tmp + low[i*low_stride] + high[i*high_stride]) >> 1;
  133. if (clip)
  134. output[(2*i+0)*out_stride] = av_clip_uintp2_c(output[(2*i+0)*out_stride], clip);
  135.  
  136. tmp = (low[(i+1)*low_stride] - low[(i-1)*low_stride] + 4) >> 3;
  137. output[(2*i+1)*out_stride] = (tmp + low[i*low_stride] - high[i*high_stride]) >> 1;
  138. if (clip)
  139. output[(2*i+1)*out_stride] = av_clip_uintp2_c(output[(2*i+1)*out_stride], clip);
  140. }
  141. }
  142. }
  143.  
  144. static inline void interlaced_vertical_filter(int16_t *output, int16_t *low, int16_t *high,
  145. int width, int linesize)
  146. {
  147. int i;
  148. for (i = 0; i < width; i++) {
  149. output[i] = (*low + *high)/2;
  150. output[i + linesize] = (*low - *high)/2;
  151. low++;
  152. high++;
  153. }
  154. }
  155. static void horiz_filter(int16_t *output, int16_t *low, int16_t *high,
  156. int width)
  157. {
  158. filter(output, 1, low, 1, high, 1, width, 0);
  159. }
  160.  
  161. static void horiz_filter_clip(int16_t *output, int16_t *low, int16_t *high,
  162. int width, int clip)
  163. {
  164. filter(output, 1, low, 1, high, 1, width, clip);
  165. }
  166.  
  167. static void vert_filter(int16_t *output, ptrdiff_t out_stride,
  168. int16_t *low, ptrdiff_t low_stride,
  169. int16_t *high, ptrdiff_t high_stride, int len)
  170. {
  171. filter(output, out_stride, low, low_stride, high, high_stride, len, 0);
  172. }
  173.  
  174. static void free_buffers(CFHDContext *s)
  175. {
  176. int i, j;
  177.  
  178. for (i = 0; i < FF_ARRAY_ELEMS(s->plane); i++) {
  179. av_freep(&s->plane[i].idwt_buf);
  180. av_freep(&s->plane[i].idwt_tmp);
  181.  
  182. for (j = 0; j < 9; j++)
  183. s->plane[i].subband[j] = NULL;
  184.  
  185. for (j = 0; j < 8; j++)
  186. s->plane[i].l_h[j] = NULL;
  187. }
  188. s->a_height = 0;
  189. s->a_width = 0;
  190. }
  191.  
  192. static int alloc_buffers(AVCodecContext *avctx)
  193. {
  194. CFHDContext *s = avctx->priv_data;
  195. int i, j, ret, planes;
  196. int chroma_x_shift, chroma_y_shift;
  197. unsigned k;
  198.  
  199. if ((ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height)) < 0)
  200. return ret;
  201. avctx->pix_fmt = s->coded_format;
  202.  
  203. if ((ret = av_pix_fmt_get_chroma_sub_sample(s->coded_format,
  204. &chroma_x_shift,
  205. &chroma_y_shift)) < 0)
  206. return ret;
  207. planes = av_pix_fmt_count_planes(s->coded_format);
  208.  
  209. for (i = 0; i < planes; i++) {
  210. int w8, h8, w4, h4, w2, h2;
  211. int width = i ? avctx->width >> chroma_x_shift : avctx->width;
  212. int height = i ? avctx->height >> chroma_y_shift : avctx->height;
  213. ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
  214. height = FFALIGN(height / 8, 2) * 8;
  215. s->plane[i].width = width;
  216. s->plane[i].height = height;
  217. s->plane[i].stride = stride;
  218.  
  219. w8 = FFALIGN(s->plane[i].width / 8, 8);
  220. h8 = FFALIGN(s->plane[i].height / 8, 2);
  221. w4 = w8 * 2;
  222. h4 = h8 * 2;
  223. w2 = w4 * 2;
  224. h2 = h4 * 2;
  225.  
  226. s->plane[i].idwt_buf =
  227. av_mallocz_array(height * stride, sizeof(*s->plane[i].idwt_buf));
  228. s->plane[i].idwt_tmp =
  229. av_malloc_array(height * stride, sizeof(*s->plane[i].idwt_tmp));
  230. if (!s->plane[i].idwt_buf || !s->plane[i].idwt_tmp)
  231. return AVERROR(ENOMEM);
  232.  
  233. s->plane[i].subband[0] = s->plane[i].idwt_buf;
  234. s->plane[i].subband[1] = s->plane[i].idwt_buf + 2 * w8 * h8;
  235. s->plane[i].subband[2] = s->plane[i].idwt_buf + 1 * w8 * h8;
  236. s->plane[i].subband[3] = s->plane[i].idwt_buf + 3 * w8 * h8;
  237. s->plane[i].subband[4] = s->plane[i].idwt_buf + 2 * w4 * h4;
  238. s->plane[i].subband[5] = s->plane[i].idwt_buf + 1 * w4 * h4;
  239. s->plane[i].subband[6] = s->plane[i].idwt_buf + 3 * w4 * h4;
  240. s->plane[i].subband[7] = s->plane[i].idwt_buf + 2 * w2 * h2;
  241. s->plane[i].subband[8] = s->plane[i].idwt_buf + 1 * w2 * h2;
  242. s->plane[i].subband[9] = s->plane[i].idwt_buf + 3 * w2 * h2;
  243.  
  244. for (j = 0; j < DWT_LEVELS; j++) {
  245. for (k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  246. s->plane[i].band[j][k].a_width = w8 << j;
  247. s->plane[i].band[j][k].a_height = h8 << j;
  248. }
  249. }
  250.  
  251. /* ll2 and ll1 commented out because they are done in-place */
  252. s->plane[i].l_h[0] = s->plane[i].idwt_tmp;
  253. s->plane[i].l_h[1] = s->plane[i].idwt_tmp + 2 * w8 * h8;
  254. // s->plane[i].l_h[2] = ll2;
  255. s->plane[i].l_h[3] = s->plane[i].idwt_tmp;
  256. s->plane[i].l_h[4] = s->plane[i].idwt_tmp + 2 * w4 * h4;
  257. // s->plane[i].l_h[5] = ll1;
  258. s->plane[i].l_h[6] = s->plane[i].idwt_tmp;
  259. s->plane[i].l_h[7] = s->plane[i].idwt_tmp + 2 * w2 * h2;
  260. }
  261.  
  262. s->a_height = s->coded_height;
  263. s->a_width = s->coded_width;
  264. s->a_format = s->coded_format;
  265.  
  266. return 0;
  267. }
  268.  
  269. static int cfhd_decode(AVCodecContext *avctx, void *data, int *got_frame,
  270. AVPacket *avpkt)
  271. {
  272. CFHDContext *s = avctx->priv_data;
  273. GetByteContext gb;
  274. ThreadFrame frame = { .f = data };
  275. AVFrame *pic = data;
  276. int ret = 0, i, j, planes, plane, got_buffer = 0;
  277. int16_t *coeff_data;
  278.  
  279. s->coded_format = AV_PIX_FMT_YUV422P10;
  280. init_frame_defaults(s);
  281. planes = av_pix_fmt_count_planes(s->coded_format);
  282.  
  283. bytestream2_init(&gb, avpkt->data, avpkt->size);
  284.  
  285. while (bytestream2_get_bytes_left(&gb) > 4) {
  286. /* Bit weird but implement the tag parsing as the spec says */
  287. uint16_t tagu = bytestream2_get_be16(&gb);
  288. int16_t tag = (int16_t)tagu;
  289. int8_t tag8 = (int8_t)(tagu >> 8);
  290. uint16_t abstag = abs(tag);
  291. int8_t abs_tag8 = abs(tag8);
  292. uint16_t data = bytestream2_get_be16(&gb);
  293. if (abs_tag8 >= 0x60 && abs_tag8 <= 0x6f) {
  294. av_log(avctx, AV_LOG_DEBUG, "large len %x\n", ((tagu & 0xff) << 16) | data);
  295. } else if (tag == Progressive) {
  296. av_log(avctx, AV_LOG_DEBUG, "Progressive?%"PRIu16"\n", data);
  297. s->progressive = data;
  298. } else if (tag == ImageWidth) {
  299. av_log(avctx, AV_LOG_DEBUG, "Width %"PRIu16"\n", data);
  300. s->coded_width = data;
  301. } else if (tag == ImageHeight) {
  302. av_log(avctx, AV_LOG_DEBUG, "Height %"PRIu16"\n", data);
  303. s->coded_height = data;
  304. } else if (tag == 101) {
  305. av_log(avctx, AV_LOG_DEBUG, "Bits per component: %"PRIu16"\n", data);
  306. if (data < 1 || data > 31) {
  307. av_log(avctx, AV_LOG_ERROR, "Bits per component %d is invalid\n", data);
  308. ret = AVERROR(EINVAL);
  309. break;
  310. }
  311. s->bpc = data;
  312. } else if (tag == ChannelCount) {
  313. av_log(avctx, AV_LOG_DEBUG, "Channel Count: %"PRIu16"\n", data);
  314. s->channel_cnt = data;
  315. if (data > 4) {
  316. av_log(avctx, AV_LOG_ERROR, "Channel Count of %"PRIu16" is unsupported\n", data);
  317. ret = AVERROR_PATCHWELCOME;
  318. break;
  319. }
  320. } else if (tag == SubbandCount) {
  321. av_log(avctx, AV_LOG_DEBUG, "Subband Count: %"PRIu16"\n", data);
  322. if (data != SUBBAND_COUNT) {
  323. av_log(avctx, AV_LOG_ERROR, "Subband Count of %"PRIu16" is unsupported\n", data);
  324. ret = AVERROR_PATCHWELCOME;
  325. break;
  326. }
  327. } else if (tag == ChannelNumber) {
  328. s->channel_num = data;
  329. av_log(avctx, AV_LOG_DEBUG, "Channel number %"PRIu16"\n", data);
  330. if (s->channel_num >= planes) {
  331. av_log(avctx, AV_LOG_ERROR, "Invalid channel number\n");
  332. ret = AVERROR(EINVAL);
  333. break;
  334. }
  335. init_plane_defaults(s);
  336. } else if (tag == SubbandNumber) {
  337. if (s->subband_num != 0 && data == 1) // hack
  338. s->level++;
  339. av_log(avctx, AV_LOG_DEBUG, "Subband number %"PRIu16"\n", data);
  340. s->subband_num = data;
  341. if (s->level >= DWT_LEVELS) {
  342. av_log(avctx, AV_LOG_ERROR, "Invalid level\n");
  343. ret = AVERROR(EINVAL);
  344. break;
  345. }
  346. if (s->subband_num > 3) {
  347. av_log(avctx, AV_LOG_ERROR, "Invalid subband number\n");
  348. ret = AVERROR(EINVAL);
  349. break;
  350. }
  351. } else if (tag == 51) {
  352. av_log(avctx, AV_LOG_DEBUG, "Subband number actual %"PRIu16"\n", data);
  353. s->subband_num_actual = data;
  354. if (s->subband_num_actual >= 10) {
  355. av_log(avctx, AV_LOG_ERROR, "Invalid subband number actual\n");
  356. ret = AVERROR(EINVAL);
  357. break;
  358. }
  359. } else if (tag == LowpassPrecision)
  360. av_log(avctx, AV_LOG_DEBUG, "Lowpass precision bits: %"PRIu16"\n", data);
  361. else if (tag == Quantization) {
  362. s->quantisation = data;
  363. av_log(avctx, AV_LOG_DEBUG, "Quantisation: %"PRIu16"\n", data);
  364. } else if (tag == PrescaleShift) {
  365. s->prescale_shift[0] = (data >> 0) & 0x7;
  366. s->prescale_shift[1] = (data >> 3) & 0x7;
  367. s->prescale_shift[2] = (data >> 6) & 0x7;
  368. av_log(avctx, AV_LOG_DEBUG, "Prescale shift (VC-5): %x\n", data);
  369. } else if (tag == 27) {
  370. av_log(avctx, AV_LOG_DEBUG, "Lowpass width %"PRIu16"\n", data);
  371. if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_width) {
  372. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass width\n");
  373. ret = AVERROR(EINVAL);
  374. break;
  375. }
  376. s->plane[s->channel_num].band[0][0].width = data;
  377. s->plane[s->channel_num].band[0][0].stride = data;
  378. } else if (tag == 28) {
  379. av_log(avctx, AV_LOG_DEBUG, "Lowpass height %"PRIu16"\n", data);
  380. if (data < 3 || data > s->plane[s->channel_num].band[0][0].a_height) {
  381. av_log(avctx, AV_LOG_ERROR, "Invalid lowpass height\n");
  382. ret = AVERROR(EINVAL);
  383. break;
  384. }
  385. s->plane[s->channel_num].band[0][0].height = data;
  386. } else if (tag == 1)
  387. av_log(avctx, AV_LOG_DEBUG, "Sample type? %"PRIu16"\n", data);
  388. else if (tag == 10) {
  389. if (data != 0) {
  390. avpriv_report_missing_feature(avctx, "Transform type of %"PRIu16, data);
  391. ret = AVERROR_PATCHWELCOME;
  392. break;
  393. }
  394. av_log(avctx, AV_LOG_DEBUG, "Transform-type? %"PRIu16"\n", data);
  395. } else if (abstag >= 0x4000 && abstag <= 0x40ff) {
  396. av_log(avctx, AV_LOG_DEBUG, "Small chunk length %d %s\n", data * 4, tag < 0 ? "optional" : "required");
  397. bytestream2_skipu(&gb, data * 4);
  398. } else if (tag == 23) {
  399. av_log(avctx, AV_LOG_DEBUG, "Skip frame\n");
  400. avpriv_report_missing_feature(avctx, "Skip frame");
  401. ret = AVERROR_PATCHWELCOME;
  402. break;
  403. } else if (tag == 2) {
  404. av_log(avctx, AV_LOG_DEBUG, "tag=2 header - skipping %i tag/value pairs\n", data);
  405. if (data > bytestream2_get_bytes_left(&gb) / 4) {
  406. av_log(avctx, AV_LOG_ERROR, "too many tag/value pairs (%d)\n", data);
  407. ret = AVERROR_INVALIDDATA;
  408. break;
  409. }
  410. for (i = 0; i < data; i++) {
  411. uint16_t tag2 = bytestream2_get_be16(&gb);
  412. uint16_t val2 = bytestream2_get_be16(&gb);
  413. av_log(avctx, AV_LOG_DEBUG, "Tag/Value = %x %x\n", tag2, val2);
  414. }
  415. } else if (tag == 41) {
  416. 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);
  417. if (data < 3) {
  418. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width\n");
  419. ret = AVERROR(EINVAL);
  420. break;
  421. }
  422. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  423. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  424. } else if (tag == 42) {
  425. av_log(avctx, AV_LOG_DEBUG, "Highpass height %i\n", data);
  426. if (data < 3) {
  427. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height\n");
  428. ret = AVERROR(EINVAL);
  429. break;
  430. }
  431. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  432. } else if (tag == 49) {
  433. av_log(avctx, AV_LOG_DEBUG, "Highpass width2 %i\n", data);
  434. if (data < 3) {
  435. av_log(avctx, AV_LOG_ERROR, "Invalid highpass width2\n");
  436. ret = AVERROR(EINVAL);
  437. break;
  438. }
  439. s->plane[s->channel_num].band[s->level][s->subband_num].width = data;
  440. s->plane[s->channel_num].band[s->level][s->subband_num].stride = FFALIGN(data, 8);
  441. } else if (tag == 50) {
  442. av_log(avctx, AV_LOG_DEBUG, "Highpass height2 %i\n", data);
  443. if (data < 3) {
  444. av_log(avctx, AV_LOG_ERROR, "Invalid highpass height2\n");
  445. ret = AVERROR(EINVAL);
  446. break;
  447. }
  448. s->plane[s->channel_num].band[s->level][s->subband_num].height = data;
  449. } else if (tag == 71) {
  450. s->codebook = data;
  451. av_log(avctx, AV_LOG_DEBUG, "Codebook %i\n", s->codebook);
  452. } else if (tag == 72) {
  453. s->codebook = data;
  454. av_log(avctx, AV_LOG_DEBUG, "Other codebook? %i\n", s->codebook);
  455. } else if (tag == 70) {
  456. av_log(avctx, AV_LOG_DEBUG, "Subsampling or bit-depth flag? %i\n", data);
  457. if (!(data == 10 || data == 12)) {
  458. av_log(avctx, AV_LOG_ERROR, "Invalid bits per channel\n");
  459. ret = AVERROR(EINVAL);
  460. break;
  461. }
  462. s->bpc = data;
  463. } else if (tag == 84) {
  464. av_log(avctx, AV_LOG_DEBUG, "Sample format? %i\n", data);
  465. if (data == 1)
  466. s->coded_format = AV_PIX_FMT_YUV422P10;
  467. else if (data == 3)
  468. s->coded_format = AV_PIX_FMT_GBRP12;
  469. else if (data == 4)
  470. s->coded_format = AV_PIX_FMT_GBRAP12;
  471. else {
  472. avpriv_report_missing_feature(avctx, "Sample format of %"PRIu16, data);
  473. ret = AVERROR_PATCHWELCOME;
  474. break;
  475. }
  476. planes = av_pix_fmt_count_planes(s->coded_format);
  477. } else if (tag == -85) {
  478. av_log(avctx, AV_LOG_DEBUG, "Cropped height %"PRIu16"\n", data);
  479. s->cropped_height = data;
  480. } else
  481. av_log(avctx, AV_LOG_DEBUG, "Unknown tag %i data %x\n", tag, data);
  482.  
  483. /* Some kind of end of header tag */
  484. if (tag == 4 && data == 0x1a4a && s->coded_width && s->coded_height &&
  485. s->coded_format != AV_PIX_FMT_NONE) {
  486. if (s->a_width != s->coded_width || s->a_height != s->coded_height ||
  487. s->a_format != s->coded_format) {
  488. free_buffers(s);
  489. if ((ret = alloc_buffers(avctx)) < 0) {
  490. free_buffers(s);
  491. return ret;
  492. }
  493. }
  494. ret = ff_set_dimensions(avctx, s->coded_width, s->coded_height);
  495. if (ret < 0)
  496. return ret;
  497. if (s->cropped_height)
  498. avctx->height = s->cropped_height;
  499. frame.f->width =
  500. frame.f->height = 0;
  501.  
  502. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  503. return ret;
  504.  
  505. s->coded_width = 0;
  506. s->coded_height = 0;
  507. s->coded_format = AV_PIX_FMT_NONE;
  508. got_buffer = 1;
  509. }
  510. coeff_data = s->plane[s->channel_num].subband[s->subband_num_actual];
  511.  
  512. /* Lowpass coefficients */
  513. if (tag == 4 && data == 0xf0f && s->a_width && s->a_height) {
  514. int lowpass_height = s->plane[s->channel_num].band[0][0].height;
  515. int lowpass_width = s->plane[s->channel_num].band[0][0].width;
  516. int lowpass_a_height = s->plane[s->channel_num].band[0][0].a_height;
  517. int lowpass_a_width = s->plane[s->channel_num].band[0][0].a_width;
  518.  
  519. if (!got_buffer) {
  520. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  521. ret = AVERROR(EINVAL);
  522. goto end;
  523. }
  524.  
  525. if (lowpass_height > lowpass_a_height || lowpass_width > lowpass_a_width ||
  526. lowpass_a_width * lowpass_a_height * sizeof(int16_t) > bytestream2_get_bytes_left(&gb)) {
  527. av_log(avctx, AV_LOG_ERROR, "Too many lowpass coefficients\n");
  528. ret = AVERROR(EINVAL);
  529. goto end;
  530. }
  531.  
  532. av_log(avctx, AV_LOG_DEBUG, "Start of lowpass coeffs component %d height:%d, width:%d\n", s->channel_num, lowpass_height, lowpass_width);
  533. for (i = 0; i < lowpass_height; i++) {
  534. for (j = 0; j < lowpass_width; j++)
  535. coeff_data[j] = bytestream2_get_be16u(&gb);
  536.  
  537. coeff_data += lowpass_width;
  538. }
  539.  
  540. /* Align to mod-4 position to continue reading tags */
  541. bytestream2_seek(&gb, bytestream2_tell(&gb) & 3, SEEK_CUR);
  542.  
  543. /* Copy last line of coefficients if odd height */
  544. if (lowpass_height & 1) {
  545. memcpy(&coeff_data[lowpass_height * lowpass_width],
  546. &coeff_data[(lowpass_height - 1) * lowpass_width],
  547. lowpass_width * sizeof(*coeff_data));
  548. }
  549.  
  550. av_log(avctx, AV_LOG_DEBUG, "Lowpass coefficients %d\n", lowpass_width * lowpass_height);
  551. }
  552.  
  553. if (tag == 55 && s->subband_num_actual != 255 && s->a_width && s->a_height) {
  554. int highpass_height = s->plane[s->channel_num].band[s->level][s->subband_num].height;
  555. int highpass_width = s->plane[s->channel_num].band[s->level][s->subband_num].width;
  556. int highpass_a_width = s->plane[s->channel_num].band[s->level][s->subband_num].a_width;
  557. int highpass_a_height = s->plane[s->channel_num].band[s->level][s->subband_num].a_height;
  558. int highpass_stride = s->plane[s->channel_num].band[s->level][s->subband_num].stride;
  559. int expected;
  560. int a_expected = highpass_a_height * highpass_a_width;
  561. int level, run, coeff;
  562. int count = 0, bytes;
  563.  
  564. if (!got_buffer) {
  565. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  566. ret = AVERROR(EINVAL);
  567. goto end;
  568. }
  569.  
  570. if (highpass_height > highpass_a_height || highpass_width > highpass_a_width || a_expected < highpass_height * (uint64_t)highpass_stride) {
  571. av_log(avctx, AV_LOG_ERROR, "Too many highpass coefficients\n");
  572. ret = AVERROR(EINVAL);
  573. goto end;
  574. }
  575. expected = highpass_height * highpass_stride;
  576.  
  577. 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);
  578.  
  579. init_get_bits(&s->gb, gb.buffer, bytestream2_get_bytes_left(&gb) * 8);
  580. {
  581. OPEN_READER(re, &s->gb);
  582. if (!s->codebook) {
  583. while (1) {
  584. UPDATE_CACHE(re, &s->gb);
  585. GET_RL_VLC(level, run, re, &s->gb, s->table_9_rl_vlc,
  586. VLC_BITS, 3, 1);
  587.  
  588. /* escape */
  589. if (level == 64)
  590. break;
  591.  
  592. count += run;
  593.  
  594. if (count > expected)
  595. break;
  596.  
  597. coeff = dequant_and_decompand(level, s->quantisation);
  598. for (i = 0; i < run; i++)
  599. *coeff_data++ = coeff;
  600. }
  601. } else {
  602. while (1) {
  603. UPDATE_CACHE(re, &s->gb);
  604. GET_RL_VLC(level, run, re, &s->gb, s->table_18_rl_vlc,
  605. VLC_BITS, 3, 1);
  606.  
  607. /* escape */
  608. if (level == 255 && run == 2)
  609. break;
  610.  
  611. count += run;
  612.  
  613. if (count > expected)
  614. break;
  615.  
  616. coeff = dequant_and_decompand(level, s->quantisation);
  617. for (i = 0; i < run; i++)
  618. *coeff_data++ = coeff;
  619. }
  620. }
  621. CLOSE_READER(re, &s->gb);
  622. }
  623.  
  624. if (count > expected) {
  625. av_log(avctx, AV_LOG_ERROR, "Escape codeword not found, probably corrupt data\n");
  626. ret = AVERROR(EINVAL);
  627. goto end;
  628. }
  629.  
  630. bytes = FFALIGN(FF_CEIL_RSHIFT(get_bits_count(&s->gb), 3), 4);
  631. if (bytes > bytestream2_get_bytes_left(&gb)) {
  632. av_log(avctx, AV_LOG_ERROR, "Bitstream overread error\n");
  633. ret = AVERROR(EINVAL);
  634. goto end;
  635. } else
  636. bytestream2_seek(&gb, bytes, SEEK_CUR);
  637.  
  638. av_log(avctx, AV_LOG_DEBUG, "End subband coeffs %i extra %i\n", count, count - expected);
  639. s->codebook = 0;
  640.  
  641. /* Copy last line of coefficients if odd height */
  642. if (highpass_height & 1) {
  643. memcpy(&coeff_data[highpass_height * highpass_stride],
  644. &coeff_data[(highpass_height - 1) * highpass_stride],
  645. highpass_stride * sizeof(*coeff_data));
  646. }
  647. }
  648. }
  649.  
  650. if (!s->a_width || !s->a_height || s->a_format == AV_PIX_FMT_NONE ||
  651. s->coded_width || s->coded_height || s->coded_format != AV_PIX_FMT_NONE) {
  652. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions\n");
  653. ret = AVERROR(EINVAL);
  654. goto end;
  655. }
  656.  
  657. if (!got_buffer) {
  658. av_log(avctx, AV_LOG_ERROR, "No end of header tag found\n");
  659. ret = AVERROR(EINVAL);
  660. goto end;
  661. }
  662.  
  663. planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  664. for (plane = 0; plane < planes && !ret; plane++) {
  665. /* level 1 */
  666. int lowpass_height = s->plane[plane].band[0][0].height;
  667. int lowpass_width = s->plane[plane].band[0][0].width;
  668. int highpass_stride = s->plane[plane].band[0][1].stride;
  669. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  670. int16_t *low, *high, *output, *dst;
  671.  
  672. if (lowpass_height > s->plane[plane].band[0][0].a_height || lowpass_width > s->plane[plane].band[0][0].a_width ||
  673. !highpass_stride || s->plane[plane].band[0][1].width > s->plane[plane].band[0][1].a_width) {
  674. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  675. ret = AVERROR(EINVAL);
  676. goto end;
  677. }
  678.  
  679. av_log(avctx, AV_LOG_DEBUG, "Decoding level 1 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  680.  
  681. low = s->plane[plane].subband[0];
  682. high = s->plane[plane].subband[2];
  683. output = s->plane[plane].l_h[0];
  684. for (i = 0; i < lowpass_width; i++) {
  685. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  686. low++;
  687. high++;
  688. output++;
  689. }
  690.  
  691. low = s->plane[plane].subband[1];
  692. high = s->plane[plane].subband[3];
  693. output = s->plane[plane].l_h[1];
  694.  
  695. for (i = 0; i < lowpass_width; i++) {
  696. // note the stride of "low" is highpass_stride
  697. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  698. low++;
  699. high++;
  700. output++;
  701. }
  702.  
  703. low = s->plane[plane].l_h[0];
  704. high = s->plane[plane].l_h[1];
  705. output = s->plane[plane].subband[0];
  706. for (i = 0; i < lowpass_height * 2; i++) {
  707. horiz_filter(output, low, high, lowpass_width);
  708. low += lowpass_width;
  709. high += lowpass_width;
  710. output += lowpass_width * 2;
  711. }
  712. if (s->bpc == 12) {
  713. output = s->plane[plane].subband[0];
  714. for (i = 0; i < lowpass_height * 2; i++) {
  715. for (j = 0; j < lowpass_width * 2; j++)
  716. output[j] *= 4;
  717.  
  718. output += lowpass_width * 2;
  719. }
  720. }
  721.  
  722. /* level 2 */
  723. lowpass_height = s->plane[plane].band[1][1].height;
  724. lowpass_width = s->plane[plane].band[1][1].width;
  725. highpass_stride = s->plane[plane].band[1][1].stride;
  726.  
  727. if (lowpass_height > s->plane[plane].band[1][1].a_height || lowpass_width > s->plane[plane].band[1][1].a_width ||
  728. !highpass_stride || s->plane[plane].band[1][1].width > s->plane[plane].band[1][1].a_width) {
  729. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  730. ret = AVERROR(EINVAL);
  731. goto end;
  732. }
  733.  
  734. av_log(avctx, AV_LOG_DEBUG, "Level 2 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  735.  
  736. low = s->plane[plane].subband[0];
  737. high = s->plane[plane].subband[5];
  738. output = s->plane[plane].l_h[3];
  739. for (i = 0; i < lowpass_width; i++) {
  740. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  741. low++;
  742. high++;
  743. output++;
  744. }
  745.  
  746. low = s->plane[plane].subband[4];
  747. high = s->plane[plane].subband[6];
  748. output = s->plane[plane].l_h[4];
  749. for (i = 0; i < lowpass_width; i++) {
  750. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  751. low++;
  752. high++;
  753. output++;
  754. }
  755.  
  756. low = s->plane[plane].l_h[3];
  757. high = s->plane[plane].l_h[4];
  758. output = s->plane[plane].subband[0];
  759. for (i = 0; i < lowpass_height * 2; i++) {
  760. horiz_filter(output, low, high, lowpass_width);
  761. low += lowpass_width;
  762. high += lowpass_width;
  763. output += lowpass_width * 2;
  764. }
  765.  
  766. output = s->plane[plane].subband[0];
  767. for (i = 0; i < lowpass_height * 2; i++) {
  768. for (j = 0; j < lowpass_width * 2; j++)
  769. output[j] *= 4;
  770.  
  771. output += lowpass_width * 2;
  772. }
  773.  
  774. /* level 3 */
  775. lowpass_height = s->plane[plane].band[2][1].height;
  776. lowpass_width = s->plane[plane].band[2][1].width;
  777. highpass_stride = s->plane[plane].band[2][1].stride;
  778.  
  779. if (lowpass_height > s->plane[plane].band[2][1].a_height || lowpass_width > s->plane[plane].band[2][1].a_width ||
  780. !highpass_stride || s->plane[plane].band[2][1].width > s->plane[plane].band[2][1].a_width) {
  781. av_log(avctx, AV_LOG_ERROR, "Invalid plane dimensions\n");
  782. ret = AVERROR(EINVAL);
  783. goto end;
  784. }
  785.  
  786. av_log(avctx, AV_LOG_DEBUG, "Level 3 plane %i %i %i %i\n", plane, lowpass_height, lowpass_width, highpass_stride);
  787. if (s->progressive) {
  788. low = s->plane[plane].subband[0];
  789. high = s->plane[plane].subband[8];
  790. output = s->plane[plane].l_h[6];
  791. for (i = 0; i < lowpass_width; i++) {
  792. vert_filter(output, lowpass_width, low, lowpass_width, high, highpass_stride, lowpass_height);
  793. low++;
  794. high++;
  795. output++;
  796. }
  797.  
  798. low = s->plane[plane].subband[7];
  799. high = s->plane[plane].subband[9];
  800. output = s->plane[plane].l_h[7];
  801. for (i = 0; i < lowpass_width; i++) {
  802. vert_filter(output, lowpass_width, low, highpass_stride, high, highpass_stride, lowpass_height);
  803. low++;
  804. high++;
  805. output++;
  806. }
  807.  
  808. dst = (int16_t *)pic->data[act_plane];
  809. low = s->plane[plane].l_h[6];
  810. high = s->plane[plane].l_h[7];
  811. for (i = 0; i < lowpass_height * 2; i++) {
  812. horiz_filter_clip(dst, low, high, lowpass_width, s->bpc);
  813. low += lowpass_width;
  814. high += lowpass_width;
  815. dst += pic->linesize[act_plane] / 2;
  816. }
  817. }
  818. else {
  819. // Interlaced frame needs HAAR 2,2 wavelet decomposition for Inverse vertical Transform
  820. low = s->plane[plane].subband[0];
  821. high = s->plane[plane].subband[7];
  822. output = s->plane[plane].l_h[6];
  823. for (i = 0; i < lowpass_height; i++) {
  824. horiz_filter(output, low, high, lowpass_width);
  825. low += lowpass_width;
  826. high += lowpass_width;
  827. output += lowpass_width * 2;
  828. }
  829.  
  830. low = s->plane[plane].subband[8];
  831. high = s->plane[plane].subband[9];
  832. output = s->plane[plane].l_h[7];
  833. for (i = 0; i < lowpass_height; i++) {
  834. horiz_filter(output, low, high, lowpass_width);
  835. low += lowpass_width;
  836. high += lowpass_width;
  837. output += lowpass_width * 2;
  838. }
  839. //#invertverticalfilter
  840. dst = (int16_t *)pic->data[act_plane];
  841. low = s->plane[plane].l_h[6];
  842. high = s->plane[plane].l_h[7];
  843. for (i = 0; i < lowpass_height; i++) {
  844. interlaced_vertical_filter(dst, low, high, lowpass_width * 2, pic->linesize[act_plane]/2);
  845. low += lowpass_width * 2;
  846. high += lowpass_width * 2;
  847. dst += pic->linesize[act_plane];
  848. }
  849. }
  850. }
  851.  
  852.  
  853. end:
  854. if (ret < 0)
  855. return ret;
  856.  
  857. *got_frame = 1;
  858. return avpkt->size;
  859. }
  860.  
  861. static av_cold int cfhd_close(AVCodecContext *avctx)
  862. {
  863. CFHDContext *s = avctx->priv_data;
  864.  
  865. free_buffers(s);
  866.  
  867. if (!avctx->internal->is_copy) {
  868. ff_free_vlc(&s->vlc_9);
  869. ff_free_vlc(&s->vlc_18);
  870. }
  871.  
  872. return 0;
  873. }
  874.  
  875. AVCodec ff_cfhd_decoder = {
  876. .name = "cfhd",
  877. .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
  878. .type = AVMEDIA_TYPE_VIDEO,
  879. .id = AV_CODEC_ID_CFHD,
  880. .priv_data_size = sizeof(CFHDContext),
  881. .init = cfhd_init,
  882. .close = cfhd_close,
  883. .decode = cfhd_decode,
  884. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  885. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  886. };
Add Comment
Please, Sign In to add comment