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.

413 lines
14KB

  1. /*
  2. * Quicktime Animation (RLE) Video Encoder
  3. * Copyright (C) 2007 Clemens Fruhwirth
  4. * Copyright (C) 2007 Alexis Ballier
  5. *
  6. * This file is based on flashsvenc.c.
  7. *
  8. * This file is part of FFmpeg.
  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 as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavutil/imgutils.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. /** Maximum RLE code for bulk copy */
  29. #define MAX_RLE_BULK 127
  30. /** Maximum RLE code for repeat */
  31. #define MAX_RLE_REPEAT 128
  32. /** Maximum RLE code for skip */
  33. #define MAX_RLE_SKIP 254
  34. typedef struct QtrleEncContext {
  35. AVCodecContext *avctx;
  36. int pixel_size;
  37. AVPicture previous_frame;
  38. unsigned int max_buf_size;
  39. int logical_width;
  40. /**
  41. * This array will contain at ith position the value of the best RLE code
  42. * if the line started at pixel i
  43. * There can be 3 values :
  44. * skip (0) : skip as much as possible pixels because they are equal to the
  45. * previous frame ones
  46. * repeat (<-1) : repeat that pixel -rle_code times, still as much as
  47. * possible
  48. * copy (>0) : copy the raw next rle_code pixels */
  49. signed char *rlecode_table;
  50. /**
  51. * This array will contain the length of the best rle encoding of the line
  52. * starting at ith pixel */
  53. int *length_table;
  54. /**
  55. * Will contain at ith position the number of consecutive pixels equal to the previous
  56. * frame starting from pixel i */
  57. uint8_t* skip_table;
  58. } QtrleEncContext;
  59. static av_cold int qtrle_encode_end(AVCodecContext *avctx)
  60. {
  61. QtrleEncContext *s = avctx->priv_data;
  62. av_frame_free(&avctx->coded_frame);
  63. avpicture_free(&s->previous_frame);
  64. av_free(s->rlecode_table);
  65. av_free(s->length_table);
  66. av_free(s->skip_table);
  67. return 0;
  68. }
  69. static av_cold int qtrle_encode_init(AVCodecContext *avctx)
  70. {
  71. QtrleEncContext *s = avctx->priv_data;
  72. int ret;
  73. if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
  74. return AVERROR(EINVAL);
  75. }
  76. s->avctx=avctx;
  77. s->logical_width=avctx->width;
  78. switch (avctx->pix_fmt) {
  79. case AV_PIX_FMT_GRAY8:
  80. if (avctx->width % 4) {
  81. av_log(avctx, AV_LOG_ERROR, "Width not being a multiple of 4 is not supported\n");
  82. return AVERROR(EINVAL);
  83. }
  84. s->logical_width = avctx->width / 4;
  85. s->pixel_size = 4;
  86. break;
  87. case AV_PIX_FMT_RGB555BE:
  88. s->pixel_size = 2;
  89. break;
  90. case AV_PIX_FMT_RGB24:
  91. s->pixel_size = 3;
  92. break;
  93. case AV_PIX_FMT_ARGB:
  94. s->pixel_size = 4;
  95. break;
  96. default:
  97. av_log(avctx, AV_LOG_ERROR, "Unsupported colorspace.\n");
  98. break;
  99. }
  100. avctx->bits_per_coded_sample = avctx->pix_fmt == AV_PIX_FMT_GRAY8 ? 40 : s->pixel_size*8;
  101. s->rlecode_table = av_mallocz(s->logical_width);
  102. s->skip_table = av_mallocz(s->logical_width);
  103. s->length_table = av_mallocz_array(s->logical_width + 1, sizeof(int));
  104. if (!s->skip_table || !s->length_table || !s->rlecode_table) {
  105. av_log(avctx, AV_LOG_ERROR, "Error allocating memory.\n");
  106. return AVERROR(ENOMEM);
  107. }
  108. if ((ret = avpicture_alloc(&s->previous_frame, avctx->pix_fmt, avctx->width, avctx->height)) < 0) {
  109. av_log(avctx, AV_LOG_ERROR, "Error allocating picture\n");
  110. return ret;
  111. }
  112. s->max_buf_size = s->logical_width*s->avctx->height*s->pixel_size*2 /* image base material */
  113. + 15 /* header + footer */
  114. + s->avctx->height*2 /* skip code+rle end */
  115. + s->logical_width/MAX_RLE_BULK + 1 /* rle codes */;
  116. avctx->coded_frame = av_frame_alloc();
  117. if (!avctx->coded_frame) {
  118. qtrle_encode_end(avctx);
  119. return AVERROR(ENOMEM);
  120. }
  121. return 0;
  122. }
  123. /**
  124. * Compute the best RLE sequence for a line
  125. */
  126. static void qtrle_encode_line(QtrleEncContext *s, const AVFrame *p, int line, uint8_t **buf)
  127. {
  128. int width=s->logical_width;
  129. int i;
  130. signed char rlecode;
  131. /* This will be the number of pixels equal to the preivous frame one's
  132. * starting from the ith pixel */
  133. unsigned int skipcount;
  134. /* This will be the number of consecutive equal pixels in the current
  135. * frame, starting from the ith one also */
  136. unsigned int av_uninit(repeatcount);
  137. /* The cost of the three different possibilities */
  138. int total_skip_cost;
  139. int total_repeat_cost;
  140. int base_bulk_cost;
  141. int lowest_bulk_cost;
  142. int lowest_bulk_cost_index;
  143. int sec_lowest_bulk_cost;
  144. int sec_lowest_bulk_cost_index;
  145. uint8_t *this_line = p-> data[0] + line*p-> linesize[0] +
  146. (width - 1)*s->pixel_size;
  147. uint8_t *prev_line = s->previous_frame.data[0] + line*s->previous_frame.linesize[0] +
  148. (width - 1)*s->pixel_size;
  149. s->length_table[width] = 0;
  150. skipcount = 0;
  151. /* Initial values */
  152. lowest_bulk_cost = INT_MAX / 2;
  153. lowest_bulk_cost_index = width;
  154. sec_lowest_bulk_cost = INT_MAX / 2;
  155. sec_lowest_bulk_cost_index = width;
  156. base_bulk_cost = 1 + s->pixel_size;
  157. for (i = width - 1; i >= 0; i--) {
  158. int prev_bulk_cost;
  159. /* If our lowest bulk cost index is too far away, replace it
  160. * with the next lowest bulk cost */
  161. if (FFMIN(width, i + MAX_RLE_BULK) < lowest_bulk_cost_index) {
  162. lowest_bulk_cost = sec_lowest_bulk_cost;
  163. lowest_bulk_cost_index = sec_lowest_bulk_cost_index;
  164. sec_lowest_bulk_cost = INT_MAX / 2;
  165. sec_lowest_bulk_cost_index = width;
  166. }
  167. /* Deal with the first pixel's bulk cost */
  168. if (!i) {
  169. base_bulk_cost++;
  170. lowest_bulk_cost++;
  171. sec_lowest_bulk_cost++;
  172. }
  173. /* Look at the bulk cost of the previous loop and see if it is
  174. * a new lower bulk cost */
  175. prev_bulk_cost = s->length_table[i + 1] + base_bulk_cost;
  176. if (prev_bulk_cost <= sec_lowest_bulk_cost) {
  177. /* If it's lower than the 2nd lowest, then it may be lower
  178. * than the lowest */
  179. if (prev_bulk_cost <= lowest_bulk_cost) {
  180. /* If we have found a new lowest bulk cost,
  181. * then the 2nd lowest bulk cost is now farther than the
  182. * lowest bulk cost, and will never be used */
  183. sec_lowest_bulk_cost = INT_MAX / 2;
  184. lowest_bulk_cost = prev_bulk_cost;
  185. lowest_bulk_cost_index = i + 1;
  186. } else {
  187. /* Then it must be the 2nd lowest bulk cost */
  188. sec_lowest_bulk_cost = prev_bulk_cost;
  189. sec_lowest_bulk_cost_index = i + 1;
  190. }
  191. }
  192. if (!s->avctx->coded_frame->key_frame && !memcmp(this_line, prev_line, s->pixel_size))
  193. skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
  194. else
  195. skipcount = 0;
  196. total_skip_cost = s->length_table[i + skipcount] + 2;
  197. s->skip_table[i] = skipcount;
  198. if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
  199. repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
  200. else
  201. repeatcount = 1;
  202. total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
  203. /* skip code is free for the first pixel, it costs one byte for repeat and bulk copy
  204. * so let's make it aware */
  205. if (i == 0) {
  206. total_skip_cost--;
  207. total_repeat_cost++;
  208. }
  209. if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
  210. /* repeat is the best */
  211. s->length_table[i] = total_repeat_cost;
  212. s->rlecode_table[i] = -repeatcount;
  213. }
  214. else if (skipcount > 0) {
  215. /* skip is the best choice here */
  216. s->length_table[i] = total_skip_cost;
  217. s->rlecode_table[i] = 0;
  218. }
  219. else {
  220. /* We cannot do neither skip nor repeat
  221. * thus we use the best bulk copy */
  222. s->length_table[i] = lowest_bulk_cost;
  223. s->rlecode_table[i] = lowest_bulk_cost_index - i;
  224. }
  225. /* These bulk costs increase every iteration */
  226. lowest_bulk_cost += s->pixel_size;
  227. sec_lowest_bulk_cost += s->pixel_size;
  228. this_line -= s->pixel_size;
  229. prev_line -= s->pixel_size;
  230. }
  231. /* Good ! Now we have the best sequence for this line, let's output it */
  232. /* We do a special case for the first pixel so that we avoid testing it in
  233. * the whole loop */
  234. i=0;
  235. this_line = p-> data[0] + line*p->linesize[0];
  236. if (s->rlecode_table[0] == 0) {
  237. bytestream_put_byte(buf, s->skip_table[0] + 1);
  238. i += s->skip_table[0];
  239. }
  240. else bytestream_put_byte(buf, 1);
  241. while (i < width) {
  242. rlecode = s->rlecode_table[i];
  243. bytestream_put_byte(buf, rlecode);
  244. if (rlecode == 0) {
  245. /* Write a skip sequence */
  246. bytestream_put_byte(buf, s->skip_table[i] + 1);
  247. i += s->skip_table[i];
  248. }
  249. else if (rlecode > 0) {
  250. /* bulk copy */
  251. if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  252. int j;
  253. // QT grayscale colorspace has 0=white and 255=black, we will
  254. // ignore the palette that is included in the AVFrame because
  255. // AV_PIX_FMT_GRAY8 has defined color mapping
  256. for (j = 0; j < rlecode*s->pixel_size; ++j)
  257. bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
  258. } else {
  259. bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
  260. }
  261. i += rlecode;
  262. }
  263. else {
  264. /* repeat the bits */
  265. if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  266. int j;
  267. // QT grayscale colorspace has 0=white and 255=black, ...
  268. for (j = 0; j < s->pixel_size; ++j)
  269. bytestream_put_byte(buf, *(this_line + i*s->pixel_size + j) ^ 0xff);
  270. } else {
  271. bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
  272. }
  273. i -= rlecode;
  274. }
  275. }
  276. bytestream_put_byte(buf, -1); // end RLE line
  277. }
  278. /** Encode frame including header */
  279. static int encode_frame(QtrleEncContext *s, const AVFrame *p, uint8_t *buf)
  280. {
  281. int i;
  282. int start_line = 0;
  283. int end_line = s->avctx->height;
  284. uint8_t *orig_buf = buf;
  285. if (!s->avctx->coded_frame->key_frame) {
  286. unsigned line_size = s->logical_width * s->pixel_size;
  287. for (start_line = 0; start_line < s->avctx->height; start_line++)
  288. if (memcmp(p->data[0] + start_line*p->linesize[0],
  289. s->previous_frame.data[0] + start_line*s->previous_frame.linesize[0],
  290. line_size))
  291. break;
  292. for (end_line=s->avctx->height; end_line > start_line; end_line--)
  293. if (memcmp(p->data[0] + (end_line - 1)*p->linesize[0],
  294. s->previous_frame.data[0] + (end_line - 1)*s->previous_frame.linesize[0],
  295. line_size))
  296. break;
  297. }
  298. bytestream_put_be32(&buf, 0); // CHUNK SIZE, patched later
  299. if ((start_line == 0 && end_line == s->avctx->height) || start_line == s->avctx->height)
  300. bytestream_put_be16(&buf, 0); // header
  301. else {
  302. bytestream_put_be16(&buf, 8); // header
  303. bytestream_put_be16(&buf, start_line); // starting line
  304. bytestream_put_be16(&buf, 0); // unknown
  305. bytestream_put_be16(&buf, end_line - start_line); // lines to update
  306. bytestream_put_be16(&buf, 0); // unknown
  307. }
  308. for (i = start_line; i < end_line; i++)
  309. qtrle_encode_line(s, p, i, &buf);
  310. bytestream_put_byte(&buf, 0); // zero skip code = frame finished
  311. AV_WB32(orig_buf, buf - orig_buf); // patch the chunk size
  312. return buf - orig_buf;
  313. }
  314. static int qtrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  315. const AVFrame *pict, int *got_packet)
  316. {
  317. QtrleEncContext * const s = avctx->priv_data;
  318. AVFrame * const p = avctx->coded_frame;
  319. int ret;
  320. if ((ret = ff_alloc_packet2(avctx, pkt, s->max_buf_size)) < 0)
  321. return ret;
  322. if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
  323. /* I-Frame */
  324. p->pict_type = AV_PICTURE_TYPE_I;
  325. p->key_frame = 1;
  326. } else {
  327. /* P-Frame */
  328. p->pict_type = AV_PICTURE_TYPE_P;
  329. p->key_frame = 0;
  330. }
  331. pkt->size = encode_frame(s, pict, pkt->data);
  332. /* save the current frame */
  333. av_picture_copy(&s->previous_frame, (const AVPicture *)pict,
  334. avctx->pix_fmt, avctx->width, avctx->height);
  335. if (p->key_frame)
  336. pkt->flags |= AV_PKT_FLAG_KEY;
  337. *got_packet = 1;
  338. return 0;
  339. }
  340. AVCodec ff_qtrle_encoder = {
  341. .name = "qtrle",
  342. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  343. .type = AVMEDIA_TYPE_VIDEO,
  344. .id = AV_CODEC_ID_QTRLE,
  345. .priv_data_size = sizeof(QtrleEncContext),
  346. .init = qtrle_encode_init,
  347. .encode2 = qtrle_encode_frame,
  348. .close = qtrle_encode_end,
  349. .pix_fmts = (const enum AVPixelFormat[]){
  350. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB555BE, AV_PIX_FMT_ARGB, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  351. },
  352. };