output_example.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. #ifndef M_PI
  29. #define M_PI 3.1415926535897931
  30. #endif
  31. #include "avformat.h"
  32. /* 5 seconds stream duration */
  33. #define STREAM_DURATION 5.0
  34. /**************************************************************/
  35. /* audio output */
  36. float t, tincr, tincr2;
  37. int16_t *samples;
  38. uint8_t *audio_outbuf;
  39. int audio_outbuf_size;
  40. int audio_input_frame_size;
  41. /*
  42. * add an audio output stream
  43. */
  44. AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
  45. {
  46. AVCodecContext *c;
  47. AVStream *st;
  48. st = av_new_stream(oc, 1);
  49. if (!st) {
  50. fprintf(stderr, "Could not alloc stream\n");
  51. exit(1);
  52. }
  53. c = &st->codec;
  54. c->codec_id = codec_id;
  55. c->codec_type = CODEC_TYPE_AUDIO;
  56. /* put sample parameters */
  57. c->bit_rate = 64000;
  58. c->sample_rate = 44100;
  59. c->channels = 2;
  60. return st;
  61. }
  62. void open_audio(AVFormatContext *oc, AVStream *st)
  63. {
  64. AVCodecContext *c;
  65. AVCodec *codec;
  66. c = &st->codec;
  67. /* find the audio encoder */
  68. codec = avcodec_find_encoder(c->codec_id);
  69. if (!codec) {
  70. fprintf(stderr, "codec not found\n");
  71. exit(1);
  72. }
  73. /* open it */
  74. if (avcodec_open(c, codec) < 0) {
  75. fprintf(stderr, "could not open codec\n");
  76. exit(1);
  77. }
  78. /* init signal generator */
  79. t = 0;
  80. tincr = 2 * M_PI * 110.0 / c->sample_rate;
  81. /* increment frequency by 110 Hz per second */
  82. tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
  83. audio_outbuf_size = 10000;
  84. audio_outbuf = malloc(audio_outbuf_size);
  85. /* ugly hack for PCM codecs (will be removed ASAP with new PCM
  86. support to compute the input frame size in samples */
  87. if (c->frame_size <= 1) {
  88. audio_input_frame_size = audio_outbuf_size / c->channels;
  89. switch(st->codec.codec_id) {
  90. case CODEC_ID_PCM_S16LE:
  91. case CODEC_ID_PCM_S16BE:
  92. case CODEC_ID_PCM_U16LE:
  93. case CODEC_ID_PCM_U16BE:
  94. audio_input_frame_size >>= 1;
  95. break;
  96. default:
  97. break;
  98. }
  99. } else {
  100. audio_input_frame_size = c->frame_size;
  101. }
  102. samples = malloc(audio_input_frame_size * 2 * c->channels);
  103. }
  104. /* prepare a 16 bit dummy audio frame of 'frame_size' samples and
  105. 'nb_channels' channels */
  106. void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
  107. {
  108. int j, i, v;
  109. int16_t *q;
  110. q = samples;
  111. for(j=0;j<frame_size;j++) {
  112. v = (int)(sin(t) * 10000);
  113. for(i = 0; i < nb_channels; i++)
  114. *q++ = v;
  115. t += tincr;
  116. tincr += tincr2;
  117. }
  118. }
  119. void write_audio_frame(AVFormatContext *oc, AVStream *st)
  120. {
  121. int out_size;
  122. AVCodecContext *c;
  123. c = &st->codec;
  124. get_audio_frame(samples, audio_input_frame_size, c->channels);
  125. out_size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
  126. /* write the compressed frame in the media file */
  127. if (av_write_frame(oc, st->index, audio_outbuf, out_size) != 0) {
  128. fprintf(stderr, "Error while writing audio frame\n");
  129. exit(1);
  130. }
  131. }
  132. void close_audio(AVFormatContext *oc, AVStream *st)
  133. {
  134. avcodec_close(&st->codec);
  135. av_free(samples);
  136. av_free(audio_outbuf);
  137. }
  138. /**************************************************************/
  139. /* video output */
  140. AVFrame *picture, *tmp_picture;
  141. uint8_t *video_outbuf;
  142. int frame_count, video_outbuf_size;
  143. /* add a video output stream */
  144. AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
  145. {
  146. AVCodecContext *c;
  147. AVStream *st;
  148. st = av_new_stream(oc, 0);
  149. if (!st) {
  150. fprintf(stderr, "Could not alloc stream\n");
  151. exit(1);
  152. }
  153. c = &st->codec;
  154. c->codec_id = codec_id;
  155. c->codec_type = CODEC_TYPE_VIDEO;
  156. /* put sample parameters */
  157. c->bit_rate = 400000;
  158. /* resolution must be a multiple of two */
  159. c->width = 352;
  160. c->height = 288;
  161. /* frames per second */
  162. c->frame_rate = 25;
  163. c->frame_rate_base= 1;
  164. c->gop_size = 12; /* emit one intra frame every twelve frames */
  165. return st;
  166. }
  167. AVFrame *alloc_picture(int pix_fmt, int width, int height)
  168. {
  169. AVFrame *picture;
  170. uint8_t *picture_buf;
  171. int size;
  172. picture = avcodec_alloc_frame();
  173. if (!picture)
  174. return NULL;
  175. size = avpicture_get_size(pix_fmt, width, height);
  176. picture_buf = malloc(size);
  177. if (!picture_buf) {
  178. av_free(picture);
  179. return NULL;
  180. }
  181. avpicture_fill((AVPicture *)picture, picture_buf,
  182. pix_fmt, width, height);
  183. return picture;
  184. }
  185. void open_video(AVFormatContext *oc, AVStream *st)
  186. {
  187. AVCodec *codec;
  188. AVCodecContext *c;
  189. c = &st->codec;
  190. /* find the video encoder */
  191. codec = avcodec_find_encoder(c->codec_id);
  192. if (!codec) {
  193. fprintf(stderr, "codec not found\n");
  194. exit(1);
  195. }
  196. /* open the codec */
  197. if (avcodec_open(c, codec) < 0) {
  198. fprintf(stderr, "could not open codec\n");
  199. exit(1);
  200. }
  201. video_outbuf = NULL;
  202. if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
  203. /* allocate output buffer */
  204. /* XXX: API change will be done */
  205. video_outbuf_size = 200000;
  206. video_outbuf = malloc(video_outbuf_size);
  207. }
  208. /* allocate the encoded raw picture */
  209. picture = alloc_picture(c->pix_fmt, c->width, c->height);
  210. if (!picture) {
  211. fprintf(stderr, "Could not allocate picture\n");
  212. exit(1);
  213. }
  214. /* if the output format is not YUV420P, then a temporary YUV420P
  215. picture is needed too. It is then converted to the required
  216. output format */
  217. tmp_picture = NULL;
  218. if (c->pix_fmt != PIX_FMT_YUV420P) {
  219. tmp_picture = alloc_picture(PIX_FMT_YUV420P, c->width, c->height);
  220. if (!tmp_picture) {
  221. fprintf(stderr, "Could not allocate temporary picture\n");
  222. exit(1);
  223. }
  224. }
  225. }
  226. /* prepare a dummy image */
  227. void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
  228. {
  229. int x, y, i;
  230. i = frame_index;
  231. /* Y */
  232. for(y=0;y<height;y++) {
  233. for(x=0;x<width;x++) {
  234. pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
  235. }
  236. }
  237. /* Cb and Cr */
  238. for(y=0;y<height/2;y++) {
  239. for(x=0;x<width/2;x++) {
  240. pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
  241. pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
  242. }
  243. }
  244. }
  245. void write_video_frame(AVFormatContext *oc, AVStream *st)
  246. {
  247. int out_size, ret;
  248. AVCodecContext *c;
  249. c = &st->codec;
  250. if (c->pix_fmt != PIX_FMT_YUV420P) {
  251. /* as we only generate a YUV420P picture, we must convert it
  252. to the codec pixel format if needed */
  253. fill_yuv_image(tmp_picture, frame_count, c->width, c->height);
  254. img_convert((AVPicture *)picture, c->pix_fmt,
  255. (AVPicture *)tmp_picture, PIX_FMT_YUV420P,
  256. c->width, c->height);
  257. } else {
  258. fill_yuv_image(picture, frame_count, c->width, c->height);
  259. }
  260. if (oc->oformat->flags & AVFMT_RAWPICTURE) {
  261. /* raw video case. The API will change slightly in the near
  262. futur for that */
  263. ret = av_write_frame(oc, st->index,
  264. (uint8_t *)picture, sizeof(AVPicture));
  265. } else {
  266. /* encode the image */
  267. out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
  268. /* write the compressed frame in the media file */
  269. ret = av_write_frame(oc, st->index, video_outbuf, out_size);
  270. }
  271. if (ret != 0) {
  272. fprintf(stderr, "Error while writing video frame\n");
  273. exit(1);
  274. }
  275. frame_count++;
  276. }
  277. void close_video(AVFormatContext *oc, AVStream *st)
  278. {
  279. avcodec_close(&st->codec);
  280. av_free(picture->data[0]);
  281. av_free(picture);
  282. if (tmp_picture) {
  283. av_free(tmp_picture->data[0]);
  284. av_free(tmp_picture);
  285. }
  286. av_free(video_outbuf);
  287. }
  288. /**************************************************************/
  289. /* media file output */
  290. int main(int argc, char **argv)
  291. {
  292. const char *filename;
  293. AVOutputFormat *fmt;
  294. AVFormatContext *oc;
  295. AVStream *audio_st, *video_st;
  296. double audio_pts, video_pts;
  297. int i;
  298. /* initialize libavcodec, and register all codecs and formats */
  299. av_register_all();
  300. if (argc != 2) {
  301. printf("usage: %s output_file\n"
  302. "API example program to output a media file with libavformat.\n"
  303. "The output format is automatically guessed according to the file extension.\n"
  304. "Raw images can also be output by using '%%d' in the filename\n"
  305. "\n", argv[0]);
  306. exit(1);
  307. }
  308. filename = argv[1];
  309. /* auto detect the output format from the name. default is
  310. mpeg. */
  311. fmt = guess_format(NULL, filename, NULL);
  312. if (!fmt) {
  313. printf("Could not deduce output format from file extension: using MPEG.\n");
  314. fmt = guess_format("mpeg", NULL, NULL);
  315. }
  316. if (!fmt) {
  317. fprintf(stderr, "Could not find suitable output format\n");
  318. exit(1);
  319. }
  320. /* allocate the output media context */
  321. oc = av_mallocz(sizeof(AVFormatContext));
  322. if (!oc) {
  323. fprintf(stderr, "Memory error\n");
  324. exit(1);
  325. }
  326. oc->oformat = fmt;
  327. snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
  328. /* add the audio and video streams using the default format codecs
  329. and initialize the codecs */
  330. video_st = NULL;
  331. audio_st = NULL;
  332. if (fmt->video_codec != CODEC_ID_NONE) {
  333. video_st = add_video_stream(oc, fmt->video_codec);
  334. }
  335. if (fmt->audio_codec != CODEC_ID_NONE) {
  336. audio_st = add_audio_stream(oc, fmt->audio_codec);
  337. }
  338. /* set the output parameters (must be done even if no
  339. parameters). */
  340. if (av_set_parameters(oc, NULL) < 0) {
  341. fprintf(stderr, "Invalid output format parameters\n");
  342. exit(1);
  343. }
  344. dump_format(oc, 0, filename, 1);
  345. /* now that all the parameters are set, we can open the audio and
  346. video codecs and allocate the necessary encode buffers */
  347. if (video_st)
  348. open_video(oc, video_st);
  349. if (audio_st)
  350. open_audio(oc, audio_st);
  351. /* open the output file, if needed */
  352. if (!(fmt->flags & AVFMT_NOFILE)) {
  353. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  354. fprintf(stderr, "Could not open '%s'\n", filename);
  355. exit(1);
  356. }
  357. }
  358. /* write the stream header, if any */
  359. av_write_header(oc);
  360. for(;;) {
  361. /* compute current audio and video time */
  362. if (audio_st)
  363. audio_pts = (double)audio_st->pts.val * oc->pts_num / oc->pts_den;
  364. else
  365. audio_pts = 0.0;
  366. if (video_st)
  367. video_pts = (double)video_st->pts.val * oc->pts_num / oc->pts_den;
  368. else
  369. video_pts = 0.0;
  370. if ((!audio_st || audio_pts >= STREAM_DURATION) &&
  371. (!video_st || video_pts >= STREAM_DURATION))
  372. break;
  373. /* write interleaved audio and video frames */
  374. if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
  375. write_audio_frame(oc, audio_st);
  376. } else {
  377. write_video_frame(oc, video_st);
  378. }
  379. }
  380. /* close each codec */
  381. if (video_st)
  382. close_video(oc, video_st);
  383. if (audio_st)
  384. close_audio(oc, audio_st);
  385. /* write the trailer, if any */
  386. av_write_trailer(oc);
  387. /* free the streams */
  388. for(i = 0; i < oc->nb_streams; i++) {
  389. av_freep(&oc->streams[i]);
  390. }
  391. if (!(fmt->flags & AVFMT_NOFILE)) {
  392. /* close the output file */
  393. url_fclose(&oc->pb);
  394. }
  395. /* free the stream */
  396. av_free(oc);
  397. return 0;
  398. }