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.

347 lines
10KB

  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 flashsvenc.c
  25. * Flash Screen Video encoder
  26. * @author Alex Beregszaszi
  27. * @author Benjamin Larsson
  28. */
  29. /* Bitstream description
  30. * The picture is divided into blocks that are zlib-compressed.
  31. *
  32. * The decoder is fed complete frames, the frameheader contains:
  33. * 4bits of block width
  34. * 12bits of frame width
  35. * 4bits of block height
  36. * 12bits of frame height
  37. *
  38. * Directly after the header are the compressed blocks. The blocks
  39. * have their compressed size represented with 16bits in the beginig.
  40. * If the size = 0 then the block is unchanged from the previous frame.
  41. * All blocks are decompressed until the buffer is consumed.
  42. *
  43. * Encoding ideas, a basic encoder would just use a fixed block size.
  44. * Block sizes can be multipels of 16, from 16 to 256. The blocks don't
  45. * have to be quadratic. A brute force search with a set of different
  46. * block sizes should give a better result than to just use a fixed size.
  47. */
  48. /* TODO:
  49. * Don't reencode the frame in brute force mode if the frame is a dupe. Speed up.
  50. * Make the difference check faster.
  51. */
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <zlib.h>
  55. #include "common.h"
  56. #include "avcodec.h"
  57. #include "bitstream.h"
  58. #include "bytestream.h"
  59. typedef struct FlashSVContext {
  60. AVCodecContext *avctx;
  61. uint8_t *previous_frame;
  62. AVFrame frame;
  63. int image_width, image_height;
  64. int block_width, block_height;
  65. uint8_t* tmpblock;
  66. uint8_t* encbuffer;
  67. int block_size;
  68. z_stream zstream;
  69. int last_key_frame;
  70. } FlashSVContext;
  71. static int copy_region_enc(uint8_t *sptr, uint8_t *dptr,
  72. int dx, int dy, int h, int w, int stride, uint8_t *pfptr) {
  73. int i,j;
  74. uint8_t *nsptr;
  75. uint8_t *npfptr;
  76. int diff = 0;
  77. for (i = dx+h; i > dx; i--) {
  78. nsptr = sptr+(i*stride)+dy*3;
  79. npfptr = pfptr+(i*stride)+dy*3;
  80. for (j=0 ; j<w*3 ; j++) {
  81. diff |=npfptr[j]^nsptr[j];
  82. dptr[j] = nsptr[j];
  83. }
  84. dptr += w*3;
  85. }
  86. if (diff)
  87. return 1;
  88. return 0;
  89. }
  90. static 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, "Input dimensions too large, input must be max 4096x4096 !\n");
  96. return -1;
  97. }
  98. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  99. return -1;
  100. }
  101. // Needed if zlib unused or init aborted before deflateInit
  102. memset(&(s->zstream), 0, sizeof(z_stream));
  103. /*
  104. s->zstream.zalloc = NULL; //av_malloc;
  105. s->zstream.zfree = NULL; //av_free;
  106. s->zstream.opaque = NULL;
  107. zret = deflateInit(&(s->zstream), 9);
  108. if (zret != Z_OK) {
  109. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  110. return -1;
  111. }
  112. */
  113. s->last_key_frame=0;
  114. s->image_width = avctx->width;
  115. s->image_height = avctx->height;
  116. s->tmpblock = av_mallocz(3*256*256);
  117. s->encbuffer = av_mallocz(s->image_width*s->image_height*3);
  118. if (!s->tmpblock || !s->encbuffer) {
  119. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. static int encode_bitstream(FlashSVContext *s, AVFrame *p, uint8_t *buf, int buf_size,
  125. int block_width, int block_height, uint8_t *previous_frame, int* I_frame) {
  126. PutBitContext pb;
  127. int h_blocks, v_blocks, h_part, v_part, i, j;
  128. int buf_pos, res;
  129. int pred_blocks = 0;
  130. init_put_bits(&pb, buf, buf_size*8);
  131. put_bits(&pb, 4, (block_width/16)-1);
  132. put_bits(&pb, 12, s->image_width);
  133. put_bits(&pb, 4, (block_height/16)-1);
  134. put_bits(&pb, 12, s->image_height);
  135. flush_put_bits(&pb);
  136. buf_pos=4;
  137. h_blocks = s->image_width / block_width;
  138. h_part = s->image_width % block_width;
  139. v_blocks = s->image_height / block_height;
  140. v_part = s->image_height % block_height;
  141. /* loop over all block columns */
  142. for (j = 0; j < v_blocks + (v_part?1:0); j++)
  143. {
  144. int hp = j*block_height; // horiz position in frame
  145. int hs = (j<v_blocks)?block_height:v_part; // size of block
  146. /* loop over all block rows */
  147. for (i = 0; i < h_blocks + (h_part?1:0); i++)
  148. {
  149. int wp = i*block_width; // vert position in frame
  150. int ws = (i<h_blocks)?block_width:h_part; // size of block
  151. int ret=Z_OK;
  152. uint8_t *ptr;
  153. ptr = buf+buf_pos;
  154. //copy the block to the temp buffer before compression (if it differs from the previous frame's block)
  155. res = copy_region_enc(p->data[0], s->tmpblock, s->image_height-(hp+hs+1), wp, hs, ws, p->linesize[0], previous_frame);
  156. if (res || *I_frame) {
  157. unsigned long zsize;
  158. zsize = 3*block_width*block_height;
  159. ret = compress2(ptr+2, &zsize, s->tmpblock, 3*ws*hs, 9);
  160. //ret = deflateReset(&(s->zstream));
  161. if (ret != Z_OK)
  162. av_log(s->avctx, AV_LOG_ERROR, "error while compressing block %dx%d\n", i, j);
  163. /*
  164. s->zstream.next_in = s->tmpblock;
  165. s->zstream.avail_in = 3*ws*hs;
  166. s->zstream.total_in = 0;
  167. s->zstream.next_out = ptr+2;
  168. s->zstream.avail_out = buf_size-buf_pos-2;
  169. s->zstream.total_out = 0;
  170. ret = deflate(&(s->zstream), Z_FINISH);
  171. if ((ret != Z_OK) && (ret != Z_STREAM_END))
  172. av_log(s->avctx, AV_LOG_ERROR, "error while compressing block %dx%d\n", i, j);
  173. size = s->zstream.total_out;
  174. //av_log(avctx, AV_LOG_INFO, "compressed blocks: %d\n", size);
  175. */
  176. bytestream_put_be16(&ptr,(unsigned int)zsize);
  177. buf_pos += zsize+2;
  178. //av_log(avctx, AV_LOG_ERROR, "buf_pos = %d\n", buf_pos);
  179. } else {
  180. pred_blocks++;
  181. bytestream_put_be16(&ptr,0);
  182. buf_pos += 2;
  183. }
  184. }
  185. }
  186. if (pred_blocks)
  187. *I_frame = 0;
  188. else
  189. *I_frame = 1;
  190. return buf_pos;
  191. }
  192. static int flashsv_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
  193. {
  194. FlashSVContext * const s = avctx->priv_data;
  195. AVFrame *pict = data;
  196. AVFrame * const p = &s->frame;
  197. int res;
  198. int I_frame = 0;
  199. int opt_w, opt_h;
  200. *p = *pict;
  201. /* First frame needs to be a keyframe */
  202. if (avctx->frame_number == 0) {
  203. s->previous_frame = av_mallocz(p->linesize[0]*s->image_height);
  204. if (!s->previous_frame) {
  205. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  206. return -1;
  207. }
  208. I_frame = 1;
  209. }
  210. /* Check the placement of keyframes */
  211. if (avctx->gop_size > 0) {
  212. if (avctx->frame_number >= s->last_key_frame + avctx->gop_size) {
  213. I_frame = 1;
  214. }
  215. }
  216. #if 0
  217. int w, h;
  218. int optim_sizes[16][16];
  219. int smallest_size;
  220. //Try all possible combinations and store the encoded frame sizes
  221. for (w=1 ; w<17 ; w++) {
  222. for (h=1 ; h<17 ; h++) {
  223. optim_sizes[w-1][h-1] = encode_bitstream(s, p, s->encbuffer, s->image_width*s->image_height*4, w*16, h*16, s->previous_frame);
  224. //av_log(avctx, AV_LOG_ERROR, "[%d][%d]size = %d\n",w,h,optim_sizes[w-1][h-1]);
  225. }
  226. }
  227. //Search for the smallest framesize and encode the frame with those parameters
  228. smallest_size=optim_sizes[0][0];
  229. opt_w = 0;
  230. opt_h = 0;
  231. for (w=0 ; w<16 ; w++) {
  232. for (h=0 ; h<16 ; h++) {
  233. if (optim_sizes[w][h] < smallest_size) {
  234. smallest_size = optim_sizes[w][h];
  235. opt_w = w;
  236. opt_h = h;
  237. }
  238. }
  239. }
  240. res = encode_bitstream(s, p, buf, buf_size, (opt_w+1)*16, (opt_h+1)*16, s->previous_frame);
  241. av_log(avctx, AV_LOG_ERROR, "[%d][%d]optimal size = %d, res = %d|\n", opt_w, opt_h, smallest_size, res);
  242. if (buf_size < res)
  243. av_log(avctx, AV_LOG_ERROR, "buf_size %d < res %d\n", buf_size, res);
  244. #else
  245. opt_w=1;
  246. opt_h=1;
  247. if (buf_size < s->image_width*s->image_height*3) {
  248. //Conservative upper bound check for compressed data
  249. av_log(avctx, AV_LOG_ERROR, "buf_size %d < %d\n", buf_size, s->image_width*s->image_height*3);
  250. return -1;
  251. }
  252. res = encode_bitstream(s, p, buf, buf_size, opt_w*16, opt_h*16, s->previous_frame, &I_frame);
  253. #endif
  254. //save the current frame
  255. memcpy(s->previous_frame, p->data[0], s->image_height*p->linesize[0]);
  256. //mark the frame type so the muxer can mux it correctly
  257. if (I_frame) {
  258. p->pict_type = FF_I_TYPE;
  259. p->key_frame = 1;
  260. s->last_key_frame = avctx->frame_number;
  261. av_log(avctx, AV_LOG_DEBUG, "Inserting key frame at frame %d\n",avctx->frame_number);
  262. } else {
  263. p->pict_type = FF_P_TYPE;
  264. p->key_frame = 0;
  265. }
  266. avctx->coded_frame = p;
  267. return res;
  268. }
  269. static int flashsv_encode_end(AVCodecContext *avctx)
  270. {
  271. FlashSVContext *s = avctx->priv_data;
  272. deflateEnd(&(s->zstream));
  273. av_free(s->encbuffer);
  274. av_free(s->previous_frame);
  275. av_free(s->tmpblock);
  276. return 0;
  277. }
  278. AVCodec flashsv_encoder = {
  279. "flashsv",
  280. CODEC_TYPE_VIDEO,
  281. CODEC_ID_FLASHSV,
  282. sizeof(FlashSVContext),
  283. flashsv_encode_init,
  284. flashsv_encode_frame,
  285. flashsv_encode_end,
  286. .pix_fmts = (enum PixelFormat[]){PIX_FMT_BGR24, -1},
  287. };