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.

239 lines
8.3KB

  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 av_cold int flashsv_decode_init(AVCodecContext *avctx)
  47. {
  48. FlashSVContext *s = avctx->priv_data;
  49. int zret; // Zlib return code
  50. s->avctx = avctx;
  51. s->zstream.zalloc = Z_NULL;
  52. s->zstream.zfree = Z_NULL;
  53. s->zstream.opaque = Z_NULL;
  54. zret = inflateInit(&s->zstream);
  55. if (zret != Z_OK) {
  56. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  57. return 1;
  58. }
  59. avctx->pix_fmt = PIX_FMT_BGR24;
  60. s->frame.data[0] = NULL;
  61. return 0;
  62. }
  63. static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
  64. int *data_size, AVPacket *avpkt)
  65. {
  66. int buf_size = avpkt->size;
  67. FlashSVContext *s = avctx->priv_data;
  68. int h_blocks, v_blocks, h_part, v_part, i, j;
  69. GetBitContext gb;
  70. /* no supplementary picture */
  71. if (buf_size == 0)
  72. return 0;
  73. if (buf_size < 4)
  74. return -1;
  75. init_get_bits(&gb, avpkt->data, buf_size * 8);
  76. /* start to parse the bitstream */
  77. s->block_width = 16 * (get_bits(&gb, 4) + 1);
  78. s->image_width = get_bits(&gb, 12);
  79. s->block_height = 16 * (get_bits(&gb, 4) + 1);
  80. s->image_height = get_bits(&gb, 12);
  81. /* calculate number of blocks and size of border (partial) blocks */
  82. h_blocks = s->image_width / s->block_width;
  83. h_part = s->image_width % s->block_width;
  84. v_blocks = s->image_height / s->block_height;
  85. v_part = s->image_height % s->block_height;
  86. /* the block size could change between frames, make sure the buffer
  87. * is large enough, if not, get a larger one */
  88. if (s->block_size < s->block_width * s->block_height) {
  89. av_free(s->tmpblock);
  90. if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
  91. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  92. return AVERROR(ENOMEM);
  93. }
  94. }
  95. s->block_size = s->block_width * s->block_height;
  96. /* initialize the image size once */
  97. if (avctx->width == 0 && avctx->height == 0) {
  98. avctx->width = s->image_width;
  99. avctx->height = s->image_height;
  100. }
  101. /* check for changes of image width and image height */
  102. if (avctx->width != s->image_width || avctx->height != s->image_height) {
  103. av_log(avctx, AV_LOG_ERROR,
  104. "Frame width or height differs from first frames!\n");
  105. av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",
  106. avctx->height, avctx->width, s->image_height, s->image_width);
  107. return AVERROR_INVALIDDATA;
  108. }
  109. av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
  110. s->image_width, s->image_height, s->block_width, s->block_height,
  111. h_blocks, v_blocks, h_part, v_part);
  112. s->frame.reference = 3;
  113. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
  114. FF_BUFFER_HINTS_PRESERVE |
  115. FF_BUFFER_HINTS_REUSABLE;
  116. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  117. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  118. return -1;
  119. }
  120. /* loop over all block columns */
  121. for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
  122. int y_pos = j * s->block_height; // vertical position in frame
  123. int cur_blk_height = (j < v_blocks) ? s->block_height : v_part;
  124. /* loop over all block rows */
  125. for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
  126. int x_pos = i * s->block_width; // horizontal position in frame
  127. int cur_blk_width = (i < h_blocks) ? s->block_width : h_part;
  128. /* get the size of the compressed zlib chunk */
  129. int size = get_bits(&gb, 16);
  130. if (8 * size > get_bits_left(&gb)) {
  131. avctx->release_buffer(avctx, &s->frame);
  132. s->frame.data[0] = NULL;
  133. return AVERROR_INVALIDDATA;
  134. }
  135. /* skip unchanged blocks, which have size 0 */
  136. if (size) {
  137. /* decompress block */
  138. uint8_t *line = s->tmpblock;
  139. int k;
  140. int ret = inflateReset(&s->zstream);
  141. if (ret != Z_OK) {
  142. av_log(avctx, AV_LOG_ERROR,
  143. "error in decompression (reset) of block %dx%d\n", i, j);
  144. /* return -1; */
  145. }
  146. s->zstream.next_in = avpkt->data + get_bits_count(&gb) / 8;
  147. s->zstream.avail_in = size;
  148. s->zstream.next_out = s->tmpblock;
  149. s->zstream.avail_out = s->block_size * 3;
  150. ret = inflate(&s->zstream, Z_FINISH);
  151. if (ret == Z_DATA_ERROR) {
  152. av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
  153. inflateSync(&s->zstream);
  154. ret = inflate(&s->zstream, Z_FINISH);
  155. }
  156. if (ret != Z_OK && ret != Z_STREAM_END) {
  157. av_log(avctx, AV_LOG_ERROR,
  158. "error in decompression of block %dx%d: %d\n", i, j, ret);
  159. /* return -1; */
  160. }
  161. /* Flash Screen Video stores the image upside down, so copy
  162. * lines to destination in reverse order. */
  163. for (k = 1; k <= cur_blk_height; k++) {
  164. memcpy(s->frame.data[0] + x_pos * 3 +
  165. (s->image_height - y_pos - k) * s->frame.linesize[0],
  166. line, cur_blk_width * 3);
  167. /* advance source pointer to next line */
  168. line += cur_blk_width * 3;
  169. }
  170. skip_bits_long(&gb, 8 * size); /* skip the consumed bits */
  171. }
  172. }
  173. }
  174. *data_size = sizeof(AVFrame);
  175. *(AVFrame*)data = s->frame;
  176. if ((get_bits_count(&gb) / 8) != buf_size)
  177. av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
  178. buf_size, (get_bits_count(&gb) / 8));
  179. /* report that the buffer was completely consumed */
  180. return buf_size;
  181. }
  182. static av_cold int flashsv_decode_end(AVCodecContext *avctx)
  183. {
  184. FlashSVContext *s = avctx->priv_data;
  185. inflateEnd(&s->zstream);
  186. /* release the frame if needed */
  187. if (s->frame.data[0])
  188. avctx->release_buffer(avctx, &s->frame);
  189. /* free the tmpblock */
  190. av_free(s->tmpblock);
  191. return 0;
  192. }
  193. AVCodec ff_flashsv_decoder = {
  194. .name = "flashsv",
  195. .type = AVMEDIA_TYPE_VIDEO,
  196. .id = CODEC_ID_FLASHSV,
  197. .priv_data_size = sizeof(FlashSVContext),
  198. .init = flashsv_decode_init,
  199. .close = flashsv_decode_end,
  200. .decode = flashsv_decode_frame,
  201. .capabilities = CODEC_CAP_DR1,
  202. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
  203. .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),
  204. };