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.

472 lines
13KB

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