2006-09-10 17:02:42 +03:00
|
|
|
/*
|
2017-03-29 14:07:39 +02:00
|
|
|
* Copyright (c) 2001 Fabrice Bellard
|
2006-09-10 17:02:42 +03:00
|
|
|
*
|
2017-03-29 14:07:39 +02:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2006-10-07 18:30:46 +03:00
|
|
|
*
|
2017-03-29 14:07:39 +02:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2006-09-10 17:02:42 +03:00
|
|
|
*
|
2017-03-29 14:07:39 +02:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2006-09-10 17:02:42 +03:00
|
|
|
*/
|
|
|
|
|
2003-03-06 13:32:04 +02:00
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2016-10-19 21:05:22 +02:00
|
|
|
* video decoding with libavcodec API example
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2016-10-19 21:05:22 +02:00
|
|
|
* @example decode_video.c
|
2001-07-22 17:18:56 +03:00
|
|
|
*/
|
2003-03-06 13:32:04 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
#include <stdio.h>
|
2016-10-19 21:05:22 +02:00
|
|
|
#include <stdlib.h>
|
2001-07-22 17:18:56 +03:00
|
|
|
#include <string.h>
|
2007-02-18 23:23:50 +02:00
|
|
|
|
2017-03-29 14:07:39 +02:00
|
|
|
#include <libavcodec/avcodec.h>
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
#define INBUF_SIZE 4096
|
|
|
|
|
2009-03-31 12:32:59 +03:00
|
|
|
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
|
|
|
|
char *filename)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int i;
|
|
|
|
|
2017-03-29 14:07:39 +02:00
|
|
|
f = fopen(filename,"w");
|
|
|
|
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
|
|
|
|
for (i = 0; i < ysize; i++)
|
|
|
|
fwrite(buf + i * wrap, 1, xsize, f);
|
2001-07-22 17:18:56 +03:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2017-03-29 14:07:39 +02:00
|
|
|
static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,
|
|
|
|
AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
|
|
|
|
{
|
|
|
|
int len, got_frame;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
len = avcodec_decode_video2(avctx, frame, &got_frame, pkt);
|
|
|
|
if (len < 0) {
|
|
|
|
fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
if (got_frame) {
|
|
|
|
printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/* the picture is allocated by the decoder, no need to free it */
|
|
|
|
snprintf(buf, sizeof(buf), outfilename, *frame_count);
|
|
|
|
pgm_save(frame->data[0], frame->linesize[0],
|
|
|
|
frame->width, frame->height, buf);
|
|
|
|
(*frame_count)++;
|
|
|
|
}
|
|
|
|
if (pkt->data) {
|
|
|
|
pkt->size -= len;
|
|
|
|
pkt->data += len;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-19 21:05:22 +02:00
|
|
|
int main(int argc, char **argv)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
2016-10-19 21:05:22 +02:00
|
|
|
const char *filename, *outfilename;
|
2016-10-20 11:03:20 +02:00
|
|
|
const AVCodec *codec;
|
2002-09-19 21:24:52 +03:00
|
|
|
AVCodecContext *c= NULL;
|
2017-03-29 14:07:39 +02:00
|
|
|
int frame_count;
|
2001-07-22 17:18:56 +03:00
|
|
|
FILE *f;
|
2017-03-29 14:07:39 +02:00
|
|
|
AVFrame *frame;
|
2015-06-29 23:48:34 +02:00
|
|
|
uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
|
2009-04-10 14:07:52 +03:00
|
|
|
AVPacket avpkt;
|
|
|
|
|
2016-10-19 21:05:22 +02:00
|
|
|
if (argc <= 2) {
|
|
|
|
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
filename = argv[1];
|
|
|
|
outfilename = argv[2];
|
|
|
|
|
|
|
|
avcodec_register_all();
|
|
|
|
|
2009-04-10 14:07:52 +03:00
|
|
|
av_init_packet(&avpkt);
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
/* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
|
2015-06-29 23:48:34 +02:00
|
|
|
memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2003-08-01 14:01:05 +03:00
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
/* find the MPEG-1 video decoder */
|
2012-08-05 12:11:04 +03:00
|
|
|
codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (!codec) {
|
2017-03-29 14:07:39 +02:00
|
|
|
fprintf(stderr, "Codec not found\n");
|
2001-07-22 17:18:56 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-06-18 14:40:48 +03:00
|
|
|
c = avcodec_alloc_context3(codec);
|
2017-03-29 14:07:39 +02:00
|
|
|
if (!c) {
|
|
|
|
fprintf(stderr, "Could not allocate video codec context\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2015-07-07 02:41:27 +02:00
|
|
|
if (codec->capabilities & AV_CODEC_CAP_TRUNCATED)
|
2015-06-29 21:59:37 +02:00
|
|
|
c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
|
2002-11-26 18:16:04 +02:00
|
|
|
|
2007-06-12 12:29:25 +03:00
|
|
|
/* For some codecs, such as msmpeg4 and mpeg4, width and height
|
|
|
|
MUST be initialized there because this information is not
|
|
|
|
available in the bitstream. */
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
/* open it */
|
2012-02-19 12:34:40 +03:00
|
|
|
if (avcodec_open2(c, codec, NULL) < 0) {
|
2017-03-29 14:07:39 +02:00
|
|
|
fprintf(stderr, "Could not open codec\n");
|
2001-07-22 17:18:56 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2004-04-09 18:04:51 +03:00
|
|
|
f = fopen(filename, "rb");
|
2001-07-22 17:18:56 +03:00
|
|
|
if (!f) {
|
2017-03-29 14:07:39 +02:00
|
|
|
fprintf(stderr, "Could not open %s\n", filename);
|
2001-07-22 17:18:56 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2017-03-29 14:07:39 +02:00
|
|
|
frame = av_frame_alloc();
|
|
|
|
if (!frame) {
|
|
|
|
fprintf(stderr, "Could not allocate video frame\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_count = 0;
|
|
|
|
for (;;) {
|
2009-04-10 14:07:52 +03:00
|
|
|
avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
|
|
|
|
if (avpkt.size == 0)
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
|
|
|
|
and this is the only method to use them because you cannot
|
2005-12-17 20:14:38 +02:00
|
|
|
know the compressed data size before analysing it.
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2001-08-15 16:12:23 +03:00
|
|
|
BUT some other codecs (msmpeg4, mpeg4) are inherently frame
|
|
|
|
based, so you must call them with all the data for one
|
|
|
|
frame exactly. You must also initialize 'width' and
|
2001-07-22 17:18:56 +03:00
|
|
|
'height' before initializing them. */
|
|
|
|
|
|
|
|
/* NOTE2: some codecs allow the raw parameters (frame size,
|
|
|
|
sample rate) to be changed at any frame. We handle this, so
|
|
|
|
you should also take care of it */
|
|
|
|
|
|
|
|
/* here, we use a stream based decoder (mpeg1video), so we
|
|
|
|
feed decoder and see if it could decode a frame */
|
2009-04-10 14:07:52 +03:00
|
|
|
avpkt.data = inbuf;
|
2017-03-29 14:07:39 +02:00
|
|
|
while (avpkt.size > 0)
|
|
|
|
if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
|
2001-07-22 17:18:56 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
/* Some codecs, such as MPEG, transmit the I- and P-frame with a
|
2001-07-22 17:18:56 +03:00
|
|
|
latency of one frame. You must do the following to have a
|
2016-04-27 19:45:23 +02:00
|
|
|
chance to get the last frame of the video. */
|
2009-04-10 14:07:52 +03:00
|
|
|
avpkt.data = NULL;
|
|
|
|
avpkt.size = 0;
|
2017-03-29 14:07:39 +02:00
|
|
|
decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
fclose(f);
|
|
|
|
|
2016-07-20 16:10:17 +02:00
|
|
|
avcodec_free_context(&c);
|
2017-03-29 14:07:39 +02:00
|
|
|
av_frame_free(&frame);
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|