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.

664 lines
20KB

  1. /*
  2. * Copyright (c) 2003 Fabrice Bellard
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file
  24. * libavformat API example.
  25. *
  26. * @example output.c
  27. * Output a media file in any supported libavformat format. The default
  28. * codecs are used.
  29. */
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <math.h>
  34. #include "libavutil/channel_layout.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/opt.h"
  37. #include "libavformat/avformat.h"
  38. #include "libavresample/avresample.h"
  39. #include "libswscale/swscale.h"
  40. /* 5 seconds stream duration */
  41. #define STREAM_DURATION 5.0
  42. #define STREAM_FRAME_RATE 25 /* 25 images/s */
  43. #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
  44. #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
  45. #define SCALE_FLAGS SWS_BICUBIC
  46. // a wrapper around a single output AVStream
  47. typedef struct OutputStream {
  48. AVStream *st;
  49. /* pts of the next frame that will be generated */
  50. int64_t next_pts;
  51. AVFrame *frame;
  52. AVFrame *tmp_frame;
  53. float t, tincr, tincr2;
  54. struct SwsContext *sws_ctx;
  55. AVAudioResampleContext *avr;
  56. } OutputStream;
  57. /**************************************************************/
  58. /* audio output */
  59. /*
  60. * add an audio output stream
  61. */
  62. static void add_audio_stream(OutputStream *ost, AVFormatContext *oc,
  63. enum AVCodecID codec_id)
  64. {
  65. AVCodecContext *c;
  66. AVCodec *codec;
  67. int ret;
  68. /* find the audio encoder */
  69. codec = avcodec_find_encoder(codec_id);
  70. if (!codec) {
  71. fprintf(stderr, "codec not found\n");
  72. exit(1);
  73. }
  74. ost->st = avformat_new_stream(oc, codec);
  75. if (!ost->st) {
  76. fprintf(stderr, "Could not alloc stream\n");
  77. exit(1);
  78. }
  79. c = ost->st->codec;
  80. /* put sample parameters */
  81. c->sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_S16;
  82. c->sample_rate = codec->supported_samplerates ? codec->supported_samplerates[0] : 44100;
  83. c->channel_layout = codec->channel_layouts ? codec->channel_layouts[0] : AV_CH_LAYOUT_STEREO;
  84. c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
  85. c->bit_rate = 64000;
  86. ost->st->time_base = (AVRational){ 1, c->sample_rate };
  87. // some formats want stream headers to be separate
  88. if (oc->oformat->flags & AVFMT_GLOBALHEADER)
  89. c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  90. /* initialize sample format conversion;
  91. * to simplify the code, we always pass the data through lavr, even
  92. * if the encoder supports the generated format directly -- the price is
  93. * some extra data copying;
  94. */
  95. ost->avr = avresample_alloc_context();
  96. if (!ost->avr) {
  97. fprintf(stderr, "Error allocating the resampling context\n");
  98. exit(1);
  99. }
  100. av_opt_set_int(ost->avr, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
  101. av_opt_set_int(ost->avr, "in_sample_rate", 44100, 0);
  102. av_opt_set_int(ost->avr, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
  103. av_opt_set_int(ost->avr, "out_sample_fmt", c->sample_fmt, 0);
  104. av_opt_set_int(ost->avr, "out_sample_rate", c->sample_rate, 0);
  105. av_opt_set_int(ost->avr, "out_channel_layout", c->channel_layout, 0);
  106. ret = avresample_open(ost->avr);
  107. if (ret < 0) {
  108. fprintf(stderr, "Error opening the resampling context\n");
  109. exit(1);
  110. }
  111. }
  112. static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,
  113. uint64_t channel_layout,
  114. int sample_rate, int nb_samples)
  115. {
  116. AVFrame *frame = av_frame_alloc();
  117. int ret;
  118. if (!frame) {
  119. fprintf(stderr, "Error allocating an audio frame\n");
  120. exit(1);
  121. }
  122. frame->format = sample_fmt;
  123. frame->channel_layout = channel_layout;
  124. frame->sample_rate = sample_rate;
  125. frame->nb_samples = nb_samples;
  126. if (nb_samples) {
  127. ret = av_frame_get_buffer(frame, 0);
  128. if (ret < 0) {
  129. fprintf(stderr, "Error allocating an audio buffer\n");
  130. exit(1);
  131. }
  132. }
  133. return frame;
  134. }
  135. static void open_audio(AVFormatContext *oc, OutputStream *ost)
  136. {
  137. AVCodecContext *c;
  138. int nb_samples;
  139. c = ost->st->codec;
  140. /* open it */
  141. if (avcodec_open2(c, NULL, NULL) < 0) {
  142. fprintf(stderr, "could not open codec\n");
  143. exit(1);
  144. }
  145. /* init signal generator */
  146. ost->t = 0;
  147. ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
  148. /* increment frequency by 110 Hz per second */
  149. ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
  150. if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  151. nb_samples = 10000;
  152. else
  153. nb_samples = c->frame_size;
  154. ost->frame = alloc_audio_frame(c->sample_fmt, c->channel_layout,
  155. c->sample_rate, nb_samples);
  156. ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, AV_CH_LAYOUT_STEREO,
  157. 44100, nb_samples);
  158. }
  159. /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
  160. * 'nb_channels' channels. */
  161. static AVFrame *get_audio_frame(OutputStream *ost)
  162. {
  163. AVFrame *frame = ost->tmp_frame;
  164. int j, i, v;
  165. int16_t *q = (int16_t*)frame->data[0];
  166. /* check if we want to generate more frames */
  167. if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
  168. STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
  169. return NULL;
  170. for (j = 0; j < frame->nb_samples; j++) {
  171. v = (int)(sin(ost->t) * 10000);
  172. for (i = 0; i < ost->st->codec->channels; i++)
  173. *q++ = v;
  174. ost->t += ost->tincr;
  175. ost->tincr += ost->tincr2;
  176. }
  177. return frame;
  178. }
  179. /* if a frame is provided, send it to the encoder, otherwise flush the encoder;
  180. * return 1 when encoding is finished, 0 otherwise
  181. */
  182. static int encode_audio_frame(AVFormatContext *oc, OutputStream *ost,
  183. AVFrame *frame)
  184. {
  185. AVPacket pkt = { 0 }; // data and size must be 0;
  186. int got_packet;
  187. av_init_packet(&pkt);
  188. avcodec_encode_audio2(ost->st->codec, &pkt, frame, &got_packet);
  189. if (got_packet) {
  190. pkt.stream_index = ost->st->index;
  191. av_packet_rescale_ts(&pkt, ost->st->codec->time_base, ost->st->time_base);
  192. /* Write the compressed frame to the media file. */
  193. if (av_interleaved_write_frame(oc, &pkt) != 0) {
  194. fprintf(stderr, "Error while writing audio frame\n");
  195. exit(1);
  196. }
  197. }
  198. return (frame || got_packet) ? 0 : 1;
  199. }
  200. /*
  201. * encode one audio frame and send it to the muxer
  202. * return 1 when encoding is finished, 0 otherwise
  203. */
  204. static int process_audio_stream(AVFormatContext *oc, OutputStream *ost)
  205. {
  206. AVFrame *frame;
  207. int got_output = 0;
  208. int ret;
  209. frame = get_audio_frame(ost);
  210. got_output |= !!frame;
  211. /* feed the data to lavr */
  212. if (frame) {
  213. ret = avresample_convert(ost->avr, NULL, 0, 0,
  214. frame->extended_data, frame->linesize[0],
  215. frame->nb_samples);
  216. if (ret < 0) {
  217. fprintf(stderr, "Error feeding audio data to the resampler\n");
  218. exit(1);
  219. }
  220. }
  221. while ((frame && avresample_available(ost->avr) >= ost->frame->nb_samples) ||
  222. (!frame && avresample_get_out_samples(ost->avr, 0))) {
  223. /* when we pass a frame to the encoder, it may keep a reference to it
  224. * internally;
  225. * make sure we do not overwrite it here
  226. */
  227. ret = av_frame_make_writable(ost->frame);
  228. if (ret < 0)
  229. exit(1);
  230. /* the difference between the two avresample calls here is that the
  231. * first one just reads the already converted data that is buffered in
  232. * the lavr output buffer, while the second one also flushes the
  233. * resampler */
  234. if (frame) {
  235. ret = avresample_read(ost->avr, ost->frame->extended_data,
  236. ost->frame->nb_samples);
  237. } else {
  238. ret = avresample_convert(ost->avr, ost->frame->extended_data,
  239. ost->frame->linesize[0], ost->frame->nb_samples,
  240. NULL, 0, 0);
  241. }
  242. if (ret < 0) {
  243. fprintf(stderr, "Error while resampling\n");
  244. exit(1);
  245. } else if (frame && ret != ost->frame->nb_samples) {
  246. fprintf(stderr, "Too few samples returned from lavr\n");
  247. exit(1);
  248. }
  249. ost->frame->nb_samples = ret;
  250. ost->frame->pts = ost->next_pts;
  251. ost->next_pts += ost->frame->nb_samples;
  252. got_output |= encode_audio_frame(oc, ost, ret ? ost->frame : NULL);
  253. }
  254. return !got_output;
  255. }
  256. /**************************************************************/
  257. /* video output */
  258. /* Add a video output stream. */
  259. static void add_video_stream(OutputStream *ost, AVFormatContext *oc,
  260. enum AVCodecID codec_id)
  261. {
  262. AVCodecContext *c;
  263. AVCodec *codec;
  264. /* find the video encoder */
  265. codec = avcodec_find_encoder(codec_id);
  266. if (!codec) {
  267. fprintf(stderr, "codec not found\n");
  268. exit(1);
  269. }
  270. ost->st = avformat_new_stream(oc, codec);
  271. if (!ost->st) {
  272. fprintf(stderr, "Could not alloc stream\n");
  273. exit(1);
  274. }
  275. c = ost->st->codec;
  276. /* Put sample parameters. */
  277. c->bit_rate = 400000;
  278. /* Resolution must be a multiple of two. */
  279. c->width = 352;
  280. c->height = 288;
  281. /* timebase: This is the fundamental unit of time (in seconds) in terms
  282. * of which frame timestamps are represented. For fixed-fps content,
  283. * timebase should be 1/framerate and timestamp increments should be
  284. * identical to 1. */
  285. ost->st->time_base = (AVRational){ 1, STREAM_FRAME_RATE };
  286. c->time_base = ost->st->time_base;
  287. c->gop_size = 12; /* emit one intra frame every twelve frames at most */
  288. c->pix_fmt = STREAM_PIX_FMT;
  289. if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  290. /* just for testing, we also add B frames */
  291. c->max_b_frames = 2;
  292. }
  293. if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  294. /* Needed to avoid using macroblocks in which some coeffs overflow.
  295. * This does not happen with normal video, it just happens here as
  296. * the motion of the chroma plane does not match the luma plane. */
  297. c->mb_decision = 2;
  298. }
  299. /* Some formats want stream headers to be separate. */
  300. if (oc->oformat->flags & AVFMT_GLOBALHEADER)
  301. c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  302. }
  303. static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
  304. {
  305. AVFrame *picture;
  306. int ret;
  307. picture = av_frame_alloc();
  308. if (!picture)
  309. return NULL;
  310. picture->format = pix_fmt;
  311. picture->width = width;
  312. picture->height = height;
  313. /* allocate the buffers for the frame data */
  314. ret = av_frame_get_buffer(picture, 32);
  315. if (ret < 0) {
  316. fprintf(stderr, "Could not allocate frame data.\n");
  317. exit(1);
  318. }
  319. return picture;
  320. }
  321. static void open_video(AVFormatContext *oc, OutputStream *ost)
  322. {
  323. AVCodecContext *c;
  324. c = ost->st->codec;
  325. /* open the codec */
  326. if (avcodec_open2(c, NULL, NULL) < 0) {
  327. fprintf(stderr, "could not open codec\n");
  328. exit(1);
  329. }
  330. /* Allocate the encoded raw picture. */
  331. ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
  332. if (!ost->frame) {
  333. fprintf(stderr, "Could not allocate picture\n");
  334. exit(1);
  335. }
  336. /* If the output format is not YUV420P, then a temporary YUV420P
  337. * picture is needed too. It is then converted to the required
  338. * output format. */
  339. ost->tmp_frame = NULL;
  340. if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
  341. ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
  342. if (!ost->tmp_frame) {
  343. fprintf(stderr, "Could not allocate temporary picture\n");
  344. exit(1);
  345. }
  346. }
  347. }
  348. /* Prepare a dummy image. */
  349. static void fill_yuv_image(AVFrame *pict, int frame_index,
  350. int width, int height)
  351. {
  352. int x, y, i, ret;
  353. /* when we pass a frame to the encoder, it may keep a reference to it
  354. * internally;
  355. * make sure we do not overwrite it here
  356. */
  357. ret = av_frame_make_writable(pict);
  358. if (ret < 0)
  359. exit(1);
  360. i = frame_index;
  361. /* Y */
  362. for (y = 0; y < height; y++)
  363. for (x = 0; x < width; x++)
  364. pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
  365. /* Cb and Cr */
  366. for (y = 0; y < height / 2; y++) {
  367. for (x = 0; x < width / 2; x++) {
  368. pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
  369. pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
  370. }
  371. }
  372. }
  373. static AVFrame *get_video_frame(OutputStream *ost)
  374. {
  375. AVCodecContext *c = ost->st->codec;
  376. /* check if we want to generate more frames */
  377. if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
  378. STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
  379. return NULL;
  380. if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
  381. /* as we only generate a YUV420P picture, we must convert it
  382. * to the codec pixel format if needed */
  383. if (!ost->sws_ctx) {
  384. ost->sws_ctx = sws_getContext(c->width, c->height,
  385. AV_PIX_FMT_YUV420P,
  386. c->width, c->height,
  387. c->pix_fmt,
  388. SCALE_FLAGS, NULL, NULL, NULL);
  389. if (!ost->sws_ctx) {
  390. fprintf(stderr,
  391. "Cannot initialize the conversion context\n");
  392. exit(1);
  393. }
  394. }
  395. fill_yuv_image(ost->tmp_frame, ost->next_pts, c->width, c->height);
  396. sws_scale(ost->sws_ctx, ost->tmp_frame->data, ost->tmp_frame->linesize,
  397. 0, c->height, ost->frame->data, ost->frame->linesize);
  398. } else {
  399. fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
  400. }
  401. ost->frame->pts = ost->next_pts++;
  402. return ost->frame;
  403. }
  404. /*
  405. * encode one video frame and send it to the muxer
  406. * return 1 when encoding is finished, 0 otherwise
  407. */
  408. static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
  409. {
  410. int ret;
  411. AVCodecContext *c;
  412. AVFrame *frame;
  413. int got_packet = 0;
  414. c = ost->st->codec;
  415. frame = get_video_frame(ost);
  416. if (oc->oformat->flags & AVFMT_RAWPICTURE) {
  417. /* a hack to avoid data copy with some raw video muxers */
  418. AVPacket pkt;
  419. av_init_packet(&pkt);
  420. if (!frame)
  421. return 1;
  422. pkt.flags |= AV_PKT_FLAG_KEY;
  423. pkt.stream_index = ost->st->index;
  424. pkt.data = (uint8_t *)frame;
  425. pkt.size = sizeof(AVPicture);
  426. pkt.pts = pkt.dts = frame->pts;
  427. av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
  428. ret = av_interleaved_write_frame(oc, &pkt);
  429. } else {
  430. AVPacket pkt = { 0 };
  431. av_init_packet(&pkt);
  432. /* encode the image */
  433. ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
  434. if (ret < 0) {
  435. fprintf(stderr, "Error encoding a video frame\n");
  436. exit(1);
  437. }
  438. if (got_packet) {
  439. av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
  440. pkt.stream_index = ost->st->index;
  441. /* Write the compressed frame to the media file. */
  442. ret = av_interleaved_write_frame(oc, &pkt);
  443. }
  444. }
  445. if (ret != 0) {
  446. fprintf(stderr, "Error while writing video frame\n");
  447. exit(1);
  448. }
  449. return (frame || got_packet) ? 0 : 1;
  450. }
  451. static void close_stream(AVFormatContext *oc, OutputStream *ost)
  452. {
  453. avcodec_close(ost->st->codec);
  454. av_frame_free(&ost->frame);
  455. av_frame_free(&ost->tmp_frame);
  456. sws_freeContext(ost->sws_ctx);
  457. avresample_free(&ost->avr);
  458. }
  459. /**************************************************************/
  460. /* media file output */
  461. int main(int argc, char **argv)
  462. {
  463. OutputStream video_st = { 0 }, audio_st = { 0 };
  464. const char *filename;
  465. AVOutputFormat *fmt;
  466. AVFormatContext *oc;
  467. int have_video = 0, have_audio = 0;
  468. int encode_video = 0, encode_audio = 0;
  469. /* Initialize libavcodec, and register all codecs and formats. */
  470. av_register_all();
  471. if (argc != 2) {
  472. printf("usage: %s output_file\n"
  473. "API example program to output a media file with libavformat.\n"
  474. "The output format is automatically guessed according to the file extension.\n"
  475. "Raw images can also be output by using '%%d' in the filename\n"
  476. "\n", argv[0]);
  477. return 1;
  478. }
  479. filename = argv[1];
  480. /* Autodetect the output format from the name. default is MPEG. */
  481. fmt = av_guess_format(NULL, filename, NULL);
  482. if (!fmt) {
  483. printf("Could not deduce output format from file extension: using MPEG.\n");
  484. fmt = av_guess_format("mpeg", NULL, NULL);
  485. }
  486. if (!fmt) {
  487. fprintf(stderr, "Could not find suitable output format\n");
  488. return 1;
  489. }
  490. /* Allocate the output media context. */
  491. oc = avformat_alloc_context();
  492. if (!oc) {
  493. fprintf(stderr, "Memory error\n");
  494. return 1;
  495. }
  496. oc->oformat = fmt;
  497. snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
  498. /* Add the audio and video streams using the default format codecs
  499. * and initialize the codecs. */
  500. if (fmt->video_codec != AV_CODEC_ID_NONE) {
  501. add_video_stream(&video_st, oc, fmt->video_codec);
  502. have_video = 1;
  503. encode_video = 1;
  504. }
  505. if (fmt->audio_codec != AV_CODEC_ID_NONE) {
  506. add_audio_stream(&audio_st, oc, fmt->audio_codec);
  507. have_audio = 1;
  508. encode_audio = 1;
  509. }
  510. /* Now that all the parameters are set, we can open the audio and
  511. * video codecs and allocate the necessary encode buffers. */
  512. if (have_video)
  513. open_video(oc, &video_st);
  514. if (have_audio)
  515. open_audio(oc, &audio_st);
  516. av_dump_format(oc, 0, filename, 1);
  517. /* open the output file, if needed */
  518. if (!(fmt->flags & AVFMT_NOFILE)) {
  519. if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) {
  520. fprintf(stderr, "Could not open '%s'\n", filename);
  521. return 1;
  522. }
  523. }
  524. /* Write the stream header, if any. */
  525. avformat_write_header(oc, NULL);
  526. while (encode_video || encode_audio) {
  527. /* select the stream to encode */
  528. if (encode_video &&
  529. (!encode_audio || av_compare_ts(video_st.next_pts, video_st.st->codec->time_base,
  530. audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
  531. encode_video = !write_video_frame(oc, &video_st);
  532. } else {
  533. encode_audio = !process_audio_stream(oc, &audio_st);
  534. }
  535. }
  536. /* Write the trailer, if any. The trailer must be written before you
  537. * close the CodecContexts open when you wrote the header; otherwise
  538. * av_write_trailer() may try to use memory that was freed on
  539. * av_codec_close(). */
  540. av_write_trailer(oc);
  541. /* Close each codec. */
  542. if (have_video)
  543. close_stream(oc, &video_st);
  544. if (have_audio)
  545. close_stream(oc, &audio_st);
  546. if (!(fmt->flags & AVFMT_NOFILE))
  547. /* Close the output file. */
  548. avio_close(oc->pb);
  549. /* free the stream */
  550. avformat_free_context(oc);
  551. return 0;
  552. }