aboutsummaryrefslogtreecommitdiffstats
path: root/src/video.zig
blob: 9cf50eb4c0554a4860696a1178c02492c4ff04d8 (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
const std = @import("std");
const c = @import("c.zig");
const gl = @import("gl.zig");
const build_config = @import("build_config");
const dec = @import("video/decoder.zig");
const ffmpeg = if (build_config.have_ffmpeg) @import("video/ffmpeg.zig") else unreachable;
const hap = if (build_config.have_hap) @import("video/hap.zig") else unreachable;

const check = dec.check;

pub fn loadVideo(progress_root: std.Progress.Node, filename: [*:0]const u8, texture_type: gl.Texture.Type) !gl.Texture {
    if (!build_config.have_ffmpeg) return error.texturesDisabled;

    const progress = progress_root.start("loading texture", 2);
    defer progress.end();

    var format_ctx: ?*c.AVFormatContext = null;
    try check(c.avformat_open_input(&format_ctx, filename, null, null));
    defer c.avformat_close_input(&format_ctx);

    if (format_ctx) |format| {
        try check(c.avformat_find_stream_info(format, null));

        const video_stream = for (format.streams, 0..format.nb_streams) |c_stream, _| {
            const stream = @as(*c.AVStream, c_stream orelse unreachable);
            if (stream.codecpar.*.codec_type == c.AVMEDIA_TYPE_VIDEO) break stream;
        } else unreachable;

        const codec_par = video_stream.codecpar.*;
        std.debug.print("loading codec, size {}x{}, ~{} frames\n", .{ codec_par.width, codec_par.height, video_stream.nb_frames });

        var packet = c.av_packet_alloc();
        defer c.av_packet_free(&packet);

        const allocator = std.heap.c_allocator;

        const is_hap = codec_par.codec_tag == c.MKTAG('H', 'a', 'p', '1') or codec_par.codec_tag == c.MKTAG('H', 'a', 'p', '5');
        const decoder = if (is_hap and build_config.have_hap)
            try hap.HAPDecoder.init(allocator, codec_par)
        else
            try ffmpeg.AVDecoder.init(allocator, codec_par);

        defer decoder.deinit(allocator);

        return try decoder.createTexture(progress, format, video_stream, texture_type);
    } else unreachable;
}