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.

296 lines
9.1KB

  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 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. /* 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. int image_width, image_height;
  54. int block_width, block_height;
  55. uint8_t *tmpblock;
  56. uint8_t *encbuffer;
  57. int block_size;
  58. z_stream zstream;
  59. int last_key_frame;
  60. } FlashSVContext;
  61. static int copy_region_enc(uint8_t *sptr, uint8_t *dptr, int dx, int dy,
  62. int h, int w, int stride, uint8_t *pfptr)
  63. {
  64. int i, j;
  65. uint8_t *nsptr;
  66. uint8_t *npfptr;
  67. int diff = 0;
  68. for (i = dx + h; i > dx; i--) {
  69. nsptr = sptr + i * stride + dy * 3;
  70. npfptr = pfptr + i * stride + dy * 3;
  71. for (j = 0; j < w * 3; j++) {
  72. diff |= npfptr[j] ^ nsptr[j];
  73. dptr[j] = nsptr[j];
  74. }
  75. dptr += w * 3;
  76. }
  77. if (diff)
  78. return 1;
  79. return 0;
  80. }
  81. static av_cold int flashsv_encode_end(AVCodecContext *avctx)
  82. {
  83. FlashSVContext *s = avctx->priv_data;
  84. deflateEnd(&s->zstream);
  85. av_free(s->encbuffer);
  86. av_free(s->previous_frame);
  87. av_free(s->tmpblock);
  88. return 0;
  89. }
  90. static av_cold int flashsv_encode_init(AVCodecContext *avctx)
  91. {
  92. FlashSVContext *s = avctx->priv_data;
  93. s->avctx = avctx;
  94. if (avctx->width > 4095 || avctx->height > 4095) {
  95. av_log(avctx, AV_LOG_ERROR,
  96. "Input dimensions too large, input must be max 4096x4096 !\n");
  97. return AVERROR_INVALIDDATA;
  98. }
  99. // Needed if zlib unused or init aborted before deflateInit
  100. memset(&s->zstream, 0, sizeof(z_stream));
  101. s->last_key_frame = 0;
  102. s->image_width = avctx->width;
  103. s->image_height = avctx->height;
  104. s->tmpblock = av_mallocz(3 * 256 * 256);
  105. s->encbuffer = av_mallocz(s->image_width * s->image_height * 3);
  106. if (!s->tmpblock || !s->encbuffer) {
  107. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  108. return AVERROR(ENOMEM);
  109. }
  110. return 0;
  111. }
  112. static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
  113. int buf_size, int block_width, int block_height,
  114. uint8_t *previous_frame, int *I_frame)
  115. {
  116. PutBitContext pb;
  117. int h_blocks, v_blocks, h_part, v_part, i, j;
  118. int buf_pos, res;
  119. int pred_blocks = 0;
  120. init_put_bits(&pb, buf, buf_size * 8);
  121. put_bits(&pb, 4, block_width / 16 - 1);
  122. put_bits(&pb, 12, s->image_width);
  123. put_bits(&pb, 4, block_height / 16 - 1);
  124. put_bits(&pb, 12, s->image_height);
  125. flush_put_bits(&pb);
  126. buf_pos = 4;
  127. h_blocks = s->image_width / block_width;
  128. h_part = s->image_width % block_width;
  129. v_blocks = s->image_height / block_height;
  130. v_part = s->image_height % block_height;
  131. /* loop over all block columns */
  132. for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
  133. int y_pos = j * block_height; // vertical position in frame
  134. int cur_blk_height = (j < v_blocks) ? block_height : v_part;
  135. /* loop over all block rows */
  136. for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
  137. int x_pos = i * block_width; // horizontal position in frame
  138. int cur_blk_width = (i < h_blocks) ? block_width : h_part;
  139. int ret = Z_OK;
  140. uint8_t *ptr = buf + buf_pos;
  141. /* copy the block to the temp buffer before compression
  142. * (if it differs from the previous frame's block) */
  143. res = copy_region_enc(p->data[0], s->tmpblock,
  144. s->image_height - (y_pos + cur_blk_height + 1),
  145. x_pos, cur_blk_height, cur_blk_width,
  146. p->linesize[0], previous_frame);
  147. if (res || *I_frame) {
  148. unsigned long zsize = 3 * block_width * block_height;
  149. ret = compress2(ptr + 2, &zsize, s->tmpblock,
  150. 3 * cur_blk_width * cur_blk_height, 9);
  151. //ret = deflateReset(&s->zstream);
  152. if (ret != Z_OK)
  153. av_log(s->avctx, AV_LOG_ERROR,
  154. "error while compressing block %dx%d\n", i, j);
  155. bytestream_put_be16(&ptr, zsize);
  156. buf_pos += zsize + 2;
  157. ff_dlog(s->avctx, "buf_pos = %d\n", buf_pos);
  158. } else {
  159. pred_blocks++;
  160. bytestream_put_be16(&ptr, 0);
  161. buf_pos += 2;
  162. }
  163. }
  164. }
  165. if (pred_blocks)
  166. *I_frame = 0;
  167. else
  168. *I_frame = 1;
  169. return buf_pos;
  170. }
  171. static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  172. const AVFrame *pict, int *got_packet)
  173. {
  174. FlashSVContext * const s = avctx->priv_data;
  175. const AVFrame * const p = pict;
  176. uint8_t *pfptr;
  177. int res;
  178. int I_frame = 0;
  179. int opt_w = 4, opt_h = 4;
  180. /* First frame needs to be a keyframe */
  181. if (avctx->frame_number == 0) {
  182. s->previous_frame = av_mallocz(FFABS(p->linesize[0]) * s->image_height);
  183. if (!s->previous_frame) {
  184. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  185. return AVERROR(ENOMEM);
  186. }
  187. I_frame = 1;
  188. }
  189. if (p->linesize[0] < 0)
  190. pfptr = s->previous_frame - (s->image_height - 1) * p->linesize[0];
  191. else
  192. pfptr = s->previous_frame;
  193. /* Check the placement of keyframes */
  194. if (avctx->gop_size > 0 &&
  195. avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
  196. I_frame = 1;
  197. }
  198. if ((res = ff_alloc_packet(pkt, s->image_width * s->image_height * 3)) < 0) {
  199. //Conservative upper bound check for compressed data
  200. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n",
  201. s->image_width * s->image_height * 3);
  202. return res;
  203. }
  204. pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
  205. pfptr, &I_frame);
  206. //save the current frame
  207. if (p->linesize[0] > 0)
  208. memcpy(s->previous_frame, p->data[0], s->image_height * p->linesize[0]);
  209. else
  210. memcpy(s->previous_frame,
  211. p->data[0] + p->linesize[0] * (s->image_height - 1),
  212. s->image_height * FFABS(p->linesize[0]));
  213. //mark the frame type so the muxer can mux it correctly
  214. if (I_frame) {
  215. #if FF_API_CODED_FRAME
  216. FF_DISABLE_DEPRECATION_WARNINGS
  217. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  218. avctx->coded_frame->key_frame = 1;
  219. FF_ENABLE_DEPRECATION_WARNINGS
  220. #endif
  221. s->last_key_frame = avctx->frame_number;
  222. ff_dlog(avctx, "Inserting keyframe at frame %d\n", avctx->frame_number);
  223. } else {
  224. #if FF_API_CODED_FRAME
  225. FF_DISABLE_DEPRECATION_WARNINGS
  226. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
  227. avctx->coded_frame->key_frame = 0;
  228. FF_ENABLE_DEPRECATION_WARNINGS
  229. #endif
  230. }
  231. if (I_frame)
  232. pkt->flags |= AV_PKT_FLAG_KEY;
  233. *got_packet = 1;
  234. return 0;
  235. }
  236. AVCodec ff_flashsv_encoder = {
  237. .name = "flashsv",
  238. .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
  239. .type = AVMEDIA_TYPE_VIDEO,
  240. .id = AV_CODEC_ID_FLASHSV,
  241. .priv_data_size = sizeof(FlashSVContext),
  242. .init = flashsv_encode_init,
  243. .encode2 = flashsv_encode_frame,
  244. .close = flashsv_encode_end,
  245. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
  246. };