Statistics
| Branch: | Revision:

psnr-tools / Cutter / input-avs.c @ bbc3dabb

History | View | Annotate | Download (1.92 KB)

1
#include <libavformat/avformat.h>
2
#include "input.h"
3

    
4
#define STATIC_BUFF_SIZE 1000 * 1024
5

    
6
void *input_stream_open(const char *fname, int chunk_size)
7
{
8
    AVFormatContext *s;
9
    int res;
10

    
11
  avcodec_register_all();
12
  av_register_all();
13

    
14
    if (chunk_size != 0) {
15
      fprintf(stderr, "Fixed chunk size is currently unsupported!!!\n");
16
    }
17
    res = av_open_input_file(&s, fname, NULL, 0, NULL);
18
    if (res < 0) {
19
        fprintf(stderr, "Error opening %s: %d\n", fname, res);
20

    
21
        return NULL;
22
    }
23

    
24
    res = av_find_stream_info(s);
25
    if (res < 0) {
26
        fprintf(stderr, "Cannot find codec parameters for %s\n", fname);
27

    
28
        return NULL;
29
    }
30

    
31
    dump_format(s, 0, fname, 0);
32

    
33
    return s;
34
}
35

    
36
void close_input_stream(void *s)
37
{
38
    av_close_input_file(s);
39
}
40

    
41
void *chunk_read(void *s_h, int *size)
42
{
43
    AVFormatContext *s = s_h;
44
    static AVPacket pkt;
45
    static int inited;
46
    AVStream *st;
47
    int res;
48
    int done;
49
    static uint8_t static_buff[STATIC_BUFF_SIZE];
50
    uint8_t *p;
51

    
52
    *size = 0;
53
    p = static_buff;
54
    if (inited == 0) {
55
        inited = 1;
56
        res = av_read_frame(s, &pkt);
57
        if (res < 0) {
58
            fprintf(stderr, "First read failed: %d!!!\n", res);
59

    
60
            return NULL;
61
        }
62
        if (pkt.flags & PKT_FLAG_KEY == 0) {
63
            fprintf(stderr, "First frame is not key frame!!!\n");
64

    
65
            return NULL;
66
        }
67
    }
68
    memcpy(p, pkt.data, pkt.size);
69
    *size += pkt.size;
70
    p += pkt.size;
71
    while (1) {
72
        res = av_read_frame(s, &pkt);
73
        if (res >= 0) {
74
            st = s->streams[pkt.stream_index];
75
            if (pkt.flags & PKT_FLAG_KEY) {
76
                return static_buff;
77
            }
78
            memcpy(p, pkt.data, pkt.size);
79
            *size += pkt.size;
80
            p += pkt.size;
81
        } else {
82
            if (*size > 0) {
83
                return static_buff;
84
            }
85
            return NULL;
86
        }
87
    }
88

    
89
    return NULL;
90
}