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.
 
 
 
 
 
 

279 lines
7.1 KiB

  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. pipi_sequence_t *pipi_open_sequence(char const *file,
  48. int width, int height, int fps,
  49. int par_num, int par_den, int bitrate)
  50. {
  51. #if defined USE_FFMPEG
  52. static int initialised = 0;
  53. pipi_sequence_t *seq;
  54. ffmpeg_codec_t *ff;
  55. uint8_t *tmp;
  56. seq = malloc(sizeof(pipi_sequence_t));
  57. seq->w = width;
  58. seq->h = height;
  59. seq->fps = fps;
  60. ff = malloc(sizeof(ffmpeg_codec_t));
  61. memset(ff, 0, sizeof(*ff));
  62. seq->codec_priv = ff;
  63. if (!initialised)
  64. {
  65. av_register_all();
  66. initialised = 1;
  67. }
  68. ff->fmt_ctx = avformat_alloc_context();
  69. if (!ff->fmt_ctx)
  70. goto error;
  71. /* Careful here: the Win32 snprintf doesn't seem to add a trailing
  72. * zero to the truncated output. */
  73. snprintf(ff->fmt_ctx->filename, sizeof(ff->fmt_ctx->filename),
  74. file);
  75. ff->fmt_ctx->filename[sizeof(ff->fmt_ctx->filename) - 1] = '\0';
  76. ff->fmt_ctx->oformat = guess_format(NULL, file, NULL);
  77. if (!ff->fmt_ctx->oformat)
  78. ff->fmt_ctx->oformat = guess_format("mpeg", NULL, NULL);
  79. if (!ff->fmt_ctx->oformat)
  80. goto error;
  81. ff->stream = av_new_stream(ff->fmt_ctx, 0);
  82. if (!ff->stream)
  83. goto error;
  84. ff->stream->sample_aspect_ratio.num = par_num;
  85. ff->stream->sample_aspect_ratio.den = par_den;
  86. ff->cod_ctx = ff->stream->codec;
  87. ff->cod_ctx->width = width;
  88. ff->cod_ctx->height = height;
  89. ff->cod_ctx->sample_aspect_ratio.num = par_num;
  90. ff->cod_ctx->sample_aspect_ratio.den = par_den;
  91. ff->cod_ctx->codec_id = ff->fmt_ctx->oformat->video_codec;
  92. ff->cod_ctx->codec_type = CODEC_TYPE_VIDEO;
  93. ff->cod_ctx->bit_rate = bitrate;
  94. ff->cod_ctx->time_base.num = 1;
  95. ff->cod_ctx->time_base.den = fps;
  96. ff->cod_ctx->gop_size = fps * 3 / 4; /* empirical */
  97. ff->cod_ctx->pix_fmt = PIX_FMT_YUV420P; /* send YUV 420 */
  98. if (ff->cod_ctx->codec_id == CODEC_ID_MPEG2VIDEO)
  99. ff->cod_ctx->max_b_frames = 2;
  100. if (ff->cod_ctx->codec_id == CODEC_ID_MPEG1VIDEO)
  101. ff->cod_ctx->mb_decision = 2;
  102. if (ff->fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  103. ff->cod_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
  104. if (av_set_parameters(ff->fmt_ctx, NULL) < 0)
  105. goto error;
  106. ff->codec = avcodec_find_encoder(ff->cod_ctx->codec_id);
  107. if (!ff->codec)
  108. goto error;
  109. if (avcodec_open(ff->cod_ctx, ff->codec) < 0)
  110. goto error;
  111. ff->frame = avcodec_alloc_frame();
  112. if (!ff->frame)
  113. goto error;
  114. tmp = (uint8_t *)av_malloc(avpicture_get_size(ff->cod_ctx->pix_fmt,
  115. ff->cod_ctx->width,
  116. ff->cod_ctx->height));
  117. if (!tmp)
  118. goto error;
  119. avpicture_fill((AVPicture *)ff->frame, tmp, ff->cod_ctx->pix_fmt,
  120. ff->cod_ctx->width, ff->cod_ctx->height);
  121. if (!(ff->fmt_ctx->flags & AVFMT_NOFILE))
  122. if (url_fopen(&ff->fmt_ctx->pb, file, URL_WRONLY) < 0)
  123. goto error;
  124. ff->buf_len = 64 * 1024 * 1024;
  125. ff->buf = (uint8_t *)av_malloc(ff->buf_len);
  126. av_write_header(ff->fmt_ctx);
  127. return seq;
  128. error:
  129. pipi_close_sequence(seq);
  130. return NULL;
  131. #else
  132. return NULL;
  133. #endif
  134. }
  135. int pipi_feed_sequence(pipi_sequence_t *seq, uint8_t const *buffer,
  136. int width, int height)
  137. {
  138. #if defined USE_FFMPEG
  139. AVPacket packet;
  140. size_t bytes;
  141. int pitch;
  142. ffmpeg_codec_t *ff = (ffmpeg_codec_t *)seq->codec_priv;
  143. if (ff->src_width != width || ff->src_height != height)
  144. {
  145. ff->src_width = width;
  146. ff->src_height = height;
  147. if (ff->sws_ctx)
  148. sws_freeContext(ff->sws_ctx);
  149. ff->sws_ctx = NULL;
  150. }
  151. if (!ff->sws_ctx)
  152. ff->sws_ctx = sws_getContext(width, height, PIX_FMT_RGB32,
  153. ff->cod_ctx->width,
  154. ff->cod_ctx->height,
  155. ff->cod_ctx->pix_fmt, SWS_BICUBIC,
  156. NULL, NULL, NULL);
  157. if (!ff->sws_ctx)
  158. return -1;
  159. pitch = width * 4;
  160. sws_scale(ff->sws_ctx, &buffer, &pitch, 0, height,
  161. ff->frame->data, ff->frame->linesize);
  162. bytes = avcodec_encode_video(ff->cod_ctx, ff->buf,
  163. ff->buf_len, ff->frame);
  164. if (bytes <= 0)
  165. return 0;
  166. av_init_packet(&packet);
  167. if (ff->cod_ctx->coded_frame->pts != 0x8000000000000000LL)
  168. packet.pts = av_rescale_q(ff->cod_ctx->coded_frame->pts,
  169. ff->cod_ctx->time_base, ff->stream->time_base);
  170. if (ff->cod_ctx->coded_frame->key_frame)
  171. packet.flags |= PKT_FLAG_KEY;
  172. packet.stream_index = 0;
  173. packet.data = ff->buf;
  174. packet.size = bytes;
  175. if (av_interleaved_write_frame(ff->fmt_ctx, &packet) < 0)
  176. return -1;
  177. #endif
  178. return 0;
  179. }
  180. int pipi_close_sequence(pipi_sequence_t *seq)
  181. {
  182. #if defined USE_FFMPEG
  183. ffmpeg_codec_t *ff = (ffmpeg_codec_t *)seq->codec_priv;
  184. if (ff->fmt_ctx)
  185. {
  186. av_write_trailer(ff->fmt_ctx);
  187. }
  188. if (ff->buf)
  189. {
  190. av_free(ff->buf);
  191. ff->buf = NULL;
  192. }
  193. if (ff->cod_ctx)
  194. {
  195. avcodec_close(ff->cod_ctx);
  196. ff->cod_ctx = NULL;
  197. }
  198. if (ff->frame)
  199. {
  200. av_free(ff->frame->data[0]);
  201. av_free(ff->frame);
  202. ff->frame = NULL;
  203. }
  204. if (ff->fmt_ctx)
  205. {
  206. av_freep(&ff->fmt_ctx->streams[0]->codec);
  207. ff->codec = NULL;
  208. av_freep(&ff->fmt_ctx->streams[0]);
  209. ff->stream = NULL;
  210. if (!(ff->fmt_ctx->flags & AVFMT_NOFILE))
  211. url_fclose(ff->fmt_ctx->pb);
  212. av_free(ff->fmt_ctx);
  213. ff->fmt_ctx = NULL;
  214. }
  215. if (ff->sws_ctx)
  216. {
  217. sws_freeContext(ff->sws_ctx);
  218. ff->sws_ctx = NULL;
  219. ff->src_width = ff->src_height = 0;
  220. }
  221. free(ff);
  222. free(seq);
  223. #endif
  224. return 0;
  225. }