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.

350 lines
11KB

  1. /*
  2. * Copyright (c) 2003 Michael Niedermayer
  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. * ASUS V1/V2 decoder.
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/mem.h"
  26. #include "asv.h"
  27. #include "avcodec.h"
  28. #include "blockdsp.h"
  29. #include "idctdsp.h"
  30. #include "internal.h"
  31. #include "mathops.h"
  32. #include "mpeg12data.h"
  33. #define CCP_VLC_BITS 5
  34. #define DC_CCP_VLC_BITS 4
  35. #define AC_CCP_VLC_BITS 6
  36. #define ASV1_LEVEL_VLC_BITS 4
  37. #define ASV2_LEVEL_VLC_BITS 10
  38. static VLC ccp_vlc;
  39. static VLC level_vlc;
  40. static VLC dc_ccp_vlc;
  41. static VLC ac_ccp_vlc;
  42. static VLC asv2_level_vlc;
  43. static av_cold void init_vlcs(ASV1Context *a)
  44. {
  45. static int done = 0;
  46. if (!done) {
  47. done = 1;
  48. INIT_VLC_STATIC(&ccp_vlc, CCP_VLC_BITS, 17,
  49. &ff_asv_ccp_tab[0][1], 2, 1,
  50. &ff_asv_ccp_tab[0][0], 2, 1, 32);
  51. INIT_VLC_STATIC(&dc_ccp_vlc, DC_CCP_VLC_BITS, 8,
  52. &ff_asv_dc_ccp_tab[0][1], 2, 1,
  53. &ff_asv_dc_ccp_tab[0][0], 2, 1, 16);
  54. INIT_VLC_STATIC(&ac_ccp_vlc, AC_CCP_VLC_BITS, 16,
  55. &ff_asv_ac_ccp_tab[0][1], 2, 1,
  56. &ff_asv_ac_ccp_tab[0][0], 2, 1, 64);
  57. INIT_VLC_STATIC(&level_vlc, ASV1_LEVEL_VLC_BITS, 7,
  58. &ff_asv_level_tab[0][1], 2, 1,
  59. &ff_asv_level_tab[0][0], 2, 1, 16);
  60. INIT_VLC_STATIC(&asv2_level_vlc, ASV2_LEVEL_VLC_BITS, 63,
  61. &ff_asv2_level_tab[0][1], 2, 1,
  62. &ff_asv2_level_tab[0][0], 2, 1, 1024);
  63. }
  64. }
  65. // FIXME write a reversed bitstream reader to avoid the double reverse
  66. static inline int asv2_get_bits(GetBitContext *gb, int n)
  67. {
  68. return ff_reverse[get_bits(gb, n) << (8 - n)];
  69. }
  70. static inline int asv1_get_level(GetBitContext *gb)
  71. {
  72. int code = get_vlc2(gb, level_vlc.table, ASV1_LEVEL_VLC_BITS, 1);
  73. if (code == 3)
  74. return get_sbits(gb, 8);
  75. else
  76. return code - 3;
  77. }
  78. static inline int asv2_get_level(GetBitContext *gb)
  79. {
  80. int code = get_vlc2(gb, asv2_level_vlc.table, ASV2_LEVEL_VLC_BITS, 1);
  81. if (code == 31)
  82. return (int8_t) asv2_get_bits(gb, 8);
  83. else
  84. return code - 31;
  85. }
  86. static inline int asv1_decode_block(ASV1Context *a, int16_t block[64])
  87. {
  88. int i;
  89. block[0] = 8 * get_bits(&a->gb, 8);
  90. for (i = 0; i < 11; i++) {
  91. const int ccp = get_vlc2(&a->gb, ccp_vlc.table, CCP_VLC_BITS, 1);
  92. if (ccp) {
  93. if (ccp == 16)
  94. break;
  95. if (ccp < 0 || i >= 10) {
  96. av_log(a->avctx, AV_LOG_ERROR, "coded coeff pattern damaged\n");
  97. return AVERROR_INVALIDDATA;
  98. }
  99. if (ccp & 8)
  100. block[a->scantable.permutated[4 * i + 0]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 0]) >> 4;
  101. if (ccp & 4)
  102. block[a->scantable.permutated[4 * i + 1]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 1]) >> 4;
  103. if (ccp & 2)
  104. block[a->scantable.permutated[4 * i + 2]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 2]) >> 4;
  105. if (ccp & 1)
  106. block[a->scantable.permutated[4 * i + 3]] = (asv1_get_level(&a->gb) * a->intra_matrix[4 * i + 3]) >> 4;
  107. }
  108. }
  109. return 0;
  110. }
  111. static inline int asv2_decode_block(ASV1Context *a, int16_t block[64])
  112. {
  113. int i, count, ccp;
  114. count = asv2_get_bits(&a->gb, 4);
  115. block[0] = 8 * asv2_get_bits(&a->gb, 8);
  116. ccp = get_vlc2(&a->gb, dc_ccp_vlc.table, DC_CCP_VLC_BITS, 1);
  117. if (ccp) {
  118. if (ccp & 4)
  119. block[a->scantable.permutated[1]] = (asv2_get_level(&a->gb) * a->intra_matrix[1]) >> 4;
  120. if (ccp & 2)
  121. block[a->scantable.permutated[2]] = (asv2_get_level(&a->gb) * a->intra_matrix[2]) >> 4;
  122. if (ccp & 1)
  123. block[a->scantable.permutated[3]] = (asv2_get_level(&a->gb) * a->intra_matrix[3]) >> 4;
  124. }
  125. for (i = 1; i < count + 1; i++) {
  126. const int ccp = get_vlc2(&a->gb, ac_ccp_vlc.table, AC_CCP_VLC_BITS, 1);
  127. if (ccp) {
  128. if (ccp & 8)
  129. block[a->scantable.permutated[4 * i + 0]] = (asv2_get_level(&a->gb) * a->intra_matrix[4 * i + 0]) >> 4;
  130. if (ccp & 4)
  131. block[a->scantable.permutated[4 * i + 1]] = (asv2_get_level(&a->gb) * a->intra_matrix[4 * i + 1]) >> 4;
  132. if (ccp & 2)
  133. block[a->scantable.permutated[4 * i + 2]] = (asv2_get_level(&a->gb) * a->intra_matrix[4 * i + 2]) >> 4;
  134. if (ccp & 1)
  135. block[a->scantable.permutated[4 * i + 3]] = (asv2_get_level(&a->gb) * a->intra_matrix[4 * i + 3]) >> 4;
  136. }
  137. }
  138. return 0;
  139. }
  140. static inline int decode_mb(ASV1Context *a, int16_t block[6][64])
  141. {
  142. int i, ret;
  143. a->bdsp.clear_blocks(block[0]);
  144. if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
  145. for (i = 0; i < 6; i++) {
  146. if ((ret = asv1_decode_block(a, block[i])) < 0)
  147. return ret;
  148. }
  149. } else {
  150. for (i = 0; i < 6; i++) {
  151. if ((ret = asv2_decode_block(a, block[i])) < 0)
  152. return ret;
  153. }
  154. }
  155. return 0;
  156. }
  157. static inline void idct_put(ASV1Context *a, AVFrame *frame, int mb_x, int mb_y)
  158. {
  159. int16_t(*block)[64] = a->block;
  160. int linesize = frame->linesize[0];
  161. uint8_t *dest_y = frame->data[0] + (mb_y * 16 * linesize) + mb_x * 16;
  162. uint8_t *dest_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
  163. uint8_t *dest_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
  164. a->idsp.idct_put(dest_y, linesize, block[0]);
  165. a->idsp.idct_put(dest_y + 8, linesize, block[1]);
  166. a->idsp.idct_put(dest_y + 8 * linesize, linesize, block[2]);
  167. a->idsp.idct_put(dest_y + 8 * linesize + 8, linesize, block[3]);
  168. if (!(a->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  169. a->idsp.idct_put(dest_cb, frame->linesize[1], block[4]);
  170. a->idsp.idct_put(dest_cr, frame->linesize[2], block[5]);
  171. }
  172. }
  173. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  174. AVPacket *avpkt)
  175. {
  176. ASV1Context *const a = avctx->priv_data;
  177. const uint8_t *buf = avpkt->data;
  178. int buf_size = avpkt->size;
  179. AVFrame *const p = data;
  180. int mb_x, mb_y, ret;
  181. if (buf_size * 8LL < a->mb_height * a->mb_width * 13LL)
  182. return AVERROR_INVALIDDATA;
  183. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  184. return ret;
  185. p->pict_type = AV_PICTURE_TYPE_I;
  186. p->key_frame = 1;
  187. av_fast_padded_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size,
  188. buf_size);
  189. if (!a->bitstream_buffer)
  190. return AVERROR(ENOMEM);
  191. if (avctx->codec_id == AV_CODEC_ID_ASV1) {
  192. a->bbdsp.bswap_buf((uint32_t *) a->bitstream_buffer,
  193. (const uint32_t *) buf, buf_size / 4);
  194. } else {
  195. int i;
  196. for (i = 0; i < buf_size; i++)
  197. a->bitstream_buffer[i] = ff_reverse[buf[i]];
  198. }
  199. ret = init_get_bits8(&a->gb, a->bitstream_buffer, buf_size);
  200. if (ret < 0)
  201. return ret;
  202. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  203. for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
  204. if ((ret = decode_mb(a, a->block)) < 0)
  205. return ret;
  206. idct_put(a, p, mb_x, mb_y);
  207. }
  208. }
  209. if (a->mb_width2 != a->mb_width) {
  210. mb_x = a->mb_width2;
  211. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  212. if ((ret = decode_mb(a, a->block)) < 0)
  213. return ret;
  214. idct_put(a, p, mb_x, mb_y);
  215. }
  216. }
  217. if (a->mb_height2 != a->mb_height) {
  218. mb_y = a->mb_height2;
  219. for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
  220. if ((ret = decode_mb(a, a->block)) < 0)
  221. return ret;
  222. idct_put(a, p, mb_x, mb_y);
  223. }
  224. }
  225. *got_frame = 1;
  226. emms_c();
  227. return (get_bits_count(&a->gb) + 31) / 32 * 4;
  228. }
  229. static av_cold int decode_init(AVCodecContext *avctx)
  230. {
  231. ASV1Context *const a = avctx->priv_data;
  232. const int scale = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
  233. int i;
  234. if (avctx->extradata_size < 1) {
  235. av_log(avctx, AV_LOG_WARNING, "No extradata provided\n");
  236. }
  237. ff_asv_common_init(avctx);
  238. ff_blockdsp_init(&a->bdsp, avctx);
  239. ff_idctdsp_init(&a->idsp, avctx);
  240. init_vlcs(a);
  241. ff_init_scantable(a->idsp.idct_permutation, &a->scantable, ff_asv_scantab);
  242. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  243. if (avctx->extradata_size < 1 || (a->inv_qscale = avctx->extradata[0]) == 0) {
  244. av_log(avctx, AV_LOG_ERROR, "illegal qscale 0\n");
  245. if (avctx->codec_id == AV_CODEC_ID_ASV1)
  246. a->inv_qscale = 6;
  247. else
  248. a->inv_qscale = 10;
  249. }
  250. for (i = 0; i < 64; i++) {
  251. int index = ff_asv_scantab[i];
  252. a->intra_matrix[i] = 64 * scale * ff_mpeg1_default_intra_matrix[index] /
  253. a->inv_qscale;
  254. }
  255. return 0;
  256. }
  257. static av_cold int decode_end(AVCodecContext *avctx)
  258. {
  259. ASV1Context *const a = avctx->priv_data;
  260. av_freep(&a->bitstream_buffer);
  261. a->bitstream_buffer_size = 0;
  262. return 0;
  263. }
  264. #if CONFIG_ASV1_DECODER
  265. AVCodec ff_asv1_decoder = {
  266. .name = "asv1",
  267. .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
  268. .type = AVMEDIA_TYPE_VIDEO,
  269. .id = AV_CODEC_ID_ASV1,
  270. .priv_data_size = sizeof(ASV1Context),
  271. .init = decode_init,
  272. .close = decode_end,
  273. .decode = decode_frame,
  274. .capabilities = AV_CODEC_CAP_DR1,
  275. };
  276. #endif
  277. #if CONFIG_ASV2_DECODER
  278. AVCodec ff_asv2_decoder = {
  279. .name = "asv2",
  280. .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
  281. .type = AVMEDIA_TYPE_VIDEO,
  282. .id = AV_CODEC_ID_ASV2,
  283. .priv_data_size = sizeof(ASV1Context),
  284. .init = decode_init,
  285. .close = decode_end,
  286. .decode = decode_frame,
  287. .capabilities = AV_CODEC_CAP_DR1,
  288. };
  289. #endif