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.

sequence.c 7.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2009 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * codec.c: image I/O functions
  16. */
  17. #include "config.h"
  18. #if defined _WIN32
  19. # undef _CRT_SECURE_NO_WARNINGS
  20. # define _CRT_SECURE_NO_WARNINGS /* I know how to use snprintf, thank you */
  21. # define snprintf _snprintf
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #if defined USE_FFMPEG
  27. # include <libavformat/avformat.h>
  28. # include <libswscale/swscale.h>
  29. #endif
  30. #include "pipi.h"
  31. #include "pipi_internals.h"
  32. #if defined USE_FFMPEG
  33. typedef struct
  34. {
  35. uint8_t *buf;
  36. size_t buf_len;
  37. AVFormatContext *fmt_ctx;
  38. AVStream *stream;
  39. AVCodecContext *cod_ctx;
  40. AVCodec *codec;
  41. AVFrame *frame;
  42. struct SwsContext *sws_ctx;
  43. int src_width, src_height;
  44. }
  45. ffmpeg_codec_t;
  46. #endif
  47. #define PAR_NUM 1
  48. #define PAR_DEN 1
  49. #define BITRATE (16 * 1024 * 1024)
  50. pipi_sequence_t *pipi_open_sequence(char const *file,
  51. int width, int height, int fps)
  52. {
  53. #if defined USE_FFMPEG
  54. static int initialised = 0;
  55. pipi_sequence_t *seq;
  56. ffmpeg_codec_t *ff;
  57. uint8_t *tmp;
  58. seq = malloc(sizeof(pipi_sequence_t));
  59. seq->w = width;
  60. seq->h = height;
  61. seq->fps = fps;
  62. ff = malloc(sizeof(ffmpeg_codec_t));
  63. memset(ff, 0, sizeof(*ff));
  64. seq->codec_priv = ff;
  65. if (!initialised)
  66. {
  67. av_register_all();
  68. initialised = 1;
  69. }
  70. ff->fmt_ctx = avformat_alloc_context();
  71. if (!ff->fmt_ctx)
  72. goto error;
  73. /* Careful here: the Win32 snprintf doesn't seem to add a trailing
  74. * zero to the truncated output. */
  75. snprintf(ff->fmt_ctx->filename, sizeof(ff->fmt_ctx->filename),
  76. file);
  77. ff->fmt_ctx->filename[sizeof(ff->fmt_ctx->filename) - 1] = '\0';
  78. ff->fmt_ctx->oformat = guess_format(NULL, file, NULL);
  79. if (!ff->fmt_ctx->oformat)
  80. ff->fmt_ctx->oformat = guess_format("mpeg", NULL, NULL);
  81. if (!ff->fmt_ctx->oformat)
  82. goto error;
  83. ff->stream = av_new_stream(ff->fmt_ctx, 0);
  84. if (!ff->stream)
  85. goto error;
  86. ff->stream->sample_aspect_ratio.num = PAR_NUM;
  87. ff->stream->sample_aspect_ratio.den = PAR_DEN;
  88. ff->cod_ctx = ff->stream->codec;
  89. ff->cod_ctx->width = width;
  90. ff->cod_ctx->height = height;
  91. ff->cod_ctx->sample_aspect_ratio.num = PAR_NUM;
  92. ff->cod_ctx->sample_aspect_ratio.den = PAR_DEN;
  93. ff->cod_ctx->codec_id = ff->fmt_ctx->oformat->video_codec;
  94. ff->cod_ctx->codec_type = CODEC_TYPE_VIDEO;
  95. ff->cod_ctx->bit_rate = BITRATE;
  96. ff->cod_ctx->time_base.num = 1;
  97. ff->cod_ctx->time_base.den = fps;
  98. ff->cod_ctx->gop_size = fps * 3 / 4; /* empirical */
  99. ff->cod_ctx->pix_fmt = PIX_FMT_YUV420P; /* send YUV 420 */
  100. if (ff->cod_ctx->codec_id == CODEC_ID_MPEG2VIDEO)
  101. ff->cod_ctx->max_b_frames = 2;
  102. if (ff->cod_ctx->codec_id == CODEC_ID_MPEG1VIDEO)
  103. ff->cod_ctx->mb_decision = 2;
  104. if (ff->fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  105. ff->cod_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
  106. if (av_set_parameters(ff->fmt_ctx, NULL) < 0)
  107. goto error;
  108. ff->codec = avcodec_find_encoder(ff->cod_ctx->codec_id);
  109. if (!ff->codec)
  110. goto error;
  111. if (avcodec_open(ff->cod_ctx, ff->codec) < 0)
  112. goto error;
  113. ff->frame = avcodec_alloc_frame();
  114. if (!ff->frame)
  115. goto error;
  116. tmp = (uint8_t *)av_malloc(avpicture_get_size(ff->cod_ctx->pix_fmt,
  117. ff->cod_ctx->width,
  118. ff->cod_ctx->height));
  119. if (!tmp)
  120. goto error;
  121. avpicture_fill((AVPicture *)ff->frame, tmp, ff->cod_ctx->pix_fmt,
  122. ff->cod_ctx->width, ff->cod_ctx->height);
  123. if (!(ff->fmt_ctx->flags & AVFMT_NOFILE))
  124. if (url_fopen(&ff->fmt_ctx->pb, file, URL_WRONLY) < 0)
  125. goto error;
  126. ff->buf_len = 64 * 1024 * 1024;
  127. ff->buf = (uint8_t *)av_malloc(ff->buf_len);
  128. av_write_header(ff->fmt_ctx);
  129. return seq;
  130. error:
  131. pipi_close_sequence(seq);
  132. return NULL;
  133. #else
  134. return NULL;
  135. #endif
  136. }
  137. int pipi_feed_sequence(pipi_sequence_t *seq, uint8_t *buffer, int width, int height)
  138. {
  139. #if defined USE_FFMPEG
  140. AVPacket packet;
  141. size_t bytes;
  142. int pitch;
  143. ffmpeg_codec_t *ff = (ffmpeg_codec_t *)seq->codec_priv;
  144. if (ff->src_width != width || ff->src_height != height)
  145. {
  146. ff->src_width = width;
  147. ff->src_height = height;
  148. if (ff->sws_ctx)
  149. sws_freeContext(ff->sws_ctx);
  150. ff->sws_ctx = NULL;
  151. }
  152. if (!ff->sws_ctx)
  153. ff->sws_ctx = sws_getContext(width, height, PIX_FMT_RGB32,
  154. ff->cod_ctx->width,
  155. ff->cod_ctx->height,
  156. ff->cod_ctx->pix_fmt, SWS_BICUBIC,
  157. NULL, NULL, NULL);
  158. if (!ff->sws_ctx)
  159. return -1;
  160. pitch = width * 4;
  161. sws_scale(ff->sws_ctx, &buffer, &pitch, 0, height,
  162. ff->frame->data, ff->frame->linesize);
  163. bytes = avcodec_encode_video(ff->cod_ctx, ff->buf,
  164. ff->buf_len, ff->frame);
  165. if (bytes <= 0)
  166. return 0;
  167. av_init_packet(&packet);
  168. if (ff->cod_ctx->coded_frame->pts != 0x8000000000000000LL)
  169. packet.pts = av_rescale_q(ff->cod_ctx->coded_frame->pts,
  170. ff->cod_ctx->time_base, ff->stream->time_base);
  171. if (ff->cod_ctx->coded_frame->key_frame)
  172. packet.flags |= PKT_FLAG_KEY;
  173. packet.stream_index = 0;
  174. packet.data = ff->buf;
  175. packet.size = bytes;
  176. if (av_interleaved_write_frame(ff->fmt_ctx, &packet) < 0)
  177. return -1;
  178. #endif
  179. return 0;
  180. }
  181. int pipi_close_sequence(pipi_sequence_t *seq)
  182. {
  183. #if defined USE_FFMPEG
  184. ffmpeg_codec_t *ff = (ffmpeg_codec_t *)seq->codec_priv;
  185. if (ff->fmt_ctx)
  186. {
  187. av_write_trailer(ff->fmt_ctx);
  188. }
  189. if (ff->buf)
  190. {
  191. av_free(ff->buf);
  192. ff->buf = NULL;
  193. }
  194. if (ff->cod_ctx)
  195. {
  196. avcodec_close(ff->cod_ctx);
  197. ff->cod_ctx = NULL;
  198. }
  199. if (ff->frame)
  200. {
  201. av_free(ff->frame->data[0]);
  202. av_free(ff->frame);
  203. ff->frame = NULL;
  204. }
  205. if (ff->fmt_ctx)
  206. {
  207. av_freep(&ff->fmt_ctx->streams[0]->codec);
  208. ff->codec = NULL;
  209. av_freep(&ff->fmt_ctx->streams[0]);
  210. ff->stream = NULL;
  211. if (!(ff->fmt_ctx->flags & AVFMT_NOFILE))
  212. url_fclose(ff->fmt_ctx->pb);
  213. av_free(ff->fmt_ctx);
  214. ff->fmt_ctx = NULL;
  215. }
  216. if (ff->sws_ctx)
  217. {
  218. sws_freeContext(ff->sws_ctx);
  219. ff->sws_ctx = NULL;
  220. ff->src_width = ff->src_height = 0;
  221. }
  222. free(ff);
  223. free(seq);
  224. #endif
  225. return 0;
  226. }