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.

317 lines
10KB

  1. /*
  2. * Copyright (c) 2000 Fabrice Bellard
  3. * Copyright (c) 2002 Francois Revol
  4. * Copyright (c) 2006 Baptiste Coudurier
  5. *
  6. * first version by Francois Revol <revol@free.fr>
  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. /**
  25. * @file
  26. * GIF encoder
  27. * @see http://www.w3.org/Graphics/GIF/spec-gif89a.txt
  28. */
  29. #define BITSTREAM_WRITER_LE
  30. #include "libavutil/opt.h"
  31. #include "libavutil/imgutils.h"
  32. #include "avcodec.h"
  33. #include "bytestream.h"
  34. #include "internal.h"
  35. #include "lzw.h"
  36. #include "gif.h"
  37. #include "put_bits.h"
  38. typedef struct {
  39. const AVClass *class;
  40. LZWState *lzw;
  41. uint8_t *buf;
  42. AVFrame *last_frame;
  43. int flags;
  44. uint32_t palette[AVPALETTE_COUNT]; ///< local reference palette for !pal8
  45. uint8_t *tmpl; ///< temporary line buffer
  46. } GIFContext;
  47. enum {
  48. GF_OFFSETTING = 1<<0,
  49. GF_TRANSDIFF = 1<<1,
  50. };
  51. static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
  52. {
  53. int histogram[AVPALETTE_COUNT] = {0};
  54. int x, y, i;
  55. for (y = 0; y < h; y++) {
  56. for (x = 0; x < w; x++)
  57. histogram[buf[x]]++;
  58. buf += linesize;
  59. }
  60. for (i = 0; i < FF_ARRAY_ELEMS(histogram); i++)
  61. if (!histogram[i])
  62. return i;
  63. return -1;
  64. }
  65. static int gif_image_write_image(AVCodecContext *avctx,
  66. uint8_t **bytestream, uint8_t *end,
  67. const uint32_t *palette,
  68. const uint8_t *buf, const int linesize,
  69. AVPacket *pkt)
  70. {
  71. GIFContext *s = avctx->priv_data;
  72. int len = 0, height = avctx->height, width = avctx->width, x, y;
  73. int x_start = 0, y_start = 0, trans = -1;
  74. const uint8_t *ptr;
  75. /* Crop image */
  76. // TODO support with palette change
  77. if ((s->flags & GF_OFFSETTING) && s->last_frame && !palette) {
  78. const uint8_t *ref = s->last_frame->data[0];
  79. const int ref_linesize = s->last_frame->linesize[0];
  80. int x_end = avctx->width - 1,
  81. y_end = avctx->height - 1;
  82. /* skip common lines */
  83. while (y_start < y_end) {
  84. if (memcmp(ref + y_start*ref_linesize, buf + y_start*linesize, width))
  85. break;
  86. y_start++;
  87. }
  88. while (y_end > y_start) {
  89. if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, width))
  90. break;
  91. y_end--;
  92. }
  93. height = y_end + 1 - y_start;
  94. /* skip common columns */
  95. while (x_start < x_end) {
  96. int same_column = 1;
  97. for (y = y_start; y < y_end; y++) {
  98. if (ref[y*ref_linesize + x_start] != buf[y*linesize + x_start]) {
  99. same_column = 0;
  100. break;
  101. }
  102. }
  103. if (!same_column)
  104. break;
  105. x_start++;
  106. }
  107. while (x_end > x_start) {
  108. int same_column = 1;
  109. for (y = y_start; y < y_end; y++) {
  110. if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
  111. same_column = 0;
  112. break;
  113. }
  114. }
  115. if (!same_column)
  116. break;
  117. x_end--;
  118. }
  119. width = x_end + 1 - x_start;
  120. av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
  121. width, height, x_start, y_start, avctx->width, avctx->height);
  122. }
  123. /* image block */
  124. bytestream_put_byte(bytestream, GIF_IMAGE_SEPARATOR);
  125. bytestream_put_le16(bytestream, x_start);
  126. bytestream_put_le16(bytestream, y_start);
  127. bytestream_put_le16(bytestream, width);
  128. bytestream_put_le16(bytestream, height);
  129. if (!palette) {
  130. bytestream_put_byte(bytestream, 0x00); /* flags */
  131. } else {
  132. unsigned i;
  133. bytestream_put_byte(bytestream, 1<<7 | 0x7); /* flags */
  134. for (i = 0; i < AVPALETTE_COUNT; i++) {
  135. const uint32_t v = palette[i];
  136. bytestream_put_be24(bytestream, v);
  137. }
  138. }
  139. /* TODO: support with palette change (pal8) */
  140. if ((s->flags & GF_TRANSDIFF) && s->last_frame && !palette) {
  141. trans = pick_palette_entry(buf + y_start*linesize + x_start,
  142. linesize, width, height);
  143. if (trans < 0) { // TODO, patch welcome
  144. av_log(avctx, AV_LOG_DEBUG, "No available color, can not use transparency\n");
  145. } else {
  146. uint8_t *pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  147. if (!pal_exdata)
  148. return AVERROR(ENOMEM);
  149. memcpy(pal_exdata, s->palette, AVPALETTE_SIZE);
  150. pal_exdata[trans*4 + 3*!HAVE_BIGENDIAN] = 0x00;
  151. }
  152. }
  153. bytestream_put_byte(bytestream, 0x08);
  154. ff_lzw_encode_init(s->lzw, s->buf, 2 * width * height,
  155. 12, FF_LZW_GIF, put_bits);
  156. ptr = buf + y_start*linesize + x_start;
  157. if (trans >= 0) {
  158. const int ref_linesize = s->last_frame->linesize[0];
  159. const uint8_t *ref = s->last_frame->data[0] + y_start*ref_linesize + x_start;
  160. for (y = 0; y < height; y++) {
  161. memcpy(s->tmpl, ptr, width);
  162. for (x = 0; x < width; x++)
  163. if (ref[x] == ptr[x])
  164. s->tmpl[x] = trans;
  165. len += ff_lzw_encode(s->lzw, s->tmpl, width);
  166. ptr += linesize;
  167. ref += ref_linesize;
  168. }
  169. } else {
  170. for (y = 0; y < height; y++) {
  171. len += ff_lzw_encode(s->lzw, ptr, width);
  172. ptr += linesize;
  173. }
  174. }
  175. len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
  176. ptr = s->buf;
  177. while (len > 0) {
  178. int size = FFMIN(255, len);
  179. bytestream_put_byte(bytestream, size);
  180. if (end - *bytestream < size)
  181. return -1;
  182. bytestream_put_buffer(bytestream, ptr, size);
  183. ptr += size;
  184. len -= size;
  185. }
  186. bytestream_put_byte(bytestream, 0x00); /* end of image block */
  187. return 0;
  188. }
  189. static av_cold int gif_encode_init(AVCodecContext *avctx)
  190. {
  191. GIFContext *s = avctx->priv_data;
  192. if (avctx->width > 65535 || avctx->height > 65535) {
  193. av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
  194. return AVERROR(EINVAL);
  195. }
  196. s->lzw = av_mallocz(ff_lzw_encode_state_size);
  197. s->buf = av_malloc(avctx->width*avctx->height*2);
  198. s->tmpl = av_malloc(avctx->width);
  199. if (!s->tmpl || !s->buf || !s->lzw)
  200. return AVERROR(ENOMEM);
  201. if (avpriv_set_systematic_pal2(s->palette, avctx->pix_fmt) < 0)
  202. av_assert0(avctx->pix_fmt == AV_PIX_FMT_PAL8);
  203. return 0;
  204. }
  205. static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  206. const AVFrame *pict, int *got_packet)
  207. {
  208. GIFContext *s = avctx->priv_data;
  209. AVFrame *const p = (AVFrame *)pict;
  210. uint8_t *outbuf_ptr, *end;
  211. const uint32_t *palette = NULL;
  212. int ret;
  213. if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
  214. return ret;
  215. outbuf_ptr = pkt->data;
  216. end = pkt->data + pkt->size;
  217. p->pict_type = AV_PICTURE_TYPE_I;
  218. p->key_frame = 1;
  219. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  220. uint8_t *pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  221. if (!pal_exdata)
  222. return AVERROR(ENOMEM);
  223. memcpy(pal_exdata, p->data[1], AVPALETTE_SIZE);
  224. palette = (uint32_t*)p->data[1];
  225. }
  226. gif_image_write_image(avctx, &outbuf_ptr, end, palette,
  227. pict->data[0], pict->linesize[0], pkt);
  228. if (!s->last_frame) {
  229. s->last_frame = av_frame_alloc();
  230. if (!s->last_frame)
  231. return AVERROR(ENOMEM);
  232. }
  233. av_frame_unref(s->last_frame);
  234. ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
  235. if (ret < 0)
  236. return ret;
  237. pkt->size = outbuf_ptr - pkt->data;
  238. pkt->flags |= AV_PKT_FLAG_KEY;
  239. *got_packet = 1;
  240. return 0;
  241. }
  242. static int gif_encode_close(AVCodecContext *avctx)
  243. {
  244. GIFContext *s = avctx->priv_data;
  245. av_freep(&s->lzw);
  246. av_freep(&s->buf);
  247. av_frame_free(&s->last_frame);
  248. av_freep(&s->tmpl);
  249. return 0;
  250. }
  251. #define OFFSET(x) offsetof(GIFContext, x)
  252. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  253. static const AVOption gif_options[] = {
  254. { "gifflags", "set GIF flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = GF_OFFSETTING|GF_TRANSDIFF}, 0, INT_MAX, FLAGS, "flags" },
  255. { "offsetting", "enable picture offsetting", 0, AV_OPT_TYPE_CONST, {.i64=GF_OFFSETTING}, INT_MIN, INT_MAX, FLAGS, "flags" },
  256. { "transdiff", "enable transparency detection between frames", 0, AV_OPT_TYPE_CONST, {.i64=GF_TRANSDIFF}, INT_MIN, INT_MAX, FLAGS, "flags" },
  257. { NULL }
  258. };
  259. static const AVClass gif_class = {
  260. .class_name = "GIF encoder",
  261. .item_name = av_default_item_name,
  262. .option = gif_options,
  263. .version = LIBAVUTIL_VERSION_INT,
  264. };
  265. AVCodec ff_gif_encoder = {
  266. .name = "gif",
  267. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  268. .type = AVMEDIA_TYPE_VIDEO,
  269. .id = AV_CODEC_ID_GIF,
  270. .priv_data_size = sizeof(GIFContext),
  271. .init = gif_encode_init,
  272. .encode2 = gif_encode_frame,
  273. .close = gif_encode_close,
  274. .pix_fmts = (const enum AVPixelFormat[]){
  275. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  276. AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
  277. },
  278. .priv_class = &gif_class,
  279. };