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.

327 lines
11KB

  1. /*
  2. * Quicktime Animation (RLE) Video Encoder
  3. * Copyright (C) 2007 Clemens Fruhwirth
  4. * Copyright (C) 2007 Alexis Ballier
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * This file is based on flashsvenc.c
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License, version 2.1, as published by the Free Software Foundation
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. /** Maximum RLE code for bulk copy */
  26. #define MAX_RLE_BULK 127
  27. /** Maximum RLE code for repeat */
  28. #define MAX_RLE_REPEAT 128
  29. /** Maximum RLE code for skip */
  30. #define MAX_RLE_SKIP 254
  31. typedef struct QtrleEncContext {
  32. AVCodecContext *avctx;
  33. AVFrame frame;
  34. int pixel_size;
  35. AVPicture previous_frame;
  36. unsigned int max_buf_size;
  37. /**
  38. * This array will contain at ith position the value of the best RLE code
  39. * if the line started at pixel i
  40. * There can be 3 values :
  41. * skip (0) : skip as much as possible pixels because they are equal to the
  42. * previous frame ones
  43. * repeat (<-1) : repeat that pixel -rle_code times, still as much as
  44. * possible
  45. * copy (>0) : copy the raw next rle_code pixels */
  46. signed char *rlecode_table;
  47. /**
  48. * This array will contain the length of the best rle encoding of the line
  49. * starting at ith pixel */
  50. int *length_table;
  51. /**
  52. * Will contain at ith position the number of consecutive pixels equal to the previous
  53. * frame starting from pixel i */
  54. uint8_t* skip_table;
  55. } QtrleEncContext;
  56. static av_cold int qtrle_encode_init(AVCodecContext *avctx)
  57. {
  58. QtrleEncContext *s = avctx->priv_data;
  59. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  60. return -1;
  61. }
  62. s->avctx=avctx;
  63. switch (avctx->pix_fmt) {
  64. /* case PIX_FMT_RGB555:
  65. s->pixel_size = 2;
  66. break;*/
  67. case PIX_FMT_RGB24:
  68. s->pixel_size = 3;
  69. break;
  70. default:
  71. av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
  72. break;
  73. }
  74. avctx->bits_per_sample = s->pixel_size*8;
  75. s->rlecode_table = av_mallocz(s->avctx->width);
  76. s->skip_table = av_mallocz(s->avctx->width);
  77. s->length_table = av_mallocz((s->avctx->width + 1)*sizeof(int));
  78. if (!s->skip_table || !s->length_table || !s->rlecode_table) {
  79. av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
  80. return -1;
  81. }
  82. if (avpicture_alloc(&s->previous_frame, avctx->pix_fmt, avctx->width, avctx->height) < 0) {
  83. av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
  84. return -1;
  85. }
  86. s->max_buf_size = s->avctx->width*s->avctx->height*s->pixel_size /* image base material */
  87. + 15 /* header + footer */
  88. + s->avctx->height*2 /* skip code+rle end */
  89. + s->avctx->width/MAX_RLE_BULK + 1 /* rle codes */;
  90. avctx->coded_frame = &s->frame;
  91. return 0;
  92. }
  93. /**
  94. * Computes the best RLE sequence for a line
  95. */
  96. static void qtrle_encode_line(QtrleEncContext *s, AVFrame *p, int line, uint8_t **buf)
  97. {
  98. int width=s->avctx->width;
  99. int i;
  100. signed char rlecode;
  101. /* We will use it to compute the best bulk copy sequence */
  102. unsigned int bulkcount;
  103. /* This will be the number of pixels equal to the preivous frame one's
  104. * starting from the ith pixel */
  105. unsigned int skipcount;
  106. /* This will be the number of consecutive equal pixels in the current
  107. * frame, starting from the ith one also */
  108. unsigned int repeatcount;
  109. /* The cost of the three different possibilities */
  110. int total_bulk_cost;
  111. int total_skip_cost;
  112. int total_repeat_cost;
  113. int temp_cost;
  114. int j;
  115. uint8_t *this_line = p-> data[0] + line*p->linesize[0] + (width - 1)*s->pixel_size;
  116. uint8_t *prev_line = s->previous_frame.data[0] + line*p->linesize[0] + (width - 1)*s->pixel_size;
  117. s->length_table[width] = 0;
  118. skipcount = 0;
  119. for (i = width - 1; i >= 0; i--) {
  120. if (!s->frame.key_frame && !memcmp(this_line, prev_line, s->pixel_size))
  121. skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
  122. else
  123. skipcount = 0;
  124. total_skip_cost = s->length_table[i + skipcount] + 2;
  125. s->skip_table[i] = skipcount;
  126. if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
  127. repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
  128. else
  129. repeatcount = 1;
  130. total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
  131. /* skip code is free for the first pixel, it costs one byte for repeat and bulk copy
  132. * so let's make it aware */
  133. if (i == 0) {
  134. total_skip_cost--;
  135. total_repeat_cost++;
  136. }
  137. if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
  138. /* repeat is the best */
  139. s->length_table[i] = total_repeat_cost;
  140. s->rlecode_table[i] = -repeatcount;
  141. }
  142. else if (skipcount > 0) {
  143. /* skip is the best choice here */
  144. s->length_table[i] = total_skip_cost;
  145. s->rlecode_table[i] = 0;
  146. }
  147. else {
  148. /* We cannot do neither skip nor repeat
  149. * thus we search for the best bulk copy to do */
  150. int limit = FFMIN(width - i, MAX_RLE_BULK);
  151. temp_cost = 1 + s->pixel_size + !i;
  152. total_bulk_cost = INT_MAX;
  153. for (j = 1; j <= limit; j++) {
  154. if (s->length_table[i + j] + temp_cost < total_bulk_cost) {
  155. /* We have found a better bulk copy ... */
  156. total_bulk_cost = s->length_table[i + j] + temp_cost;
  157. bulkcount = j;
  158. }
  159. temp_cost += s->pixel_size;
  160. }
  161. s->length_table[i] = total_bulk_cost;
  162. s->rlecode_table[i] = bulkcount;
  163. }
  164. this_line -= s->pixel_size;
  165. prev_line -= s->pixel_size;
  166. }
  167. /* Good ! Now we have the best sequence for this line, let's ouput it */
  168. /* We do a special case for the first pixel so that we avoid testing it in
  169. * the whole loop */
  170. i=0;
  171. this_line = p-> data[0] + line*p->linesize[0];
  172. prev_line = s->previous_frame.data[0] + line*p->linesize[0];
  173. if (s->rlecode_table[0] == 0) {
  174. bytestream_put_byte(buf, s->skip_table[0] + 1);
  175. i += s->skip_table[0];
  176. }
  177. else bytestream_put_byte(buf, 1);
  178. while (i < width) {
  179. rlecode = s->rlecode_table[i];
  180. bytestream_put_byte(buf, rlecode);
  181. if (rlecode == 0) {
  182. /* Write a skip sequence */
  183. bytestream_put_byte(buf, s->skip_table[i] + 1);
  184. i += s->skip_table[i];
  185. }
  186. else if (rlecode > 0) {
  187. /* bulk copy */
  188. bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
  189. i += rlecode;
  190. }
  191. else {
  192. /* repeat the bits */
  193. bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
  194. i -= rlecode;
  195. }
  196. }
  197. bytestream_put_byte(buf, -1); // end RLE line
  198. }
  199. /** Encodes frame including header */
  200. static int encode_frame(QtrleEncContext *s, AVFrame *p, uint8_t *buf)
  201. {
  202. int i;
  203. int start_line = 0;
  204. int end_line = s->avctx->height;
  205. uint8_t *orig_buf = buf;
  206. if (!s->frame.key_frame) {
  207. for (start_line = 0; start_line < s->avctx->height; start_line++)
  208. if (memcmp(p->data[0] + start_line*p->linesize[0],
  209. s->previous_frame.data[0] + start_line*p->linesize[0],
  210. p->linesize[0]))
  211. break;
  212. for (end_line=s->avctx->height; end_line > start_line; end_line--)
  213. if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
  214. s->previous_frame.data[0] + (end_line - 1)*p->linesize[0],
  215. p->linesize[0]))
  216. break;
  217. }
  218. bytestream_put_be32(&buf, 0); // CHUNK SIZE, patched later
  219. if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
  220. bytestream_put_be16(&buf, 0); // header
  221. else {
  222. bytestream_put_be16(&buf, 8); // header
  223. bytestream_put_be16(&buf, start_line); // starting line
  224. bytestream_put_be16(&buf, 0); // unknown
  225. bytestream_put_be16(&buf, end_line - start_line); // lines to update
  226. bytestream_put_be16(&buf, 0); // unknown
  227. }
  228. for (i = start_line; i < end_line; i++)
  229. qtrle_encode_line(s, p, i, &buf);
  230. bytestream_put_byte(&buf, 0); // zero skip code = frame finished
  231. AV_WB32(orig_buf, buf - orig_buf); // patch the chunk size
  232. return buf - orig_buf;
  233. }
  234. static int qtrle_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
  235. {
  236. QtrleEncContext * const s = avctx->priv_data;
  237. AVFrame *pict = data;
  238. AVFrame * const p = &s->frame;
  239. int chunksize;
  240. *p = *pict;
  241. if (buf_size < s->max_buf_size) {
  242. /* Upper bound check for compressed data */
  243. av_log(avctx, AV_LOG_ERROR, "buf_size %d < %d\n", buf_size, s->max_buf_size);
  244. return -1;
  245. }
  246. if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
  247. /* I-Frame */
  248. p->pict_type = FF_I_TYPE;
  249. p->key_frame = 1;
  250. } else {
  251. /* P-Frame */
  252. p->pict_type = FF_P_TYPE;
  253. p->key_frame = 0;
  254. }
  255. chunksize = encode_frame(s, pict, buf);
  256. /* save the current frame */
  257. av_picture_copy(&s->previous_frame, (AVPicture *)p, avctx->pix_fmt, avctx->width, avctx->height);
  258. return chunksize;
  259. }
  260. static av_cold int qtrle_encode_end(AVCodecContext *avctx)
  261. {
  262. QtrleEncContext *s = avctx->priv_data;
  263. avpicture_free(&s->previous_frame);
  264. av_free(s->rlecode_table);
  265. av_free(s->length_table);
  266. av_free(s->skip_table);
  267. return 0;
  268. }
  269. AVCodec qtrle_encoder = {
  270. "qtrle",
  271. CODEC_TYPE_VIDEO,
  272. CODEC_ID_QTRLE,
  273. sizeof(QtrleEncContext),
  274. qtrle_encode_init,
  275. qtrle_encode_frame,
  276. qtrle_encode_end,
  277. .pix_fmts = (enum PixelFormat[]){PIX_FMT_RGB24, -1},
  278. };