Statistics
| Branch: | Revision:

streamers / Chunkiser / input-avs.c @ fdcaec9a

History | View | Annotate | Download (4.99 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_1(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
int input_get(struct input_desc *s, struct chunk *c)
114
{
115
    AVPacket pkt;
116
    int res;
117
    static int cid;
118

    
119
    res = av_read_frame(s, &pkt);
120
    if (res < 0) {
121
      fprintf(stderr, "First read failed: %d!!!\n", res);
122

    
123
      return 0;
124
    }
125
    
126
    c->size = pkt.size;
127
    c->data = malloc(c->size);
128
    if (c->data == NULL) {
129
      av_free_packet(&pkt);
130
      return 0;
131
    }
132
    memcpy(c->data, pkt.data, c->size);
133
    c->attributes_size = 0;
134
    c->attributes = NULL;
135
    c->id = cid++; 
136
    c->timestamp = pkt.dts;
137
    av_free_packet(&pkt);
138
    return 1;
139
}
140

    
141
#if 0
142
int chunk_read_avs1(void *s_h, struct chunk *c)
143
{
144
    AVFormatContext *s = s_h;
145
    static AVPacket pkt;
146
    static int inited;
147
    AVStream *st;
148
    int res;
149
    int cnt;
150
    static uint8_t static_buff[STATIC_BUFF_SIZE];
151
    uint8_t *p, *pcurr;
152
    static uint8_t *p1;
153
    static struct chunk c2;
154
    int f1;
155
    static int f2;
156

157
    if (p1) {
158
        c2.id = c->id;
159
        *c = c2;
160
        p1 = NULL;
161

162
        return f2;
163
    }
164

165
    p = static_buff;
166
    p1 = static_buff + STATIC_BUFF_SIZE / 2;
167
    if (inited == 0) {
168
        inited = 1;
169
        res = av_read_frame(s, &pkt);
170
        if (res < 0) {
171
            fprintf(stderr, "First read failed: %d!!!\n", res);
172

173
            return 0;
174
        }
175
        if ((pkt.flags & PKT_FLAG_KEY) == 0) {
176
            fprintf(stderr, "First frame is not key frame!!!\n");
177

178
            return 0;
179
        }
180
    }
181
    cnt = 0; f1 = 0; f2 = 0;
182
    c->stride_size = 2;
183
    c2.stride_size = 2;
184
    pcurr = p1;
185
    if (pkt.size > 0) {
186
        memcpy(p, pkt.data, pkt.size);
187
        c->frame[0] = p;
188
        c->frame_len[0] = pkt.size;
189
        f1++;
190
        p += pkt.size;
191
    }
192
    while (1) {
193
        res = av_read_frame(s, &pkt);
194
        if (res >= 0) {
195
            st = s->streams[pkt.stream_index];
196
            if (pkt.flags & PKT_FLAG_KEY) {
197
                cnt++;
198
                if (cnt == 2) {
199
                    return f1;
200
                }
201
            }
202
            memcpy(pcurr, pkt.data, pkt.size);
203
            if (pcurr == p) {
204
                c->frame[f1] = pcurr;
205
                c->frame_len[f1] = pkt.size;
206
                p += pkt.size;
207
                pcurr = p1;
208
                f1++;
209
            } else {
210
                c2.frame[f2] = pcurr;
211
                c2.frame_len[f2] = pkt.size;
212
                p1 += pkt.size;
213
                pcurr = p;
214
                f2++;
215
            }
216
        } else {
217
            pkt.size = 0;
218

219
            return f1;
220
        }
221
    }
222

223
    return 0;
224
}
225
#endif