forked from dmishin/slit-scan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg_decoder.cpp
149 lines (126 loc) · 4.2 KB
/
ffmpeg_decoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//Based on sample code from https://github.com/mpenkov/ffmpeg-tutorial
#include "ffmpeg_decoder.hpp"
#include <stdio.h>
int process_ffmpeg_file( const char *fname, FrameHandler &handler, int scale )
{
AVFormatContext *pFormatCtx = NULL;
int i, videoStream;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVFrame *pFrameRGB0 = NULL, *pFrameRGB1 = NULL, *pFrameRGBOld = NULL;
AVPacket packet;
int frameFinished;
int numBytes;
uint8_t *buffer0 = NULL, *buffer1 = NULL;
AVDictionary *optionsDict = NULL;
struct SwsContext *sws_ctx = NULL;
// Open video file
if(avformat_open_input(&pFormatCtx, fname, NULL, NULL)!=0)
return -1; // Couldn't open file
// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
return -1; // Couldn't find stream information
// Dump information about file onto standard error
av_dump_format(pFormatCtx, 0, fname, 0);
// Find the first video stream
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
if(videoStream==-1)
return -1; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
// Open codec
if(avcodec_open2(pCodecCtx, pCodec, &optionsDict)<0)
return -1; // Could not open codec
// Allocate video frame
pFrame=avcodec_alloc_frame();
// Allocate an AVFrame structure
pFrameRGB0=avcodec_alloc_frame();
pFrameRGB1=avcodec_alloc_frame();
if(pFrameRGB0==NULL || pFrameRGB1==NULL || pFrameRGB1 == pFrameRGB0)
return -1;
// Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width*scale,
pCodecCtx->height*scale);
buffer0=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
buffer1=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
sws_ctx =
sws_getContext
(
pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width*scale,
pCodecCtx->height*scale,
PIX_FMT_RGB24,
SWS_BILINEAR,
NULL,
NULL,
NULL
);
// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill((AVPicture *)pFrameRGB0, buffer0, PIX_FMT_RGB24,
pCodecCtx->width*scale, pCodecCtx->height*scale);
avpicture_fill((AVPicture *)pFrameRGB1, buffer1, PIX_FMT_RGB24,
pCodecCtx->width*scale, pCodecCtx->height*scale);
// Read frames
i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
&packet);
// Did we get a video frame?
if(frameFinished) {
// Convert the image from its native format to RGB
sws_scale
(
sws_ctx,
(uint8_t const * const *)pFrame->data,
pFrame->linesize,
0,
pCodecCtx->height,
pFrameRGB0->data,
pFrameRGB0->linesize
);
// handle the frame
if (pFrameRGBOld && pFrameRGB0->data[0] == pFrameRGBOld->data[0]){
return -1;
}
if (! handler.handle(pFrameRGB0, pCodecCtx->width*scale, pCodecCtx->height*scale, i++) )
break;
pFrameRGBOld = pFrameRGB0;
pFrameRGB0 = pFrameRGB1;
pFrameRGB1 = pFrameRGBOld;
}
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
// Free the RGB image
av_free(buffer0);
av_free(buffer1);
av_free(pFrameRGB1);
av_free(pFrameRGB0);
// Free the YUV frame
av_free(pFrame);
// Close the codec
avcodec_close(pCodecCtx);
// Close the video file
avformat_close_input(&pFormatCtx);
return 1;
}