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.

477 lines
13KB

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