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.
 
 
 
 
 
 

309 line
8.2 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 = av_guess_format(NULL, file, NULL);
  77. if (!ff->fmt_ctx->oformat)
  78. ff->fmt_ctx->oformat = av_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->pix_fmt = PIX_FMT_YUV420P; /* send YUV 420 */
  97. if (ff->cod_ctx->codec_id == CODEC_ID_MPEG2VIDEO)
  98. ff->cod_ctx->max_b_frames = 2;
  99. if (ff->cod_ctx->codec_id == CODEC_ID_MPEG1VIDEO)
  100. ff->cod_ctx->mb_decision = 2;
  101. if (ff->cod_ctx->codec_id == CODEC_ID_H264)
  102. {
  103. /* Import x264 slow presets */
  104. ff->cod_ctx->coder_type = 1;
  105. ff->cod_ctx->flags |= CODEC_FLAG_LOOP_FILTER;
  106. ff->cod_ctx->me_cmp |= FF_CMP_CHROMA;
  107. ff->cod_ctx->partitions |= X264_PART_I4X4 | X264_PART_I8X8
  108. | X264_PART_P4X4 | X264_PART_P8X8;
  109. ff->cod_ctx->me_method = ME_UMH;
  110. ff->cod_ctx->me_subpel_quality = 8;
  111. ff->cod_ctx->me_range = 16;
  112. ff->cod_ctx->gop_size = 250;
  113. ff->cod_ctx->keyint_min = 25;
  114. ff->cod_ctx->scenechange_threshold = 40;
  115. ff->cod_ctx->i_quant_factor = 0.71f;
  116. ff->cod_ctx->b_frame_strategy = 2;
  117. ff->cod_ctx->qcompress = 0.6f;
  118. ff->cod_ctx->qmin = 10;
  119. ff->cod_ctx->qmax = 51;
  120. ff->cod_ctx->max_qdiff = 4;
  121. ff->cod_ctx->max_b_frames = 3;
  122. ff->cod_ctx->refs = 5;
  123. ff->cod_ctx->directpred = 3;
  124. ff->cod_ctx->trellis = 1;
  125. ff->cod_ctx->flags2 |= CODEC_FLAG2_BPYRAMID | CODEC_FLAG2_MIXED_REFS
  126. | CODEC_FLAG2_WPRED | CODEC_FLAG2_8X8DCT
  127. | CODEC_FLAG2_FASTPSKIP;
  128. ff->cod_ctx->weighted_p_pred = 2;
  129. ff->cod_ctx->rc_lookahead = 50;
  130. }
  131. if (ff->fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  132. ff->cod_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
  133. if (av_set_parameters(ff->fmt_ctx, NULL) < 0)
  134. goto error;
  135. ff->codec = avcodec_find_encoder(ff->cod_ctx->codec_id);
  136. if (!ff->codec)
  137. goto error;
  138. if (avcodec_open(ff->cod_ctx, ff->codec) < 0)
  139. goto error;
  140. ff->frame = avcodec_alloc_frame();
  141. if (!ff->frame)
  142. goto error;
  143. tmp = (uint8_t *)av_malloc(avpicture_get_size(ff->cod_ctx->pix_fmt,
  144. ff->cod_ctx->width,
  145. ff->cod_ctx->height));
  146. if (!tmp)
  147. goto error;
  148. avpicture_fill((AVPicture *)ff->frame, tmp, ff->cod_ctx->pix_fmt,
  149. ff->cod_ctx->width, ff->cod_ctx->height);
  150. if (!(ff->fmt_ctx->flags & AVFMT_NOFILE))
  151. if (url_fopen(&ff->fmt_ctx->pb, file, URL_WRONLY) < 0)
  152. goto error;
  153. ff->buf_len = 64 * 1024 * 1024;
  154. ff->buf = (uint8_t *)av_malloc(ff->buf_len);
  155. av_write_header(ff->fmt_ctx);
  156. return seq;
  157. error:
  158. pipi_close_sequence(seq);
  159. return NULL;
  160. #else
  161. return NULL;
  162. #endif
  163. }
  164. int pipi_feed_sequence(pipi_sequence_t *seq, uint8_t const *buffer,
  165. int width, int height)
  166. {
  167. #if defined USE_FFMPEG
  168. AVPacket packet;
  169. size_t bytes;
  170. int pitch;
  171. ffmpeg_codec_t *ff = (ffmpeg_codec_t *)seq->codec_priv;
  172. if (ff->src_width != width || ff->src_height != height)
  173. {
  174. ff->src_width = width;
  175. ff->src_height = height;
  176. if (ff->sws_ctx)
  177. sws_freeContext(ff->sws_ctx);
  178. ff->sws_ctx = NULL;
  179. }
  180. if (!ff->sws_ctx)
  181. ff->sws_ctx = sws_getContext(width, height, PIX_FMT_RGB32,
  182. ff->cod_ctx->width,
  183. ff->cod_ctx->height,
  184. ff->cod_ctx->pix_fmt, SWS_BICUBIC,
  185. NULL, NULL, NULL);
  186. if (!ff->sws_ctx)
  187. return -1;
  188. pitch = width * 4;
  189. sws_scale(ff->sws_ctx, &buffer, &pitch, 0, height,
  190. ff->frame->data, ff->frame->linesize);
  191. bytes = avcodec_encode_video(ff->cod_ctx, ff->buf,
  192. ff->buf_len, ff->frame);
  193. if (bytes <= 0)
  194. return 0;
  195. av_init_packet(&packet);
  196. if (ff->cod_ctx->coded_frame->pts != 0x8000000000000000LL)
  197. packet.pts = av_rescale_q(ff->cod_ctx->coded_frame->pts,
  198. ff->cod_ctx->time_base, ff->stream->time_base);
  199. if (ff->cod_ctx->coded_frame->key_frame)
  200. packet.flags |= PKT_FLAG_KEY;
  201. packet.stream_index = 0;
  202. packet.data = ff->buf;
  203. packet.size = bytes;
  204. if (av_interleaved_write_frame(ff->fmt_ctx, &packet) < 0)
  205. return -1;
  206. #endif
  207. return 0;
  208. }
  209. int pipi_close_sequence(pipi_sequence_t *seq)
  210. {
  211. #if defined USE_FFMPEG
  212. ffmpeg_codec_t *ff = (ffmpeg_codec_t *)seq->codec_priv;
  213. if (ff->fmt_ctx)
  214. {
  215. av_write_trailer(ff->fmt_ctx);
  216. }
  217. if (ff->buf)
  218. {
  219. av_free(ff->buf);
  220. ff->buf = NULL;
  221. }
  222. if (ff->cod_ctx)
  223. {
  224. avcodec_close(ff->cod_ctx);
  225. ff->cod_ctx = NULL;
  226. }
  227. if (ff->frame)
  228. {
  229. av_free(ff->frame->data[0]);
  230. av_free(ff->frame);
  231. ff->frame = NULL;
  232. }
  233. if (ff->fmt_ctx)
  234. {
  235. av_freep(&ff->fmt_ctx->streams[0]->codec);
  236. ff->codec = NULL;
  237. av_freep(&ff->fmt_ctx->streams[0]);
  238. ff->stream = NULL;
  239. if (!(ff->fmt_ctx->flags & AVFMT_NOFILE))
  240. url_fclose(ff->fmt_ctx->pb);
  241. av_free(ff->fmt_ctx);
  242. ff->fmt_ctx = NULL;
  243. }
  244. if (ff->sws_ctx)
  245. {
  246. sws_freeContext(ff->sws_ctx);
  247. ff->sws_ctx = NULL;
  248. ff->src_width = ff->src_height = 0;
  249. }
  250. free(ff);
  251. free(seq);
  252. #endif
  253. return 0;
  254. }