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.

185 lines
5.2KB

  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, got_picture;
  49. while (pkt->size > 0) {
  50. ret = avcodec_decode_video2(dec_ctx, frame, &got_picture, pkt);
  51. if (ret < 0) {
  52. fprintf(stderr, "Error while decoding frame %d\n", dec_ctx->frame_number);
  53. exit(1);
  54. }
  55. if (got_picture) {
  56. printf("saving frame %3d\n", dec_ctx->frame_number);
  57. fflush(stdout);
  58. /* the picture is allocated by the decoder. no need to
  59. free it */
  60. snprintf(buf, sizeof(buf), filename, dec_ctx->frame_number);
  61. pgm_save(frame->data[0], frame->linesize[0],
  62. frame->width, frame->height, buf);
  63. }
  64. if (pkt->data) {
  65. pkt->size -= ret;
  66. pkt->data += ret;
  67. }
  68. }
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. const char *filename, *outfilename;
  73. const AVCodec *codec;
  74. AVCodecParserContext *parser;
  75. AVCodecContext *c= NULL;
  76. FILE *f;
  77. AVFrame *frame;
  78. uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  79. uint8_t *data;
  80. size_t data_size;
  81. int ret;
  82. AVPacket avpkt;
  83. if (argc <= 2) {
  84. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  85. exit(0);
  86. }
  87. filename = argv[1];
  88. outfilename = argv[2];
  89. avcodec_register_all();
  90. av_init_packet(&avpkt);
  91. /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
  92. memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  93. /* find the MPEG-1 video decoder */
  94. codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
  95. if (!codec) {
  96. fprintf(stderr, "Codec not found\n");
  97. exit(1);
  98. }
  99. parser = av_parser_init(codec->id);
  100. if (!parser) {
  101. fprintf(stderr, "parser not found\n");
  102. exit(1);
  103. }
  104. c = avcodec_alloc_context3(codec);
  105. if (!c) {
  106. fprintf(stderr, "Could not allocate video codec context\n");
  107. exit(1);
  108. }
  109. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  110. MUST be initialized there because this information is not
  111. available in the bitstream. */
  112. /* open it */
  113. if (avcodec_open2(c, codec, NULL) < 0) {
  114. fprintf(stderr, "Could not open codec\n");
  115. exit(1);
  116. }
  117. f = fopen(filename, "rb");
  118. if (!f) {
  119. fprintf(stderr, "Could not open %s\n", filename);
  120. exit(1);
  121. }
  122. frame = av_frame_alloc();
  123. if (!frame) {
  124. fprintf(stderr, "Could not allocate video frame\n");
  125. exit(1);
  126. }
  127. while (!feof(f)) {
  128. /* read raw data from the input file */
  129. data_size = fread(inbuf, 1, INBUF_SIZE, f);
  130. if (!data_size)
  131. break;
  132. /* use the parser to split the data into frames */
  133. data = inbuf;
  134. while (data_size > 0) {
  135. ret = av_parser_parse2(parser, c, &avpkt.data, &avpkt.size,
  136. data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  137. if (ret < 0) {
  138. fprintf(stderr, "Error while parsing\n");
  139. exit(1);
  140. }
  141. data += ret;
  142. data_size -= ret;
  143. if (avpkt.size)
  144. decode(c, frame, &avpkt, outfilename);
  145. }
  146. }
  147. /* flush the decoder */
  148. avpkt.data = NULL;
  149. avpkt.size = 0;
  150. decode(c, frame, &avpkt, outfilename);
  151. fclose(f);
  152. av_parser_close(parser);
  153. avcodec_free_context(&c);
  154. av_frame_free(&frame);
  155. return 0;
  156. }