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.

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