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.

351 lines
9.4KB

  1. /*
  2. * Libavformat API example: Output a media file in any supported
  3. * libavformat format. The default codecs are used.
  4. *
  5. * Copyright (c) 2003 Fabrice Bellard
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include "avformat.h"
  29. /* 5 seconds stream duration */
  30. #define STREAM_DURATION 5.0
  31. /**************************************************************/
  32. /* audio output */
  33. float t, tincr;
  34. int16_t *samples;
  35. uint8_t *audio_outbuf;
  36. int audio_outbuf_size;
  37. int audio_input_frame_size;
  38. /*
  39. * add an audio output stream
  40. */
  41. AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
  42. {
  43. AVCodec *codec;
  44. AVCodecContext *c;
  45. AVStream *st;
  46. st = av_new_stream(oc, 1);
  47. if (!st) {
  48. fprintf(stderr, "Could not alloc stream\n");
  49. exit(1);
  50. }
  51. /* find the MP2 encoder */
  52. codec = avcodec_find_encoder(codec_id);
  53. if (!codec) {
  54. fprintf(stderr, "codec not found\n");
  55. exit(1);
  56. }
  57. c = &st->codec;
  58. avcodec_get_context_defaults(c);
  59. c->codec_type = CODEC_TYPE_AUDIO;
  60. /* put sample parameters */
  61. c->bit_rate = 64000;
  62. c->sample_rate = 44100;
  63. c->channels = 2;
  64. /* open it */
  65. if (avcodec_open(c, codec) < 0) {
  66. fprintf(stderr, "could not open codec\n");
  67. exit(1);
  68. }
  69. /* init signal generator */
  70. t = 0;
  71. tincr = 2 * M_PI * 440.0 / c->sample_rate;
  72. audio_outbuf_size = 10000;
  73. audio_outbuf = malloc(audio_outbuf_size);
  74. /* ugly hack for PCM codecs (will be removed ASAP with new PCM
  75. support to compute the input frame size in samples */
  76. if (c->frame_size <= 1) {
  77. audio_input_frame_size = audio_outbuf_size / c->channels;
  78. switch(st->codec.codec_id) {
  79. case CODEC_ID_PCM_S16LE:
  80. case CODEC_ID_PCM_S16BE:
  81. case CODEC_ID_PCM_U16LE:
  82. case CODEC_ID_PCM_U16BE:
  83. audio_input_frame_size >>= 1;
  84. break;
  85. default:
  86. break;
  87. }
  88. } else {
  89. audio_input_frame_size = c->frame_size;
  90. }
  91. samples = malloc(audio_input_frame_size * 2 * c->channels);
  92. return st;
  93. }
  94. void write_audio_frame(AVFormatContext *oc, AVStream *st)
  95. {
  96. int j, out_size;
  97. AVCodecContext *c;
  98. c = &st->codec;
  99. for(j=0;j<audio_input_frame_size;j++) {
  100. samples[2*j] = (int)(sin(t) * 10000);
  101. samples[2*j+1] = samples[2*j];
  102. t += tincr;
  103. }
  104. out_size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
  105. /* write the compressed frame in the media file */
  106. if (av_write_frame(oc, st->index, audio_outbuf, out_size) != 0) {
  107. fprintf(stderr, "Error while writing audio frame\n");
  108. exit(1);
  109. }
  110. }
  111. /**************************************************************/
  112. /* video output */
  113. AVFrame *picture;
  114. uint8_t *video_outbuf;
  115. int frame_count, video_outbuf_size;
  116. /* add a video output stream */
  117. AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
  118. {
  119. AVCodec *codec;
  120. AVCodecContext *c;
  121. AVStream *st;
  122. uint8_t *picture_buf;
  123. int size;
  124. st = av_new_stream(oc, 0);
  125. if (!st) {
  126. fprintf(stderr, "Could not alloc stream\n");
  127. exit(1);
  128. }
  129. /* find the mpeg1 video encoder */
  130. codec = avcodec_find_encoder(codec_id);
  131. if (!codec) {
  132. fprintf(stderr, "codec not found\n");
  133. exit(1);
  134. }
  135. c = &st->codec;
  136. avcodec_get_context_defaults(c);
  137. c->codec_type = CODEC_TYPE_VIDEO;
  138. /* put sample parameters */
  139. c->bit_rate = 400000;
  140. /* resolution must be a multiple of two */
  141. c->width = 352;
  142. c->height = 288;
  143. /* frames per second */
  144. c->frame_rate = 25;
  145. c->frame_rate_base= 1;
  146. c->gop_size = 12; /* emit one intra frame every twelve frames */
  147. /* open it */
  148. if (avcodec_open(c, codec) < 0) {
  149. fprintf(stderr, "could not open codec\n");
  150. exit(1);
  151. }
  152. /* alloc various buffers */
  153. picture= avcodec_alloc_frame();
  154. video_outbuf_size = 100000;
  155. video_outbuf = malloc(video_outbuf_size);
  156. size = c->width * c->height;
  157. picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
  158. picture->data[0] = picture_buf;
  159. picture->data[1] = picture->data[0] + size;
  160. picture->data[2] = picture->data[1] + size / 4;
  161. picture->linesize[0] = c->width;
  162. picture->linesize[1] = c->width / 2;
  163. picture->linesize[2] = c->width / 2;
  164. return st;
  165. }
  166. void write_video_frame(AVFormatContext *oc, AVStream *st)
  167. {
  168. int x, y, i, out_size;
  169. AVCodecContext *c;
  170. c = &st->codec;
  171. /* prepare a dummy image */
  172. /* Y */
  173. i = frame_count++;
  174. for(y=0;y<c->height;y++) {
  175. for(x=0;x<c->width;x++) {
  176. picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
  177. }
  178. }
  179. /* Cb and Cr */
  180. for(y=0;y<c->height/2;y++) {
  181. for(x=0;x<c->width/2;x++) {
  182. picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
  183. picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
  184. }
  185. }
  186. /* encode the image */
  187. out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
  188. /* write the compressed frame in the media file */
  189. if (av_write_frame(oc, st->index, video_outbuf, out_size) != 0) {
  190. fprintf(stderr, "Error while writing video frame\n");
  191. exit(1);
  192. }
  193. }
  194. /**************************************************************/
  195. /* media file output */
  196. int main(int argc, char **argv)
  197. {
  198. const char *filename;
  199. AVOutputFormat *fmt;
  200. AVFormatContext *oc;
  201. AVStream *st, *audio_st, *video_st;
  202. int i;
  203. double audio_pts, video_pts;
  204. /* initialize libavcodec, and register all codecs and formats */
  205. av_register_all();
  206. if (argc != 2) {
  207. printf("usage: %s output_file\n"
  208. "API example program for to output media file with libavformat\n"
  209. "\n", argv[0]);
  210. exit(1);
  211. }
  212. filename = argv[1];
  213. /* auto detect the output format from the name. default is
  214. mpeg. */
  215. fmt = guess_format(NULL, filename, NULL);
  216. if (!fmt) {
  217. printf("Could not deduce output format from file extension: using MPEG.\n");
  218. fmt = guess_format("mpeg", NULL, NULL);
  219. }
  220. if (!fmt) {
  221. fprintf(stderr, "Could not find suitable output format\n");
  222. exit(1);
  223. }
  224. /* allocate the output media context */
  225. oc = av_mallocz(sizeof(AVFormatContext));
  226. if (!oc) {
  227. fprintf(stderr, "Memory error\n");
  228. exit(1);
  229. }
  230. oc->oformat = fmt;
  231. /* add the audio and video streams using the default format codecs
  232. and initialize the codecs */
  233. video_st = NULL;
  234. audio_st = NULL;
  235. if (fmt->video_codec != CODEC_ID_NONE) {
  236. video_st = add_video_stream(oc, fmt->video_codec);
  237. }
  238. if (fmt->audio_codec != CODEC_ID_NONE) {
  239. audio_st = add_audio_stream(oc, fmt->audio_codec);
  240. }
  241. dump_format(oc, 0, filename, 1);
  242. /* open the output file, if needed */
  243. if (!(fmt->flags & AVFMT_NOFILE)) {
  244. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  245. fprintf(stderr, "Could not open '%s'\n", filename);
  246. exit(1);
  247. }
  248. }
  249. /* set the output parameters (must be done even if no parameters) */
  250. av_set_parameters(oc, NULL);
  251. /* write the stream header, if any */
  252. av_write_header(oc);
  253. for(;;) {
  254. /* compute current audio and video time */
  255. if (audio_st)
  256. audio_pts = (double)audio_st->pts.val * oc->pts_num / oc->pts_den;
  257. else
  258. audio_pts = 0.0;
  259. if (video_st)
  260. video_pts = (double)video_st->pts.val * oc->pts_num / oc->pts_den;
  261. else
  262. video_pts = 0.0;
  263. if ((!audio_st || audio_pts >= STREAM_DURATION) &&
  264. (!video_st || video_pts >= STREAM_DURATION))
  265. break;
  266. /* write interleaved audio and video frames */
  267. if (!video_st || (video_st && audio_pts < video_pts)) {
  268. write_audio_frame(oc, audio_st);
  269. } else {
  270. write_video_frame(oc, video_st);
  271. }
  272. }
  273. /* close each codec */
  274. for(i = 0;i < oc->nb_streams; i++) {
  275. st = oc->streams[i];
  276. avcodec_close(&st->codec);
  277. }
  278. /* write the trailer, if any */
  279. av_write_trailer(oc);
  280. if (!(fmt->flags & AVFMT_NOFILE)) {
  281. /* close the output file */
  282. url_fclose(&oc->pb);
  283. }
  284. /* free the stream */
  285. av_free(oc);
  286. return 0;
  287. }