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.

289 lines
8.8KB

  1. /*
  2. * Flash Screen Video encoder
  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. /* Encoding development sponsored by http://fh-campuswien.ac.at */
  23. /**
  24. * @file
  25. * Flash Screen Video encoder
  26. * @author Alex Beregszaszi
  27. * @author Benjamin Larsson
  28. *
  29. * A description of the bitstream format for Flash Screen Video version 1/2
  30. * is part of the SWF File Format Specification (version 10), which can be
  31. * downloaded from http://www.adobe.com/devnet/swf.html.
  32. */
  33. /*
  34. * Encoding ideas: A basic encoder would just use a fixed block size.
  35. * Block sizes can be multiples of 16, from 16 to 256. The blocks don't
  36. * have to be quadratic. A brute force search with a set of different
  37. * block sizes should give a better result than to just use a fixed size.
  38. *
  39. * TODO:
  40. * Don't reencode the frame in brute force mode if the frame is a dupe.
  41. * Speed up. Make the difference check faster.
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <zlib.h>
  46. #include "avcodec.h"
  47. #include "internal.h"
  48. #include "put_bits.h"
  49. #include "bytestream.h"
  50. typedef struct FlashSVContext {
  51. AVCodecContext *avctx;
  52. uint8_t *previous_frame;
  53. AVFrame frame;
  54. int image_width, image_height;
  55. int block_width, block_height;
  56. uint8_t *tmpblock;
  57. uint8_t *encbuffer;
  58. int block_size;
  59. z_stream zstream;
  60. int last_key_frame;
  61. } FlashSVContext;
  62. static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
  63. int h, int w, int stride, uint8_t *pfptr)
  64. {
  65. int i, j;
  66. uint8_t *nsptr;
  67. uint8_t *npfptr;
  68. int diff = 0;
  69. for (i = dx + h; i > dx; i--) {
  70. nsptr = sptr + i * stride + dy * 3;
  71. npfptr = pfptr + i * stride + dy * 3;
  72. for (j = 0; j < w * 3; j++) {
  73. diff |= npfptr[j] ^ nsptr[j];
  74. dptr[j] = nsptr[j];
  75. }
  76. dptr += w * 3;
  77. }
  78. if (diff)
  79. return 1;
  80. return 0;
  81. }
  82. static av_cold int flashsv_encode_init(AVCodecContext *avctx)
  83. {
  84. FlashSVContext *s = avctx->priv_data;
  85. s->avctx = avctx;
  86. if (avctx->width > 4095 || avctx->height > 4095) {
  87. av_log(avctx, AV_LOG_ERROR,
  88. "Input dimensions too large, input must be max 4096x4096 !\n");
  89. return AVERROR_INVALIDDATA;
  90. }
  91. // Needed if zlib unused or init aborted before deflateInit
  92. memset(&s->zstream, 0, sizeof(z_stream));
  93. s->last_key_frame = 0;
  94. s->image_width = avctx->width;
  95. s->image_height = avctx->height;
  96. s->tmpblock = av_mallocz(3 * 256 * 256);
  97. s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
  98. if (!s->tmpblock || !s->encbuffer) {
  99. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  100. return AVERROR(ENOMEM);
  101. }
  102. return 0;
  103. }
  104. static int encode_bitstream(FlashSVContext *s, AVFrame *p, uint8_t *buf,
  105. int buf_size, int block_width, int block_height,
  106. uint8_t *previous_frame, int *I_frame)
  107. {
  108. PutBitContext pb;
  109. int h_blocks, v_blocks, h_part, v_part, i, j;
  110. int buf_pos, res;
  111. int pred_blocks = 0;
  112. init_put_bits(&pb, buf, buf_size * 8);
  113. put_bits(&pb, 4, block_width / 16 - 1);
  114. put_bits(&pb, 12, s->image_width);
  115. put_bits(&pb, 4, block_height / 16 - 1);
  116. put_bits(&pb, 12, s->image_height);
  117. flush_put_bits(&pb);
  118. buf_pos = 4;
  119. h_blocks = s->image_width / block_width;
  120. h_part = s->image_width % block_width;
  121. v_blocks = s->image_height / block_height;
  122. v_part = s->image_height % block_height;
  123. /* loop over all block columns */
  124. for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
  125. int y_pos = j * block_height; // vertical position in frame
  126. int cur_blk_height = (j < v_blocks) ? block_height : v_part;
  127. /* loop over all block rows */
  128. for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
  129. int x_pos = i * block_width; // horizontal position in frame
  130. int cur_blk_width = (i < h_blocks) ? block_width : h_part;
  131. int ret = Z_OK;
  132. uint8_t *ptr = buf + buf_pos;
  133. /* copy the block to the temp buffer before compression
  134. * (if it differs from the previous frame's block) */
  135. res = copy_region_enc(p->data[0], s->tmpblock,
  136. s->image_height - (y_pos + cur_blk_height + 1),
  137. x_pos, cur_blk_height, cur_blk_width,
  138. p->linesize[0], previous_frame);
  139. if (res || *I_frame) {
  140. unsigned long zsize = 3 * block_width * block_height;
  141. ret = compress2(ptr + 2, &zsize, s->tmpblock,
  142. 3 * cur_blk_width * cur_blk_height, 9);
  143. //ret = deflateReset(&s->zstream);
  144. if (ret != Z_OK)
  145. av_log(s->avctx, AV_LOG_ERROR,
  146. "error while compressing block %dx%d\n", i, j);
  147. bytestream_put_be16(&ptr, zsize);
  148. buf_pos += zsize + 2;
  149. av_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
  150. } else {
  151. pred_blocks++;
  152. bytestream_put_be16(&ptr, 0);
  153. buf_pos += 2;
  154. }
  155. }
  156. }
  157. if (pred_blocks)
  158. *I_frame = 0;
  159. else
  160. *I_frame = 1;
  161. return buf_pos;
  162. }
  163. static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  164. const AVFrame *pict, int *got_packet)
  165. {
  166. FlashSVContext * const s = avctx->priv_data;
  167. AVFrame * const p = &s->frame;
  168. uint8_t *pfptr;
  169. int res;
  170. int I_frame = 0;
  171. int opt_w = 4, opt_h = 4;
  172. *p = *pict;
  173. /* First frame needs to be a keyframe */
  174. if (avctx->frame_number == 0) {
  175. s->previous_frame = av_mallocz(FFABS(p->linesize[0]) * s->image_height);
  176. if (!s->previous_frame) {
  177. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  178. return AVERROR(ENOMEM);
  179. }
  180. I_frame = 1;
  181. }
  182. if (p->linesize[0] < 0)
  183. pfptr = s->previous_frame - (s->image_height - 1) * p->linesize[0];
  184. else
  185. pfptr = s->previous_frame;
  186. /* Check the placement of keyframes */
  187. if (avctx->gop_size > 0 &&
  188. avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
  189. I_frame = 1;
  190. }
  191. if ((res = ff_alloc_packet2(avctx, pkt, s->image_width * s->image_height * 3)) < 0)
  192. return res;
  193. pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
  194. pfptr, &I_frame);
  195. //save the current frame
  196. if (p->linesize[0] > 0)
  197. memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
  198. else
  199. memcpy(s->previous_frame,
  200. p->data[0] + p->linesize[0] * (s->image_height - 1),
  201. s->image_height * FFABS(p->linesize[0]));
  202. //mark the frame type so the muxer can mux it correctly
  203. if (I_frame) {
  204. p->pict_type = AV_PICTURE_TYPE_I;
  205. p->key_frame = 1;
  206. s->last_key_frame = avctx->frame_number;
  207. av_dlog(avctx, "Inserting keyframe at frame %d\n", avctx->frame_number);
  208. } else {
  209. p->pict_type = AV_PICTURE_TYPE_P;
  210. p->key_frame = 0;
  211. }
  212. avctx->coded_frame = p;
  213. if (p->key_frame)
  214. pkt->flags |= AV_PKT_FLAG_KEY;
  215. *got_packet = 1;
  216. return 0;
  217. }
  218. static av_cold int flashsv_encode_end(AVCodecContext *avctx)
  219. {
  220. FlashSVContext *s = avctx->priv_data;
  221. deflateEnd(&s->zstream);
  222. av_free(s->encbuffer);
  223. av_free(s->previous_frame);
  224. av_free(s->tmpblock);
  225. return 0;
  226. }
  227. AVCodec ff_flashsv_encoder = {
  228. .name = "flashsv",
  229. .type = AVMEDIA_TYPE_VIDEO,
  230. .id = AV_CODEC_ID_FLASHSV,
  231. .priv_data_size = sizeof(FlashSVContext),
  232. .init = flashsv_encode_init,
  233. .encode2 = flashsv_encode_frame,
  234. .close = flashsv_encode_end,
  235. .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_BGR24, PIX_FMT_NONE },
  236. .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
  237. };