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.

408 lines
11KB

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