decode_video.c 5.2 KB

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