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.

343 lines
10KB

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