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.

177 lines
5.3KB

  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. * video decoding with libavcodec API example
  23. *
  24. * @example decode_video.c
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "libavcodec/avcodec.h"
  30. #include "libavutil/common.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/mathematics.h"
  33. #define INBUF_SIZE 4096
  34. static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
  35. char *filename)
  36. {
  37. FILE *f;
  38. int i;
  39. f=fopen(filename,"w");
  40. fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
  41. for(i=0;i<ysize;i++)
  42. fwrite(buf + i * wrap,1,xsize,f);
  43. fclose(f);
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. const char *filename, *outfilename;
  48. const AVCodec *codec;
  49. AVCodecContext *c= NULL;
  50. int frame, got_picture, len;
  51. FILE *f;
  52. AVFrame *picture;
  53. uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  54. char buf[1024];
  55. AVPacket avpkt;
  56. if (argc <= 2) {
  57. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  58. exit(0);
  59. }
  60. filename = argv[1];
  61. outfilename = argv[2];
  62. avcodec_register_all();
  63. av_init_packet(&avpkt);
  64. /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
  65. memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  66. /* find the MPEG-1 video decoder */
  67. codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
  68. if (!codec) {
  69. fprintf(stderr, "codec not found\n");
  70. exit(1);
  71. }
  72. c = avcodec_alloc_context3(codec);
  73. picture = av_frame_alloc();
  74. if (codec->capabilities & AV_CODEC_CAP_TRUNCATED)
  75. c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
  76. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  77. MUST be initialized there because this information is not
  78. available in the bitstream. */
  79. /* open it */
  80. if (avcodec_open2(c, codec, NULL) < 0) {
  81. fprintf(stderr, "could not open codec\n");
  82. exit(1);
  83. }
  84. f = fopen(filename, "rb");
  85. if (!f) {
  86. fprintf(stderr, "could not open %s\n", filename);
  87. exit(1);
  88. }
  89. frame = 0;
  90. for(;;) {
  91. avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
  92. if (avpkt.size == 0)
  93. break;
  94. /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
  95. and this is the only method to use them because you cannot
  96. know the compressed data size before analysing it.
  97. BUT some other codecs (msmpeg4, mpeg4) are inherently frame
  98. based, so you must call them with all the data for one
  99. frame exactly. You must also initialize 'width' and
  100. 'height' before initializing them. */
  101. /* NOTE2: some codecs allow the raw parameters (frame size,
  102. sample rate) to be changed at any frame. We handle this, so
  103. you should also take care of it */
  104. /* here, we use a stream based decoder (mpeg1video), so we
  105. feed decoder and see if it could decode a frame */
  106. avpkt.data = inbuf;
  107. while (avpkt.size > 0) {
  108. len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
  109. if (len < 0) {
  110. fprintf(stderr, "Error while decoding frame %d\n", frame);
  111. exit(1);
  112. }
  113. if (got_picture) {
  114. printf("saving frame %3d\n", frame);
  115. fflush(stdout);
  116. /* the picture is allocated by the decoder. no need to
  117. free it */
  118. snprintf(buf, sizeof(buf), outfilename, frame);
  119. pgm_save(picture->data[0], picture->linesize[0],
  120. c->width, c->height, buf);
  121. frame++;
  122. }
  123. avpkt.size -= len;
  124. avpkt.data += len;
  125. }
  126. }
  127. /* Some codecs, such as MPEG, transmit the I- and P-frame with a
  128. latency of one frame. You must do the following to have a
  129. chance to get the last frame of the video. */
  130. avpkt.data = NULL;
  131. avpkt.size = 0;
  132. len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
  133. if (got_picture) {
  134. printf("saving last frame %3d\n", frame);
  135. fflush(stdout);
  136. /* the picture is allocated by the decoder. no need to
  137. free it */
  138. snprintf(buf, sizeof(buf), outfilename, frame);
  139. pgm_save(picture->data[0], picture->linesize[0],
  140. c->width, c->height, buf);
  141. frame++;
  142. }
  143. fclose(f);
  144. avcodec_free_context(&c);
  145. av_frame_free(&picture);
  146. return 0;
  147. }