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.

244 lines
8.2KB

  1. /*
  2. * Flash Screen Video decoder
  3. * Copyright (C) 2004 Alex Beregszaszi
  4. * Copyright (C) 2006 Benjamin Larsson
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Flash Screen Video decoder
  25. * @author Alex Beregszaszi
  26. * @author Benjamin Larsson
  27. *
  28. * A description of the bitstream format for Flash Screen Video version 1/2
  29. * is part of the SWF File Format Specification (version 10), which can be
  30. * downloaded from http://www.adobe.com/devnet/swf.html.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <zlib.h>
  35. #include "avcodec.h"
  36. #include "get_bits.h"
  37. typedef struct FlashSVContext {
  38. AVCodecContext *avctx;
  39. AVFrame frame;
  40. int image_width, image_height;
  41. int block_width, block_height;
  42. uint8_t *tmpblock;
  43. int block_size;
  44. z_stream zstream;
  45. } FlashSVContext;
  46. static void copy_region(uint8_t *sptr, uint8_t *dptr,
  47. int dx, int dy, int h, int w, int stride)
  48. {
  49. int i;
  50. for (i = dx + h; i > dx; i--) {
  51. memcpy(dptr + i * stride + dy * 3, sptr, w * 3);
  52. sptr += w * 3;
  53. }
  54. }
  55. static av_cold int flashsv_decode_init(AVCodecContext *avctx)
  56. {
  57. FlashSVContext *s = avctx->priv_data;
  58. int zret; // Zlib return code
  59. s->avctx = avctx;
  60. s->zstream.zalloc = Z_NULL;
  61. s->zstream.zfree = Z_NULL;
  62. s->zstream.opaque = Z_NULL;
  63. zret = inflateInit(&s->zstream);
  64. if (zret != Z_OK) {
  65. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  66. return 1;
  67. }
  68. avctx->pix_fmt = PIX_FMT_BGR24;
  69. s->frame.data[0] = NULL;
  70. return 0;
  71. }
  72. static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
  73. int *data_size, AVPacket *avpkt)
  74. {
  75. int buf_size = avpkt->size;
  76. FlashSVContext *s = avctx->priv_data;
  77. int h_blocks, v_blocks, h_part, v_part, i, j;
  78. GetBitContext gb;
  79. /* no supplementary picture */
  80. if (buf_size == 0)
  81. return 0;
  82. if (buf_size < 4)
  83. return -1;
  84. init_get_bits(&gb, avpkt->data, buf_size * 8);
  85. /* start to parse the bitstream */
  86. s->block_width = 16 * (get_bits(&gb, 4) + 1);
  87. s->image_width = get_bits(&gb, 12);
  88. s->block_height = 16 * (get_bits(&gb, 4) + 1);
  89. s->image_height = get_bits(&gb, 12);
  90. /* calculate number of blocks and size of border (partial) blocks */
  91. h_blocks = s->image_width / s->block_width;
  92. h_part = s->image_width % s->block_width;
  93. v_blocks = s->image_height / s->block_height;
  94. v_part = s->image_height % s->block_height;
  95. /* the block size could change between frames, make sure the buffer
  96. * is large enough, if not, get a larger one */
  97. if (s->block_size < s->block_width * s->block_height) {
  98. av_free(s->tmpblock);
  99. if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
  100. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  101. return AVERROR(ENOMEM);
  102. }
  103. }
  104. s->block_size = s->block_width * s->block_height;
  105. /* initialize the image size once */
  106. if (avctx->width == 0 && avctx->height == 0) {
  107. avctx->width = s->image_width;
  108. avctx->height = s->image_height;
  109. }
  110. /* check for changes of image width and image height */
  111. if (avctx->width != s->image_width || avctx->height != s->image_height) {
  112. av_log(avctx, AV_LOG_ERROR,
  113. "Frame width or height differs from first frames!\n");
  114. av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",
  115. avctx->height, avctx->width, s->image_height, s->image_width);
  116. return AVERROR_INVALIDDATA;
  117. }
  118. av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
  119. s->image_width, s->image_height, s->block_width, s->block_height,
  120. h_blocks, v_blocks, h_part, v_part);
  121. s->frame.reference = 3;
  122. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
  123. FF_BUFFER_HINTS_PRESERVE |
  124. FF_BUFFER_HINTS_REUSABLE;
  125. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  126. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  127. return -1;
  128. }
  129. /* loop over all block columns */
  130. for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
  131. int hp = j * s->block_height; // vertical position in frame
  132. int hs = (j < v_blocks) ? s->block_height : v_part; // block size
  133. /* loop over all block rows */
  134. for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
  135. int wp = i * s->block_width; // horizontal position in frame
  136. int ws = (i < h_blocks) ? s->block_width : h_part; // block size
  137. /* get the size of the compressed zlib chunk */
  138. int size = get_bits(&gb, 16);
  139. if (8 * size > get_bits_left(&gb)) {
  140. avctx->release_buffer(avctx, &s->frame);
  141. s->frame.data[0] = NULL;
  142. return AVERROR_INVALIDDATA;
  143. }
  144. /* skip unchanged blocks, which have size 0 */
  145. if (size) {
  146. /* decompress block */
  147. int ret = inflateReset(&s->zstream);
  148. if (ret != Z_OK) {
  149. av_log(avctx, AV_LOG_ERROR,
  150. "error in decompression (reset) of block %dx%d\n", i, j);
  151. /* return -1; */
  152. }
  153. s->zstream.next_in = avpkt->data + get_bits_count(&gb) / 8;
  154. s->zstream.avail_in = size;
  155. s->zstream.next_out = s->tmpblock;
  156. s->zstream.avail_out = s->block_size * 3;
  157. ret = inflate(&s->zstream, Z_FINISH);
  158. if (ret == Z_DATA_ERROR) {
  159. av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
  160. inflateSync(&s->zstream);
  161. ret = inflate(&s->zstream, Z_FINISH);
  162. }
  163. if (ret != Z_OK && ret != Z_STREAM_END) {
  164. av_log(avctx, AV_LOG_ERROR,
  165. "error in decompression of block %dx%d: %d\n", i, j, ret);
  166. /* return -1; */
  167. }
  168. copy_region(s->tmpblock, s->frame.data[0],
  169. s->image_height - (hp + hs + 1),
  170. wp, hs, ws, s->frame.linesize[0]);
  171. skip_bits_long(&gb, 8 * size); /* skip the consumed bits */
  172. }
  173. }
  174. }
  175. *data_size = sizeof(AVFrame);
  176. *(AVFrame*)data = s->frame;
  177. if ((get_bits_count(&gb) / 8) != buf_size)
  178. av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
  179. buf_size, (get_bits_count(&gb) / 8));
  180. /* report that the buffer was completely consumed */
  181. return buf_size;
  182. }
  183. static av_cold int flashsv_decode_end(AVCodecContext *avctx)
  184. {
  185. FlashSVContext *s = avctx->priv_data;
  186. inflateEnd(&s->zstream);
  187. /* release the frame if needed */
  188. if (s->frame.data[0])
  189. avctx->release_buffer(avctx, &s->frame);
  190. /* free the tmpblock */
  191. av_free(s->tmpblock);
  192. return 0;
  193. }
  194. AVCodec ff_flashsv_decoder = {
  195. .name = "flashsv",
  196. .type = AVMEDIA_TYPE_VIDEO,
  197. .id = CODEC_ID_FLASHSV,
  198. .priv_data_size = sizeof(FlashSVContext),
  199. .init = flashsv_decode_init,
  200. .close = flashsv_decode_end,
  201. .decode = flashsv_decode_frame,
  202. .capabilities = CODEC_CAP_DR1,
  203. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
  204. .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),
  205. };