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.

182 lines
4.8KB

  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. 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 %3d\n", dec_ctx->frame_number);
  64. fflush(stdout);
  65. /* the picture is allocated by the decoder. no need to
  66. free it */
  67. snprintf(buf, sizeof(buf), filename, dec_ctx->frame_number);
  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 *picture;
  80. uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  81. uint8_t *data;
  82. size_t data_size;
  83. int ret;
  84. AVPacket *pkt;
  85. if (argc <= 2) {
  86. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  87. exit(0);
  88. }
  89. filename = argv[1];
  90. outfilename = argv[2];
  91. avcodec_register_all();
  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. picture = av_frame_alloc();
  110. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  111. MUST be initialized there because this information is not
  112. available in the bitstream. */
  113. /* open it */
  114. if (avcodec_open2(c, codec, NULL) < 0) {
  115. fprintf(stderr, "could not open codec\n");
  116. exit(1);
  117. }
  118. f = fopen(filename, "rb");
  119. if (!f) {
  120. fprintf(stderr, "could not open %s\n", filename);
  121. exit(1);
  122. }
  123. while (!feof(f)) {
  124. /* read raw data from the input file */
  125. data_size = fread(inbuf, 1, INBUF_SIZE, f);
  126. if (!data_size)
  127. break;
  128. /* use the parser to split the data into frames */
  129. data = inbuf;
  130. while (data_size > 0) {
  131. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  132. data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  133. if (ret < 0) {
  134. fprintf(stderr, "Error while parsing\n");
  135. exit(1);
  136. }
  137. data += ret;
  138. data_size -= ret;
  139. if (pkt->size)
  140. decode(c, picture, pkt, outfilename);
  141. }
  142. }
  143. /* flush the decoder */
  144. decode(c, picture, NULL, outfilename);
  145. fclose(f);
  146. av_parser_close(parser);
  147. avcodec_free_context(&c);
  148. av_frame_free(&picture);
  149. av_packet_free(&pkt);
  150. return 0;
  151. }