decode_video.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2001 Fabrice Bellard
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file libavcodec video decoding API usage example
  24. * @example decode_video.c *
  25. *
  26. * Read from an MPEG1 video file, decode frames, and generate PGM images as
  27. * output.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <libavcodec/avcodec.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,"wb");
  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. static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
  46. const char *filename)
  47. {
  48. char buf[1024];
  49. int ret;
  50. ret = avcodec_send_packet(dec_ctx, pkt);
  51. if (ret < 0) {
  52. fprintf(stderr, "Error sending a packet for decoding\n");
  53. exit(1);
  54. }
  55. while (ret >= 0) {
  56. ret = avcodec_receive_frame(dec_ctx, frame);
  57. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  58. return;
  59. else if (ret < 0) {
  60. fprintf(stderr, "Error during decoding\n");
  61. exit(1);
  62. }
  63. printf("saving frame %3"PRId64"\n", dec_ctx->frame_num);
  64. fflush(stdout);
  65. /* the picture is allocated by the decoder. no need to
  66. free it */
  67. snprintf(buf, sizeof(buf), "%s-%"PRId64, filename, dec_ctx->frame_num);
  68. pgm_save(frame->data[0], frame->linesize[0],
  69. frame->width, frame->height, buf);
  70. }
  71. }
  72. int main(int argc, char **argv)
  73. {
  74. const char *filename, *outfilename;
  75. const AVCodec *codec;
  76. AVCodecParserContext *parser;
  77. AVCodecContext *c= NULL;
  78. FILE *f;
  79. AVFrame *frame;
  80. uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  81. uint8_t *data;
  82. size_t data_size;
  83. int ret;
  84. int eof;
  85. AVPacket *pkt;
  86. if (argc <= 2) {
  87. fprintf(stderr, "Usage: %s <input file> <output file>\n"
  88. "And check your input file is encoded by mpeg1video please.\n", argv[0]);
  89. exit(0);
  90. }
  91. filename = argv[1];
  92. outfilename = argv[2];
  93. pkt = av_packet_alloc();
  94. if (!pkt)
  95. exit(1);
  96. /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
  97. memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  98. /* find the MPEG-1 video decoder */
  99. codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
  100. if (!codec) {
  101. fprintf(stderr, "Codec not found\n");
  102. exit(1);
  103. }
  104. parser = av_parser_init(codec->id);
  105. if (!parser) {
  106. fprintf(stderr, "parser not found\n");
  107. exit(1);
  108. }
  109. c = avcodec_alloc_context3(codec);
  110. if (!c) {
  111. fprintf(stderr, "Could not allocate video codec context\n");
  112. exit(1);
  113. }
  114. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  115. MUST be initialized there because this information is not
  116. available in the bitstream. */
  117. /* open it */
  118. if (avcodec_open2(c, codec, NULL) < 0) {
  119. fprintf(stderr, "Could not open codec\n");
  120. exit(1);
  121. }
  122. f = fopen(filename, "rb");
  123. if (!f) {
  124. fprintf(stderr, "Could not open %s\n", filename);
  125. exit(1);
  126. }
  127. frame = av_frame_alloc();
  128. if (!frame) {
  129. fprintf(stderr, "Could not allocate video frame\n");
  130. exit(1);
  131. }
  132. do {
  133. /* read raw data from the input file */
  134. data_size = fread(inbuf, 1, INBUF_SIZE, f);
  135. if (ferror(f))
  136. break;
  137. eof = !data_size;
  138. /* use the parser to split the data into frames */
  139. data = inbuf;
  140. while (data_size > 0 || eof) {
  141. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  142. data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  143. if (ret < 0) {
  144. fprintf(stderr, "Error while parsing\n");
  145. exit(1);
  146. }
  147. data += ret;
  148. data_size -= ret;
  149. if (pkt->size)
  150. decode(c, frame, pkt, outfilename);
  151. else if (eof)
  152. break;
  153. }
  154. } while (!eof);
  155. /* flush the decoder */
  156. decode(c, frame, NULL, outfilename);
  157. fclose(f);
  158. av_parser_close(parser);
  159. avcodec_free_context(&c);
  160. av_frame_free(&frame);
  161. av_packet_free(&pkt);
  162. return 0;
  163. }