apiexample.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * copyright (c) 2001 Fabrice Bellard
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file libavcodec/apiexample.c
  22. * avcodec API use example.
  23. *
  24. * Note that this library only handles codecs (mpeg, mpeg4, etc...),
  25. * not file formats (avi, vob, etc...). See library 'libavformat' for the
  26. * format handling
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #ifdef HAVE_AV_CONFIG_H
  32. #undef HAVE_AV_CONFIG_H
  33. #endif
  34. #include "avcodec.h"
  35. #include "libavutil/mathematics.h"
  36. #define INBUF_SIZE 4096
  37. /*
  38. * Audio encoding example
  39. */
  40. void audio_encode_example(const char *filename)
  41. {
  42. AVCodec *codec;
  43. AVCodecContext *c= NULL;
  44. int frame_size, i, j, out_size, outbuf_size;
  45. FILE *f;
  46. short *samples;
  47. float t, tincr;
  48. uint8_t *outbuf;
  49. printf("Audio encoding\n");
  50. /* find the MP2 encoder */
  51. codec = avcodec_find_encoder(CODEC_ID_MP2);
  52. if (!codec) {
  53. fprintf(stderr, "codec not found\n");
  54. exit(1);
  55. }
  56. c= avcodec_alloc_context();
  57. /* put sample parameters */
  58. c->bit_rate = 64000;
  59. c->sample_rate = 44100;
  60. c->channels = 2;
  61. /* open it */
  62. if (avcodec_open(c, codec) < 0) {
  63. fprintf(stderr, "could not open codec\n");
  64. exit(1);
  65. }
  66. /* the codec gives us the frame size, in samples */
  67. frame_size = c->frame_size;
  68. samples = malloc(frame_size * 2 * c->channels);
  69. outbuf_size = 10000;
  70. outbuf = malloc(outbuf_size);
  71. f = fopen(filename, "wb");
  72. if (!f) {
  73. fprintf(stderr, "could not open %s\n", filename);
  74. exit(1);
  75. }
  76. /* encode a single tone sound */
  77. t = 0;
  78. tincr = 2 * M_PI * 440.0 / c->sample_rate;
  79. for(i=0;i<200;i++) {
  80. for(j=0;j<frame_size;j++) {
  81. samples[2*j] = (int)(sin(t) * 10000);
  82. samples[2*j+1] = samples[2*j];
  83. t += tincr;
  84. }
  85. /* encode the samples */
  86. out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
  87. fwrite(outbuf, 1, out_size, f);
  88. }
  89. fclose(f);
  90. free(outbuf);
  91. free(samples);
  92. avcodec_close(c);
  93. av_free(c);
  94. }
  95. /*
  96. * Audio decoding.
  97. */
  98. void audio_decode_example(const char *outfilename, const char *filename)
  99. {
  100. AVCodec *codec;
  101. AVCodecContext *c= NULL;
  102. int out_size, size, len;
  103. FILE *f, *outfile;
  104. uint8_t *outbuf;
  105. uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
  106. printf("Audio decoding\n");
  107. /* find the mpeg audio decoder */
  108. codec = avcodec_find_decoder(CODEC_ID_MP2);
  109. if (!codec) {
  110. fprintf(stderr, "codec not found\n");
  111. exit(1);
  112. }
  113. c= avcodec_alloc_context();
  114. /* open it */
  115. if (avcodec_open(c, codec) < 0) {
  116. fprintf(stderr, "could not open codec\n");
  117. exit(1);
  118. }
  119. outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
  120. f = fopen(filename, "rb");
  121. if (!f) {
  122. fprintf(stderr, "could not open %s\n", filename);
  123. exit(1);
  124. }
  125. outfile = fopen(outfilename, "wb");
  126. if (!outfile) {
  127. av_free(c);
  128. exit(1);
  129. }
  130. /* decode until eof */
  131. inbuf_ptr = inbuf;
  132. for(;;) {
  133. size = fread(inbuf, 1, INBUF_SIZE, f);
  134. if (size == 0)
  135. break;
  136. inbuf_ptr = inbuf;
  137. while (size > 0) {
  138. len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
  139. inbuf_ptr, size);
  140. if (len < 0) {
  141. fprintf(stderr, "Error while decoding\n");
  142. exit(1);
  143. }
  144. if (out_size > 0) {
  145. /* if a frame has been decoded, output it */
  146. fwrite(outbuf, 1, out_size, outfile);
  147. }
  148. size -= len;
  149. inbuf_ptr += len;
  150. }
  151. }
  152. fclose(outfile);
  153. fclose(f);
  154. free(outbuf);
  155. avcodec_close(c);
  156. av_free(c);
  157. }
  158. /*
  159. * Video encoding example
  160. */
  161. void video_encode_example(const char *filename)
  162. {
  163. AVCodec *codec;
  164. AVCodecContext *c= NULL;
  165. int i, out_size, size, x, y, outbuf_size;
  166. FILE *f;
  167. AVFrame *picture;
  168. uint8_t *outbuf, *picture_buf;
  169. printf("Video encoding\n");
  170. /* find the mpeg1 video encoder */
  171. codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
  172. if (!codec) {
  173. fprintf(stderr, "codec not found\n");
  174. exit(1);
  175. }
  176. c= avcodec_alloc_context();
  177. picture= avcodec_alloc_frame();
  178. /* put sample parameters */
  179. c->bit_rate = 400000;
  180. /* resolution must be a multiple of two */
  181. c->width = 352;
  182. c->height = 288;
  183. /* frames per second */
  184. c->time_base= (AVRational){1,25};
  185. c->gop_size = 10; /* emit one intra frame every ten frames */
  186. c->max_b_frames=1;
  187. c->pix_fmt = PIX_FMT_YUV420P;
  188. /* open it */
  189. if (avcodec_open(c, codec) < 0) {
  190. fprintf(stderr, "could not open codec\n");
  191. exit(1);
  192. }
  193. f = fopen(filename, "wb");
  194. if (!f) {
  195. fprintf(stderr, "could not open %s\n", filename);
  196. exit(1);
  197. }
  198. /* alloc image and output buffer */
  199. outbuf_size = 100000;
  200. outbuf = malloc(outbuf_size);
  201. size = c->width * c->height;
  202. picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
  203. picture->data[0] = picture_buf;
  204. picture->data[1] = picture->data[0] + size;
  205. picture->data[2] = picture->data[1] + size / 4;
  206. picture->linesize[0] = c->width;
  207. picture->linesize[1] = c->width / 2;
  208. picture->linesize[2] = c->width / 2;
  209. /* encode 1 second of video */
  210. for(i=0;i<25;i++) {
  211. fflush(stdout);
  212. /* prepare a dummy image */
  213. /* Y */
  214. for(y=0;y<c->height;y++) {
  215. for(x=0;x<c->width;x++) {
  216. picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
  217. }
  218. }
  219. /* Cb and Cr */
  220. for(y=0;y<c->height/2;y++) {
  221. for(x=0;x<c->width/2;x++) {
  222. picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
  223. picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
  224. }
  225. }
  226. /* encode the image */
  227. out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
  228. printf("encoding frame %3d (size=%5d)\n", i, out_size);
  229. fwrite(outbuf, 1, out_size, f);
  230. }
  231. /* get the delayed frames */
  232. for(; out_size; i++) {
  233. fflush(stdout);
  234. out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
  235. printf("write frame %3d (size=%5d)\n", i, out_size);
  236. fwrite(outbuf, 1, out_size, f);
  237. }
  238. /* add sequence end code to have a real mpeg file */
  239. outbuf[0] = 0x00;
  240. outbuf[1] = 0x00;
  241. outbuf[2] = 0x01;
  242. outbuf[3] = 0xb7;
  243. fwrite(outbuf, 1, 4, f);
  244. fclose(f);
  245. free(picture_buf);
  246. free(outbuf);
  247. avcodec_close(c);
  248. av_free(c);
  249. av_free(picture);
  250. printf("\n");
  251. }
  252. /*
  253. * Video decoding example
  254. */
  255. void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
  256. {
  257. FILE *f;
  258. int i;
  259. f=fopen(filename,"w");
  260. fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
  261. for(i=0;i<ysize;i++)
  262. fwrite(buf + i * wrap,1,xsize,f);
  263. fclose(f);
  264. }
  265. void video_decode_example(const char *outfilename, const char *filename)
  266. {
  267. AVCodec *codec;
  268. AVCodecContext *c= NULL;
  269. int frame, size, got_picture, len;
  270. FILE *f;
  271. AVFrame *picture;
  272. uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
  273. char buf[1024];
  274. /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
  275. memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  276. printf("Video decoding\n");
  277. /* find the mpeg1 video decoder */
  278. codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
  279. if (!codec) {
  280. fprintf(stderr, "codec not found\n");
  281. exit(1);
  282. }
  283. c= avcodec_alloc_context();
  284. picture= avcodec_alloc_frame();
  285. if(codec->capabilities&CODEC_CAP_TRUNCATED)
  286. c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
  287. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  288. MUST be initialized there because this information is not
  289. available in the bitstream. */
  290. /* open it */
  291. if (avcodec_open(c, codec) < 0) {
  292. fprintf(stderr, "could not open codec\n");
  293. exit(1);
  294. }
  295. /* the codec gives us the frame size, in samples */
  296. f = fopen(filename, "rb");
  297. if (!f) {
  298. fprintf(stderr, "could not open %s\n", filename);
  299. exit(1);
  300. }
  301. frame = 0;
  302. for(;;) {
  303. size = fread(inbuf, 1, INBUF_SIZE, f);
  304. if (size == 0)
  305. break;
  306. /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
  307. and this is the only method to use them because you cannot
  308. know the compressed data size before analysing it.
  309. BUT some other codecs (msmpeg4, mpeg4) are inherently frame
  310. based, so you must call them with all the data for one
  311. frame exactly. You must also initialize 'width' and
  312. 'height' before initializing them. */
  313. /* NOTE2: some codecs allow the raw parameters (frame size,
  314. sample rate) to be changed at any frame. We handle this, so
  315. you should also take care of it */
  316. /* here, we use a stream based decoder (mpeg1video), so we
  317. feed decoder and see if it could decode a frame */
  318. inbuf_ptr = inbuf;
  319. while (size > 0) {
  320. len = avcodec_decode_video(c, picture, &got_picture,
  321. inbuf_ptr, size);
  322. if (len < 0) {
  323. fprintf(stderr, "Error while decoding frame %d\n", frame);
  324. exit(1);
  325. }
  326. if (got_picture) {
  327. printf("saving frame %3d\n", frame);
  328. fflush(stdout);
  329. /* the picture is allocated by the decoder. no need to
  330. free it */
  331. snprintf(buf, sizeof(buf), outfilename, frame);
  332. pgm_save(picture->data[0], picture->linesize[0],
  333. c->width, c->height, buf);
  334. frame++;
  335. }
  336. size -= len;
  337. inbuf_ptr += len;
  338. }
  339. }
  340. /* some codecs, such as MPEG, transmit the I and P frame with a
  341. latency of one frame. You must do the following to have a
  342. chance to get the last frame of the video */
  343. len = avcodec_decode_video(c, picture, &got_picture,
  344. NULL, 0);
  345. if (got_picture) {
  346. printf("saving last frame %3d\n", frame);
  347. fflush(stdout);
  348. /* the picture is allocated by the decoder. no need to
  349. free it */
  350. snprintf(buf, sizeof(buf), outfilename, frame);
  351. pgm_save(picture->data[0], picture->linesize[0],
  352. c->width, c->height, buf);
  353. frame++;
  354. }
  355. fclose(f);
  356. avcodec_close(c);
  357. av_free(c);
  358. av_free(picture);
  359. printf("\n");
  360. }
  361. int main(int argc, char **argv)
  362. {
  363. const char *filename;
  364. /* must be called before using avcodec lib */
  365. avcodec_init();
  366. /* register all the codecs */
  367. avcodec_register_all();
  368. if (argc <= 1) {
  369. audio_encode_example("/tmp/test.mp2");
  370. audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
  371. video_encode_example("/tmp/test.mpg");
  372. filename = "/tmp/test.mpg";
  373. } else {
  374. filename = argv[1];
  375. }
  376. // audio_decode_example("/tmp/test.sw", filename);
  377. video_decode_example("/tmp/test%d.pgm", filename);
  378. return 0;
  379. }