258 rivejä
6.7 KiB

  1. //
  2. // Lol Engine — GIF encoding sample
  3. //
  4. // Copyright © 2016 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <lol/engine.h>
  16. #include "loldebug.h"
  17. extern "C" {
  18. #include <libavutil/avassert.h>
  19. #include <libavutil/channel_layout.h>
  20. #include <libavutil/mathematics.h>
  21. #include <libavutil/timestamp.h>
  22. #include <libavformat/avformat.h>
  23. #include <libswscale/swscale.h>
  24. #include <libswresample/swresample.h>
  25. }
  26. using namespace lol;
  27. class gif_encoder
  28. {
  29. public:
  30. gif_encoder(ivec2 size)
  31. : m_avformat(nullptr),
  32. m_avcodec(nullptr),
  33. m_stream(nullptr),
  34. m_frame(nullptr),
  35. m_size(size),
  36. m_index(0)
  37. {
  38. av_register_all();
  39. av_log_set_callback(ffmpeg_logger);
  40. m_frame = av_frame_alloc();
  41. ASSERT(m_frame);
  42. m_frame->format = AV_PIX_FMT_RGB8; // 3:3:2 packed for GIF
  43. m_frame->width = m_size.x;
  44. m_frame->height = m_size.y;
  45. int ret = av_frame_get_buffer(m_frame, 32);
  46. ASSERT(ret >= 0);
  47. }
  48. bool open_file(char const *filename)
  49. {
  50. avformat_alloc_output_context2(&m_avformat, nullptr, nullptr, filename);
  51. if (!m_avformat)
  52. {
  53. msg::debug("could not deduce output format from file extension %s: using GIF\n", filename);
  54. avformat_alloc_output_context2(&m_avformat, nullptr, "gif", filename);
  55. if (!m_avformat)
  56. return false;
  57. }
  58. if (!open_codec())
  59. return false;
  60. if (!(m_avformat->oformat->flags & AVFMT_NOFILE))
  61. {
  62. int ret = avio_open(&m_avformat->pb, filename, AVIO_FLAG_WRITE);
  63. if (ret < 0)
  64. {
  65. msg::error("could not open '%s': %s\n", filename, error2string(ret).C());
  66. return false;
  67. }
  68. }
  69. int ret = avformat_write_header(m_avformat, nullptr);
  70. if (ret < 0)
  71. {
  72. msg::error("could not write header: %s\n", error2string(ret).C());
  73. return false;
  74. }
  75. return true;
  76. }
  77. bool push_image(Image &im)
  78. {
  79. // Make sure the encoder does not hold a reference on our
  80. // frame (GIF does that in order to compress using deltas).
  81. if (av_frame_make_writable(m_frame) < 0)
  82. return false;
  83. // Convert image to 3:3:2. TODO: add some dithering
  84. u8vec3 *data = im.Lock<PixelFormat::RGB_8>();
  85. for (int n = 0; n < im.GetSize().x * im.GetSize().y; ++n)
  86. m_frame->data[0][n] = (data[n].r & 0xe0) | ((data[n].g & 0xe0) >> 3) | (data[n].b >> 6);
  87. im.Unlock(data);
  88. m_frame->pts = m_index++;
  89. AVPacket pkt;
  90. memset(&pkt, 0, sizeof(pkt));
  91. av_init_packet(&pkt);
  92. // XXX: is got_packet necessary?
  93. int got_packet = 0;
  94. int ret = avcodec_encode_video2(m_avcodec, &pkt, m_frame, &got_packet);
  95. if (ret < 0)
  96. {
  97. msg::error("cannot encode video frame: %s\n", error2string(ret).C());
  98. return false;
  99. }
  100. if (got_packet)
  101. {
  102. pkt.stream_index = m_stream->index;
  103. ret = av_interleaved_write_frame(m_avformat, &pkt);
  104. if (ret < 0)
  105. {
  106. msg::error("cannot write video frame: %s\n", error2string(ret).C());
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. void close()
  113. {
  114. // this must be done before m_avcodec is freed
  115. av_write_trailer(m_avformat);
  116. avcodec_free_context(&m_avcodec);
  117. av_frame_free(&m_frame);
  118. if (!(m_avformat->oformat->flags & AVFMT_NOFILE))
  119. avio_closep(&m_avformat->pb);
  120. avformat_free_context(m_avformat);
  121. }
  122. private:
  123. bool open_codec()
  124. {
  125. AVCodec *codec = avcodec_find_encoder(m_avformat->oformat->video_codec);
  126. if (!codec)
  127. {
  128. msg::error("no encoder found for %s\n", avcodec_get_name(m_avformat->oformat->video_codec));
  129. return false;
  130. }
  131. m_stream = avformat_new_stream(m_avformat, nullptr);
  132. if (!m_stream)
  133. {
  134. msg::error("cannot allocate stream\n");
  135. return false;
  136. }
  137. m_stream->id = 0; // first (and only?) stream
  138. m_stream->time_base = AVRational{ 1, 30 }; // 30 fps
  139. m_avcodec = avcodec_alloc_context3(codec);
  140. if (!m_avcodec)
  141. {
  142. msg::error("cannot allocate encoding context\n");
  143. return false;
  144. }
  145. m_avcodec->codec_id = m_avformat->oformat->video_codec;
  146. m_avcodec->width = m_frame->width;
  147. m_avcodec->height = m_frame->height;
  148. m_avcodec->pix_fmt = AVPixelFormat(m_frame->format);
  149. m_avcodec->time_base = m_stream->time_base;
  150. if (m_avformat->oformat->flags & AVFMT_GLOBALHEADER)
  151. m_avcodec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  152. int ret = avcodec_open2(m_avcodec, codec, nullptr);
  153. if (ret < 0)
  154. {
  155. msg::error("cannot open video codec: %s\n", error2string(ret).C());
  156. return false;
  157. }
  158. ret = avcodec_parameters_from_context(m_stream->codecpar, m_avcodec);
  159. if (ret < 0)
  160. {
  161. msg::error("cannot copy stream parameters\n");
  162. return false;
  163. }
  164. return true;
  165. }
  166. static String error2string(int errnum)
  167. {
  168. char tmp[AV_ERROR_MAX_STRING_SIZE];
  169. av_strerror(errnum, tmp, AV_ERROR_MAX_STRING_SIZE);
  170. return String(tmp);
  171. }
  172. static void ffmpeg_logger(void *ptr, int level, const char *fmt, va_list vl)
  173. {
  174. // FIXME: use lol::msg::debug
  175. UNUSED(ptr, level);
  176. vfprintf(stderr, fmt, vl);
  177. }
  178. private:
  179. AVFormatContext *m_avformat;
  180. AVCodecContext *m_avcodec;
  181. AVStream *m_stream;
  182. AVFrame *m_frame;
  183. ivec2 m_size;
  184. int m_index;
  185. };
  186. int main(int argc, char **argv)
  187. {
  188. UNUSED(argc, argv);
  189. ivec2 size(256, 256);
  190. gif_encoder enc(size);
  191. if (!enc.open_file("16_movie.gif"))
  192. return EXIT_FAILURE;
  193. for (int i = 0; i < 256; ++i)
  194. {
  195. Image im(size);
  196. array2d<u8vec3> &data = im.Lock2D<PixelFormat::RGB_8>();
  197. for (int y = 0; y < size.y; ++y)
  198. for (int x = 0; x < size.x; ++x)
  199. {
  200. data[x][y].r = x * i / 2;
  201. data[x][y].g = x / 4 * 4 * y / 16 + i;
  202. data[x][y].b = y + i;
  203. }
  204. im.Unlock2D(data);
  205. if (!enc.push_image(im))
  206. break;
  207. }
  208. enc.close();
  209. return 0;
  210. }