decode_video.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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,"w");
  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. AVPacket *pkt;
  84. if (argc <= 2) {
  85. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  86. exit(0);
  87. }
  88. filename = argv[1];
  89. outfilename = argv[2];
  90. pkt = av_packet_alloc();
  91. if (!pkt)
  92. exit(1);
  93. /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
  94. memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  95. /* find the MPEG-1 video decoder */
  96. codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
  97. if (!codec) {
  98. fprintf(stderr, "Codec not found\n");
  99. exit(1);
  100. }
  101. parser = av_parser_init(codec->id);
  102. if (!parser) {
  103. fprintf(stderr, "parser not found\n");
  104. exit(1);
  105. }
  106. c = avcodec_alloc_context3(codec);
  107. if (!c) {
  108. fprintf(stderr, "Could not allocate video codec context\n");
  109. exit(1);
  110. }
  111. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  112. MUST be initialized there because this information is not
  113. available in the bitstream. */
  114. /* open it */
  115. if (avcodec_open2(c, codec, NULL) < 0) {
  116. fprintf(stderr, "Could not open codec\n");
  117. exit(1);
  118. }
  119. f = fopen(filename, "rb");
  120. if (!f) {
  121. fprintf(stderr, "Could not open %s\n", filename);
  122. exit(1);
  123. }
  124. frame = av_frame_alloc();
  125. if (!frame) {
  126. fprintf(stderr, "Could not allocate video frame\n");
  127. exit(1);
  128. }
  129. while (!feof(f)) {
  130. /* read raw data from the input file */
  131. data_size = fread(inbuf, 1, INBUF_SIZE, f);
  132. if (!data_size)
  133. break;
  134. /* use the parser to split the data into frames */
  135. data = inbuf;
  136. while (data_size > 0) {
  137. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  138. data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  139. if (ret < 0) {
  140. fprintf(stderr, "Error while parsing\n");
  141. exit(1);
  142. }
  143. data += ret;
  144. data_size -= ret;
  145. if (pkt->size)
  146. decode(c, frame, pkt, outfilename);
  147. }
  148. }
  149. /* flush the decoder */
  150. decode(c, frame, NULL, outfilename);
  151. fclose(f);
  152. av_parser_close(parser);
  153. avcodec_free_context(&c);
  154. av_frame_free(&frame);
  155. av_packet_free(&pkt);
  156. return 0;
  157. }