Statistics
| Branch: | Revision:

streamers / Chunkiser / input-avs.c @ 4bb789ed

History | View | Annotate | Download (4.44 KB)

1
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6

    
7
#include <libavformat/avformat.h>
8

    
9
#include <chunk.h>
10

    
11
#define input_desc AVFormatContext
12
#include "../input.h"
13
#define STATIC_BUFF_SIZE 1000 * 1024
14

    
15
struct input_desc *input_open(const char *fname)
16
{
17
    AVFormatContext *s;
18
    int res;
19

    
20
  avcodec_register_all();
21
  av_register_all();
22

    
23
    res = av_open_input_file(&s, fname, NULL, 0, NULL);
24
    if (res < 0) {
25
        fprintf(stderr, "Error opening %s: %d\n", fname, res);
26

    
27
        return NULL;
28
    }
29

    
30
    res = av_find_stream_info(s);
31
    if (res < 0) {
32
        fprintf(stderr, "Cannot find codec parameters for %s\n", fname);
33

    
34
        return NULL;
35
    }
36

    
37
    dump_format(s, 0, fname, 0);
38

    
39
    return s;
40
}
41

    
42
void input_close(struct input_desc *s)
43
{
44
    av_close_input_file(s);
45
}
46

    
47
int input_get(struct input_desc *s, struct chunk *c)
48
{
49
    static AVPacket pkt;
50
    static int inited;
51
    AVStream *st;
52
    int res;
53
    static uint8_t static_buff[STATIC_BUFF_SIZE];
54
    static int cid;
55
    uint8_t *p;
56

    
57
    p = static_buff;
58
    if (inited == 0) {
59
        inited = 1;
60
        res = av_read_frame(s, &pkt);
61
        if (res < 0) {
62
            fprintf(stderr, "First read failed: %d!!!\n", res);
63

    
64
            return 0;
65
        }
66
        if ((pkt.flags & PKT_FLAG_KEY) == 0) {
67
            fprintf(stderr, "First frame is not key frame!!!\n");
68

    
69
            return 0;
70
        }
71
    }
72
    c->timestamp = pkt.dts;
73
    memcpy(p, pkt.data, pkt.size);
74
    p += pkt.size;
75
    while (1) {
76
        res = av_read_frame(s, &pkt);
77
        if (res >= 0) {
78
            st = s->streams[pkt.stream_index];
79
            if (pkt.flags & PKT_FLAG_KEY) {
80
                c->size = p - static_buff;
81
                c->data = malloc(c->size);
82
                if (c->data == NULL) {
83
                  return 0;
84
                }
85
                memcpy(c->data, static_buff, c->size);
86
                c->attributes_size = 0;
87
                c->attributes = NULL;
88
                c->id = cid++; 
89
                return 1;
90
            }
91
            memcpy(p, pkt.data, pkt.size);
92
            p += pkt.size;
93
        } else {
94
            if (p - static_buff > 0) {
95
                c->size = p - static_buff;
96
                c->data = malloc(c->size);
97
                if (c->data == NULL) {
98
                  return 0;
99
                }
100
                memcpy(c->data, static_buff, c->size);
101
                c->attributes_size = 0;
102
                c->attributes = NULL;
103
                c->id = cid++; 
104
                return 1;
105
            }
106
            return 0;
107
        }
108
    }
109

    
110
    return 0;
111
}
112

    
113
#if 0
114
int chunk_read_avs1(void *s_h, struct chunk *c)
115
{
116
    AVFormatContext *s = s_h;
117
    static AVPacket pkt;
118
    static int inited;
119
    AVStream *st;
120
    int res;
121
    int cnt;
122
    static uint8_t static_buff[STATIC_BUFF_SIZE];
123
    uint8_t *p, *pcurr;
124
    static uint8_t *p1;
125
    static struct chunk c2;
126
    int f1;
127
    static int f2;
128

129
    if (p1) {
130
        c2.id = c->id;
131
        *c = c2;
132
        p1 = NULL;
133

134
        return f2;
135
    }
136

137
    p = static_buff;
138
    p1 = static_buff + STATIC_BUFF_SIZE / 2;
139
    if (inited == 0) {
140
        inited = 1;
141
        res = av_read_frame(s, &pkt);
142
        if (res < 0) {
143
            fprintf(stderr, "First read failed: %d!!!\n", res);
144

145
            return 0;
146
        }
147
        if ((pkt.flags & PKT_FLAG_KEY) == 0) {
148
            fprintf(stderr, "First frame is not key frame!!!\n");
149

150
            return 0;
151
        }
152
    }
153
    cnt = 0; f1 = 0; f2 = 0;
154
    c->stride_size = 2;
155
    c2.stride_size = 2;
156
    pcurr = p1;
157
    if (pkt.size > 0) {
158
        memcpy(p, pkt.data, pkt.size);
159
        c->frame[0] = p;
160
        c->frame_len[0] = pkt.size;
161
        f1++;
162
        p += pkt.size;
163
    }
164
    while (1) {
165
        res = av_read_frame(s, &pkt);
166
        if (res >= 0) {
167
            st = s->streams[pkt.stream_index];
168
            if (pkt.flags & PKT_FLAG_KEY) {
169
                cnt++;
170
                if (cnt == 2) {
171
                    return f1;
172
                }
173
            }
174
            memcpy(pcurr, pkt.data, pkt.size);
175
            if (pcurr == p) {
176
                c->frame[f1] = pcurr;
177
                c->frame_len[f1] = pkt.size;
178
                p += pkt.size;
179
                pcurr = p1;
180
                f1++;
181
            } else {
182
                c2.frame[f2] = pcurr;
183
                c2.frame_len[f2] = pkt.size;
184
                p1 += pkt.size;
185
                pcurr = p;
186
                f2++;
187
            }
188
        } else {
189
            pkt.size = 0;
190

191
            return f1;
192
        }
193
    }
194

195
    return 0;
196
}
197
#endif