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.

345 lines
9.3KB

  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. c->codec_type = CODEC_TYPE_AUDIO;
  59. /* put sample parameters */
  60. c->bit_rate = 64000;
  61. c->sample_rate = 44100;
  62. c->channels = 2;
  63. /* open it */
  64. if (avcodec_open(c, codec) < 0) {
  65. fprintf(stderr, "could not open codec\n");
  66. exit(1);
  67. }
  68. /* init signal generator */
  69. t = 0;
  70. tincr = 2 * M_PI * 440.0 / c->sample_rate;
  71. audio_outbuf_size = 10000;
  72. audio_outbuf = malloc(audio_outbuf_size);
  73. /* ugly hack for PCM codecs (will be removed ASAP with new PCM
  74. support to compute the input frame size in samples */
  75. if (c->frame_size <= 1) {
  76. audio_input_frame_size = audio_outbuf_size / c->channels;
  77. switch(st->codec.codec_id) {
  78. case CODEC_ID_PCM_S16LE:
  79. case CODEC_ID_PCM_S16BE:
  80. case CODEC_ID_PCM_U16LE:
  81. case CODEC_ID_PCM_U16BE:
  82. audio_input_frame_size >>= 1;
  83. break;
  84. default:
  85. break;
  86. }
  87. } else {
  88. audio_input_frame_size = c->frame_size;
  89. }
  90. samples = malloc(audio_input_frame_size * 2 * c->channels);
  91. return st;
  92. }
  93. void write_audio_frame(AVFormatContext *oc, AVStream *st)
  94. {
  95. int j, out_size;
  96. AVCodecContext *c;
  97. c = &st->codec;
  98. for(j=0;j<audio_input_frame_size;j++) {
  99. samples[2*j] = (int)(sin(t) * 10000);
  100. samples[2*j+1] = samples[2*j];
  101. t += tincr;
  102. }
  103. out_size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
  104. /* write the compressed frame in the media file */
  105. if (av_write_frame(oc, st->index, audio_outbuf, out_size) != 0) {
  106. fprintf(stderr, "Error while writing audio frame\n");
  107. exit(1);
  108. }
  109. }
  110. /**************************************************************/
  111. /* video output */
  112. AVFrame *picture;
  113. uint8_t *video_outbuf;
  114. int frame_count, video_outbuf_size;
  115. /* add a video output stream */
  116. AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
  117. {
  118. AVCodec *codec;
  119. AVCodecContext *c;
  120. AVStream *st;
  121. uint8_t *picture_buf;
  122. int size;
  123. st = av_new_stream(oc, 0);
  124. if (!st) {
  125. fprintf(stderr, "Could not alloc stream\n");
  126. exit(1);
  127. }
  128. /* find the mpeg1 video encoder */
  129. codec = avcodec_find_encoder(codec_id);
  130. if (!codec) {
  131. fprintf(stderr, "codec not found\n");
  132. exit(1);
  133. }
  134. c = &st->codec;
  135. c->codec_type = CODEC_TYPE_VIDEO;
  136. /* put sample parameters */
  137. c->bit_rate = 400000;
  138. /* resolution must be a multiple of two */
  139. c->width = 352;
  140. c->height = 288;
  141. /* frames per second */
  142. c->frame_rate = 25;
  143. c->frame_rate_base= 1;
  144. c->gop_size = 12; /* emit one intra frame every twelve frames */
  145. /* open it */
  146. if (avcodec_open(c, codec) < 0) {
  147. fprintf(stderr, "could not open codec\n");
  148. exit(1);
  149. }
  150. /* alloc various buffers */
  151. picture= avcodec_alloc_frame();
  152. video_outbuf_size = 100000;
  153. video_outbuf = malloc(video_outbuf_size);
  154. size = c->width * c->height;
  155. picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
  156. picture->data[0] = picture_buf;
  157. picture->data[1] = picture->data[0] + size;
  158. picture->data[2] = picture->data[1] + size / 4;
  159. picture->linesize[0] = c->width;
  160. picture->linesize[1] = c->width / 2;
  161. picture->linesize[2] = c->width / 2;
  162. return st;
  163. }
  164. void write_video_frame(AVFormatContext *oc, AVStream *st)
  165. {
  166. int x, y, i, out_size;
  167. AVCodecContext *c;
  168. c = &st->codec;
  169. /* prepare a dummy image */
  170. /* Y */
  171. i = frame_count++;
  172. for(y=0;y<c->height;y++) {
  173. for(x=0;x<c->width;x++) {
  174. picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
  175. }
  176. }
  177. /* Cb and Cr */
  178. for(y=0;y<c->height/2;y++) {
  179. for(x=0;x<c->width/2;x++) {
  180. picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
  181. picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
  182. }
  183. }
  184. /* encode the image */
  185. out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
  186. /* write the compressed frame in the media file */
  187. if (av_write_frame(oc, st->index, video_outbuf, out_size) != 0) {
  188. fprintf(stderr, "Error while writing video frame\n");
  189. exit(1);
  190. }
  191. }
  192. /**************************************************************/
  193. /* media file output */
  194. int main(int argc, char **argv)
  195. {
  196. const char *filename;
  197. AVOutputFormat *fmt;
  198. AVFormatContext *oc;
  199. AVStream *st, *audio_st, *video_st;
  200. int i;
  201. double audio_pts, video_pts;
  202. /* initialize libavcodec, and register all codecs and formats */
  203. av_register_all();
  204. if (argc != 2) {
  205. printf("usage: %s output_file\n"
  206. "API example program for to output media file with libavformat\n"
  207. "\n", argv[0]);
  208. exit(1);
  209. }
  210. filename = argv[1];
  211. /* auto detect the output format from the name. default is
  212. mpeg. */
  213. fmt = guess_format(NULL, filename, NULL);
  214. if (!fmt) {
  215. printf("Could not deduce output format from file extension: using MPEG.\n");
  216. fmt = guess_format("mpeg", NULL, NULL);
  217. }
  218. if (!fmt) {
  219. fprintf(stderr, "Could not find suitable output format\n");
  220. exit(1);
  221. }
  222. /* allocate the output media context */
  223. oc = av_mallocz(sizeof(AVFormatContext));
  224. if (!oc) {
  225. fprintf(stderr, "Memory error\n");
  226. exit(1);
  227. }
  228. oc->oformat = fmt;
  229. /* add the audio and video streams using the default format codecs
  230. and initialize the codecs */
  231. video_st = NULL;
  232. audio_st = NULL;
  233. if (fmt->video_codec != CODEC_ID_NONE) {
  234. video_st = add_video_stream(oc, fmt->video_codec);
  235. }
  236. if (fmt->audio_codec != CODEC_ID_NONE) {
  237. audio_st = add_audio_stream(oc, fmt->audio_codec);
  238. }
  239. dump_format(oc, 0, filename, 1);
  240. /* open the output file, if needed */
  241. if (!(fmt->flags & AVFMT_NOFILE)) {
  242. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  243. fprintf(stderr, "Could not open '%s'\n", filename);
  244. exit(1);
  245. }
  246. }
  247. /* set the output parameters (must be done even if no parameters) */
  248. av_set_parameters(oc, NULL);
  249. /* write the stream header, if any */
  250. av_write_header(oc);
  251. for(;;) {
  252. /* compute current audio and video time */
  253. if (audio_st)
  254. audio_pts = (double)audio_st->pts.val * oc->pts_num / oc->pts_den;
  255. else
  256. audio_pts = 0.0;
  257. if (video_st)
  258. video_pts = (double)video_st->pts.val * oc->pts_num / oc->pts_den;
  259. else
  260. video_pts = 0.0;
  261. if ((!audio_st || audio_pts >= STREAM_DURATION) &&
  262. (!video_st || video_pts >= STREAM_DURATION))
  263. break;
  264. /* write interleaved audio and video frames */
  265. if (!video_st || (video_st && audio_pts < video_pts)) {
  266. write_audio_frame(oc, audio_st);
  267. } else {
  268. write_video_frame(oc, video_st);
  269. }
  270. }
  271. /* close each codec */
  272. for(i = 0;i < oc->nb_streams; i++) {
  273. st = oc->streams[i];
  274. avcodec_close(&st->codec);
  275. }
  276. /* write the trailer, if any */
  277. av_write_trailer(oc);
  278. if (!(fmt->flags & AVFMT_NOFILE)) {
  279. /* close the output file */
  280. url_fclose(&oc->pb);
  281. }
  282. /* free the stream */
  283. av_free(oc);
  284. return 0;
  285. }