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.

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