summaryrefslogtreecommitdiff
path: root/ffmpeg_backend.cpp
blob: b3d5be8bb752eebc4de667488dda265a5fef40b3 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
}

// workaround to make libav error printing work
#ifdef av_err2str
#undef av_err2str
#include <string>
av_always_inline std::string av_err2string(int errnum) {
    char str[AV_ERROR_MAX_STRING_SIZE];
    return av_make_error_string(str, AV_ERROR_MAX_STRING_SIZE, errnum);
}
#define av_err2str(err) av_err2string(err).c_str()
#endif  // av_err2str

#include "ffmpeg_backend.h"

bool32 AV_TryFrame(av_info *AV, int32 *err)
{
    *err = av_read_frame(AV->FileFormatContext, AV->VideoPacket);
    if (*err >= 0 && AV->VideoPacket->stream_index != AV->VideoStream->index) {
        av_packet_unref(AV->VideoPacket);
        return 0;
    }

    if (*err < 0)
        *err = avcodec_send_packet(AV->VideoCodecContext, AV->VideoPacket);
    else {
        *err = avcodec_send_packet(AV->VideoCodecContext, AV->VideoPacket);
    }
    av_packet_unref(AV->VideoPacket);

    if (*err < 0)
    {
        fprintf(stderr, "Libav *error: (%s)\n", av_err2str(*err));
        Assert(0);
    }

    while (*err >= 0) {
        *err = avcodec_receive_frame(AV->VideoCodecContext, AV->VideoFrame);
        if (*err == AVERROR_EOF) {
        } else if (*err == AVERROR(EAGAIN)) {
            *err = 0;
            break;
        } else if (*err < 0) {
            Assert(0);
        }
        return 1;
    }
    return 0;
}

// TODO(fox): Could be combined into AV_Init once we have dealloc functions for
// the AVInfo allocation.
bool32 AV_IsFileSupported(char *filename)
{
    int32 err = 0;

    // enum AVHWDeviceType type;
    // while((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
    //     printf("%s\n", av_hwdevice_get_type_name(type));

    AVFormatContext *temp = avformat_alloc_context();
    err = avformat_open_input(&temp, filename, NULL, NULL);;

    if (err < 0) {
            fprintf(stderr, "Libav error: (%s)\n", av_err2str(err));
            avformat_free_context(temp);
            return 0;
    }

    err = avformat_find_stream_info(temp,  NULL);

    if (err < 0) {
            fprintf(stderr, "Libav error: (%s)\n", av_err2str(err));
            avformat_free_context(temp);
            return 0;
    }

    avformat_free_context(temp);

    return 1;
}

void AV_Init(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory)
{
    BitmapInfo->AVInfo = AllocateMemory(Memory, sizeof(av_info), P_AVInfo);
    av_info *AV = (av_info *)BitmapInfo->AVInfo;
    *AV = {};

    int32 err = 0;

    // enum AVHWDeviceType type;
    // while((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
    //     printf("%s\n", av_hwdevice_get_type_name(type));

    // The two calls below theoretically shouldn't fail since we already tested them in IsFileSupported.

    AV->FileFormatContext = avformat_alloc_context();
    err = avformat_open_input(&AV->FileFormatContext, Source->Path, NULL, NULL);;
    if (err < 0) {
            fprintf(stderr, "Libav error: (%s)\n", av_err2str(err));
            Assert(0);
    }

    err = avformat_find_stream_info(AV->FileFormatContext,  NULL);

    if (err < 0) {
            fprintf(stderr, "Libav error: (%s)\n", av_err2str(err));
            Assert(0);
    }

    for (uint32 i = 0; i < AV->FileFormatContext->nb_streams; i++)
    {
        AVCodecParameters *LocalCodecParameters = NULL;
        LocalCodecParameters = AV->FileFormatContext->streams[i]->codecpar;
        if (LocalCodecParameters->codec_type == AVMEDIA_TYPE_VIDEO) {
            AV->VideoCodecParameters = LocalCodecParameters;
            AV->VideoStream =  AV->FileFormatContext->streams[i];
            break;
        }
    }

    if (!AV->VideoCodecParameters) {
        printf("Libav error: No video track found.");
    }

    AV->VideoCodec = avcodec_find_decoder(AV->VideoCodecParameters->codec_id);

    if (!AV->VideoCodec) {
        printf("Libav error: Video codec could not be identified.");
    }
/*
    int16 codecs = 0;
    for (;;) {
        AV->VideoHWConfig = avcodec_get_hw_config(AV->VideoCodec, codecs);
        if (!AV->VideoHWConfig) {
            printf("Libav error: Hardware acceleration not found for decoder %s.",
                    AV->VideoCodec->name);
            break;
        }
        AV->HWPixFormat = AV->VideoHWConfig->pix_fmt;
        break;
        // if (AV->VideoHWConfig->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
        //     AV->VideoHWConfig->device_type == type) {
        // }
        codecs++;
    }
*/
    AV->VideoCodecContext = avcodec_alloc_context3(AV->VideoCodec);
    if (!AV->VideoCodecContext) {
        printf("Libav error: Decoder context allocation failed.");
    }
    err = avcodec_parameters_to_context(AV->VideoCodecContext, AV->VideoCodecParameters);
    if (err < 0) {
            fprintf(stderr, "Libav error: (%s)\n", av_err2str(err));
    }
    avcodec_open2(AV->VideoCodecContext, AV->VideoCodec, NULL);
    if (err < 0) {
            fprintf(stderr, "Libav error: (%s)\n", av_err2str(err));
    }

    AV->VideoPacket = av_packet_alloc();
    if (err < 0) {
            fprintf(stderr, "Libav error: (%s)\n", av_err2str(err));
    }
    AV->VideoFrame = av_frame_alloc();
    if (err < 0) {
            fprintf(stderr, "Libav error: (%s)\n", av_err2str(err));
    }

    Source->Info.FPS = (real32)AV->VideoStream->r_frame_rate.num / AV->VideoStream->r_frame_rate.den;
    Source->Info.Width = AV->VideoCodecContext->width;
    Source->Info.Height = AV->VideoCodecContext->height;
};



void AV_GetPTSAverage(source *Source, av_info *AV, int32 *err)
{
    // TODO(fox): This PTS average isn't exact and causes an occasional
    // frame skip. See libav remarks in forum for more details.

    // TODO(fox): Handle footage under five seconds.
    int16 TestAmount = 5;

    real32 FPS = (real32)AV->VideoStream->r_frame_rate.num / AV->VideoStream->r_frame_rate.den;

    int16 i = 0;
    real32 AvgPTSPerSecond = 0;
    for (;;) {
        if (AV_TryFrame(AV, err)) {
            if (i >= FPS * TestAmount) {
                AvgPTSPerSecond = (real32)AV->VideoFrame->pts / TestAmount;
                printf("frame: %i, pts: %li\n", i, AV->VideoFrame->pts);
                break;
            }
            i++;
            av_frame_unref(AV->VideoFrame);
        }
    }

    Source->Info.AvgPTSPerFrame = (real32)AvgPTSPerSecond / (int32)(FPS + 0.5f);
    printf("Avg PTS per sec: %.06f, Avg PTS per frame: %.06f\n", AvgPTSPerSecond, Source->Info.AvgPTSPerFrame);

    av_seek_frame(AV->FileFormatContext, -1, 0, AVSEEK_FLAG_BACKWARD);
}


cached_bitmap * AV_LoadVideoFrame(source *Source, layer_bitmap_info *BitmapInfo, memory *Memory, int32 TimelineFrame)
{
    av_info *AV = (av_info *)BitmapInfo->AVInfo;
    int32 *CurrentlyRenderedFrame = &BitmapInfo->CurrentFrame;

    int32 err = 0;

    if (!Source->Info.AvgPTSPerFrame) {
        AV_GetPTSAverage(Source, AV, &err);
    }
    Assert(Source->Info.AvgPTSPerFrame);

    int32 FrameToSeek = TimelineFrame - BitmapInfo->FrameOffset;
    if (*CurrentlyRenderedFrame == FrameToSeek || FrameToSeek < 0)
        return 0;

    // NOTE(fox): The decoder automatically advances to the next frame, so we
    // don't need to call av_seek_frame under normal playback.
    // This function only seeks to the nearest "keyframe."

    if (*CurrentlyRenderedFrame != FrameToSeek - 1) {
        int64 SeekSeconds = (int64)(FrameToSeek / (int32)(Source->Info.FPS + 0.5f) * AV_TIME_BASE);
        av_seek_frame(AV->FileFormatContext, -1, SeekSeconds, AVSEEK_FLAG_BACKWARD);
        printf("Seek activated\n");
    } else if (*CurrentlyRenderedFrame < 0) {
        av_seek_frame(AV->FileFormatContext, -1, 0, AVSEEK_FLAG_BACKWARD);
    }

    *CurrentlyRenderedFrame = FrameToSeek;

    int64 SeekPTS = (int64)(Source->Info.AvgPTSPerFrame*FrameToSeek + 0.5f);

    while (err >= 0) {
        if (AV_TryFrame(AV, &err)) {

            // The first frame that gets loaded isn't always the actual
            // first frame, so we need to check until it's correct.
            if (FrameToSeek == 0 && AV->VideoFrame->pts != AV->VideoStream->start_time) {
                av_frame_unref(AV->VideoFrame);
                printf("NON-START: avg: %li, real pts: %li", SeekPTS, AV->VideoFrame->pts);
                continue;
            }

            int64 Difference = AV->VideoFrame->pts - SeekPTS;
            if (abs(Difference) < Source->Info.AvgPTSPerFrame)
            {
                if (AV->PreviousPTS == -1) {
                    AV->PreviousPTS = AV->VideoFrame->pts;
                    printf("avg: %li, real pts: %li, difference: %li\n", SeekPTS, AV->VideoFrame->pts, Difference);
                } else {
                    printf("avg: %li, real pts: %li, difference: %li difference from last pts: %li\n", SeekPTS, AV->VideoFrame->pts, AV->VideoFrame->pts - SeekPTS, AV->VideoFrame->pts - AV->PreviousPTS);
                    AV->PreviousPTS = AV->VideoFrame->pts;
                }

                uint16 Width = Source->Info.Width;
                uint16 Height = Source->Info.Height;
                uint16 BytesPerPixel = Source->Info.BytesPerPixel;
                int32 Pitch = Width*BytesPerPixel;

                cached_bitmap *Bitmap = Memory_RollingBitmap(Memory, Source, FrameToSeek);
                void *Buffer = Bitmap->Data;

                int out_linesize[4] = { Pitch, Pitch, Pitch, Pitch };
                uint8 *dst_data[4] = { (uint8 *)Buffer, (uint8 *)Buffer + Width*Height*BytesPerPixel,
                                       (uint8 *)Buffer + Width*Height*BytesPerPixel*2, (uint8 *)Buffer + Width*Height*BytesPerPixel*3 };

                // NOTE(fox): This function will be replaced in the future.
                AV->RGBContext = sws_getContext(AV->VideoFrame->width, AV->VideoFrame->height, (AVPixelFormat)AV->VideoFrame->format,
                                                AV->VideoFrame->width, AV->VideoFrame->height, AV_PIX_FMT_RGBA, SWS_BILINEAR,
                                                NULL, NULL, NULL);

                if(!AV->RGBContext) {
                    printf("Libav error: SwsContext creation failed.");
                }

                sws_scale(AV->RGBContext, AV->VideoFrame->data, AV->VideoFrame->linesize, 0, AV->VideoFrame->height,
                          dst_data, out_linesize);

                av_frame_unref(AV->VideoFrame);

                Assert(BitmapInfo->BitmapBuffer);
                return Bitmap;
            }
            else
            {
                // If this gets printed when not seeking, a frame has been skipped!
                printf("FRAME SKIP: avg: %li, real pts: %li, difference: %li\n", SeekPTS, AV->VideoFrame->pts, Difference);
            }
            av_frame_unref(AV->VideoFrame);
        }
    }
    return 0;
}