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.

240 lines
8.4KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. avcodec_get_frame_defaults(&s->frame);
  61. s->frame.data[0] = NULL;
  62. return 0;
  63. }
  64. static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
  65. int *data_size, AVPacket *avpkt)
  66. {
  67. int buf_size = avpkt->size;
  68. FlashSVContext *s = avctx->priv_data;
  69. int h_blocks, v_blocks, h_part, v_part, i, j;
  70. GetBitContext gb;
  71. /* no supplementary picture */
  72. if (buf_size == 0)
  73. return 0;
  74. if (buf_size < 4)
  75. return -1;
  76. init_get_bits(&gb, avpkt->data, buf_size * 8);
  77. /* start to parse the bitstream */
  78. s->block_width = 16 * (get_bits(&gb, 4) + 1);
  79. s->image_width = get_bits(&gb, 12);
  80. s->block_height = 16 * (get_bits(&gb, 4) + 1);
  81. s->image_height = get_bits(&gb, 12);
  82. /* calculate number of blocks and size of border (partial) blocks */
  83. h_blocks = s->image_width / s->block_width;
  84. h_part = s->image_width % s->block_width;
  85. v_blocks = s->image_height / s->block_height;
  86. v_part = s->image_height % s->block_height;
  87. /* the block size could change between frames, make sure the buffer
  88. * is large enough, if not, get a larger one */
  89. if (s->block_size < s->block_width * s->block_height) {
  90. av_free(s->tmpblock);
  91. if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
  92. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  93. return AVERROR(ENOMEM);
  94. }
  95. }
  96. s->block_size = s->block_width * s->block_height;
  97. /* initialize the image size once */
  98. if (avctx->width == 0 && avctx->height == 0) {
  99. avctx->width = s->image_width;
  100. avctx->height = s->image_height;
  101. }
  102. /* check for changes of image width and image height */
  103. if (avctx->width != s->image_width || avctx->height != s->image_height) {
  104. av_log(avctx, AV_LOG_ERROR,
  105. "Frame width or height differs from first frames!\n");
  106. av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",
  107. avctx->height, avctx->width, s->image_height, s->image_width);
  108. return AVERROR_INVALIDDATA;
  109. }
  110. av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
  111. s->image_width, s->image_height, s->block_width, s->block_height,
  112. h_blocks, v_blocks, h_part, v_part);
  113. s->frame.reference = 3;
  114. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
  115. FF_BUFFER_HINTS_PRESERVE |
  116. FF_BUFFER_HINTS_REUSABLE;
  117. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  118. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  119. return -1;
  120. }
  121. /* loop over all block columns */
  122. for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
  123. int y_pos = j * s->block_height; // vertical position in frame
  124. int cur_blk_height = (j < v_blocks) ? s->block_height : v_part;
  125. /* loop over all block rows */
  126. for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
  127. int x_pos = i * s->block_width; // horizontal position in frame
  128. int cur_blk_width = (i < h_blocks) ? s->block_width : h_part;
  129. /* get the size of the compressed zlib chunk */
  130. int size = get_bits(&gb, 16);
  131. if (8 * size > get_bits_left(&gb)) {
  132. avctx->release_buffer(avctx, &s->frame);
  133. s->frame.data[0] = NULL;
  134. return AVERROR_INVALIDDATA;
  135. }
  136. /* skip unchanged blocks, which have size 0 */
  137. if (size) {
  138. /* decompress block */
  139. uint8_t *line = s->tmpblock;
  140. int k;
  141. int ret = inflateReset(&s->zstream);
  142. if (ret != Z_OK) {
  143. av_log(avctx, AV_LOG_ERROR,
  144. "error in decompression (reset) of block %dx%d\n", i, j);
  145. /* return -1; */
  146. }
  147. s->zstream.next_in = avpkt->data + get_bits_count(&gb) / 8;
  148. s->zstream.avail_in = size;
  149. s->zstream.next_out = s->tmpblock;
  150. s->zstream.avail_out = s->block_size * 3;
  151. ret = inflate(&s->zstream, Z_FINISH);
  152. if (ret == Z_DATA_ERROR) {
  153. av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
  154. inflateSync(&s->zstream);
  155. ret = inflate(&s->zstream, Z_FINISH);
  156. }
  157. if (ret != Z_OK && ret != Z_STREAM_END) {
  158. av_log(avctx, AV_LOG_ERROR,
  159. "error in decompression of block %dx%d: %d\n", i, j, ret);
  160. /* return -1; */
  161. }
  162. /* Flash Screen Video stores the image upside down, so copy
  163. * lines to destination in reverse order. */
  164. for (k = 1; k <= cur_blk_height; k++) {
  165. memcpy(s->frame.data[0] + x_pos * 3 +
  166. (s->image_height - y_pos - k) * s->frame.linesize[0],
  167. line, cur_blk_width * 3);
  168. /* advance source pointer to next line */
  169. line += cur_blk_width * 3;
  170. }
  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. };