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.

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