decode_video.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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
  24. * video decoding with libavcodec API example
  25. *
  26. * @example decode_video.c
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <libavcodec/avcodec.h>
  32. #define INBUF_SIZE 4096
  33. static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
  34. char *filename)
  35. {
  36. FILE *f;
  37. int i;
  38. f = fopen(filename,"wb");
  39. fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
  40. for (i = 0; i < ysize; i++)
  41. fwrite(buf + i * wrap, 1, xsize, f);
  42. fclose(f);
  43. }
  44. static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
  45. const char *filename)
  46. {
  47. char buf[1024];
  48. int ret;
  49. ret = avcodec_send_packet(dec_ctx, pkt);
  50. if (ret < 0) {
  51. fprintf(stderr, "Error sending a packet for decoding\n");
  52. exit(1);
  53. }
  54. while (ret >= 0) {
  55. ret = avcodec_receive_frame(dec_ctx, frame);
  56. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  57. return;
  58. else if (ret < 0) {
  59. fprintf(stderr, "Error during decoding\n");
  60. exit(1);
  61. }
  62. printf("saving frame %3d\n", dec_ctx->frame_number);
  63. fflush(stdout);
  64. /* the picture is allocated by the decoder. no need to
  65. free it */
  66. snprintf(buf, sizeof(buf), "%s-%d", filename, dec_ctx->frame_number);
  67. pgm_save(frame->data[0], frame->linesize[0],
  68. frame->width, frame->height, buf);
  69. }
  70. }
  71. int main(int argc, char **argv)
  72. {
  73. const char *filename, *outfilename;
  74. const AVCodec *codec;
  75. AVCodecParserContext *parser;
  76. AVCodecContext *c= NULL;
  77. FILE *f;
  78. AVFrame *frame;
  79. uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  80. uint8_t *data;
  81. size_t data_size;
  82. int ret;
  83. int eof;
  84. AVPacket *pkt;
  85. if (argc <= 2) {
  86. fprintf(stderr, "Usage: %s <input file> <output file>\n"
  87. "And check your input file is encoded by mpeg1video please.\n", argv[0]);
  88. exit(0);
  89. }
  90. filename = argv[1];
  91. outfilename = argv[2];
  92. pkt = av_packet_alloc();
  93. if (!pkt)
  94. exit(1);
  95. /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
  96. memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  97. /* find the MPEG-1 video decoder */
  98. codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
  99. if (!codec) {
  100. fprintf(stderr, "Codec not found\n");
  101. exit(1);
  102. }
  103. parser = av_parser_init(codec->id);
  104. if (!parser) {
  105. fprintf(stderr, "parser not found\n");
  106. exit(1);
  107. }
  108. c = avcodec_alloc_context3(codec);
  109. if (!c) {
  110. fprintf(stderr, "Could not allocate video codec context\n");
  111. exit(1);
  112. }
  113. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  114. MUST be initialized there because this information is not
  115. available in the bitstream. */
  116. /* open it */
  117. if (avcodec_open2(c, codec, NULL) < 0) {
  118. fprintf(stderr, "Could not open codec\n");
  119. exit(1);
  120. }
  121. f = fopen(filename, "rb");
  122. if (!f) {
  123. fprintf(stderr, "Could not open %s\n", filename);
  124. exit(1);
  125. }
  126. frame = av_frame_alloc();
  127. if (!frame) {
  128. fprintf(stderr, "Could not allocate video frame\n");
  129. exit(1);
  130. }
  131. do {
  132. /* read raw data from the input file */
  133. data_size = fread(inbuf, 1, INBUF_SIZE, f);
  134. if (ferror(f))
  135. break;
  136. eof = !data_size;
  137. /* use the parser to split the data into frames */
  138. data = inbuf;
  139. while (data_size > 0 || eof) {
  140. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  141. data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  142. if (ret < 0) {
  143. fprintf(stderr, "Error while parsing\n");
  144. exit(1);
  145. }
  146. data += ret;
  147. data_size -= ret;
  148. if (pkt->size)
  149. decode(c, frame, pkt, outfilename);
  150. else if (eof)
  151. break;
  152. }
  153. } while (!eof);
  154. /* flush the decoder */
  155. decode(c, frame, NULL, outfilename);
  156. fclose(f);
  157. av_parser_close(parser);
  158. avcodec_free_context(&c);
  159. av_frame_free(&frame);
  160. av_packet_free(&pkt);
  161. return 0;
  162. }