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.

438 lines
12KB

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