Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.48 KB)

1 ad2a1304 Csaba Kiraly
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6
7 5b504d3f Csaba Kiraly
#include <libavformat/avformat.h>
8 a6862d0c luca
9
#include "chunk.h"
10 681de281 luca
#include "input-avs.h"
11 bbc3dabb Csaba Kiraly
#define STATIC_BUFF_SIZE 1000 * 1024
12 5b504d3f Csaba Kiraly
13 681de281 luca
void *input_stream_open_avs(const char *fname, int chunk_size)
14 5b504d3f Csaba Kiraly
{
15
    AVFormatContext *s;
16
    int res;
17
18
  avcodec_register_all();
19
  av_register_all();
20
21
    if (chunk_size != 0) {
22
      fprintf(stderr, "Fixed chunk size is currently unsupported!!!\n");
23
    }
24
    res = av_open_input_file(&s, fname, NULL, 0, NULL);
25
    if (res < 0) {
26
        fprintf(stderr, "Error opening %s: %d\n", fname, res);
27
28
        return NULL;
29
    }
30
31
    res = av_find_stream_info(s);
32
    if (res < 0) {
33
        fprintf(stderr, "Cannot find codec parameters for %s\n", fname);
34
35
        return NULL;
36
    }
37
38
    dump_format(s, 0, fname, 0);
39
40
    return s;
41
}
42
43 681de281 luca
void close_input_stream_avs(void *s)
44 5b504d3f Csaba Kiraly
{
45
    av_close_input_file(s);
46
}
47
48 a6862d0c luca
int chunk_read_avs(void *s_h, struct chunk *c)
49 5b504d3f Csaba Kiraly
{
50
    AVFormatContext *s = s_h;
51
    static AVPacket pkt;
52
    static int inited;
53
    AVStream *st;
54
    int res;
55
    static uint8_t static_buff[STATIC_BUFF_SIZE];
56
    uint8_t *p;
57
58
    p = static_buff;
59
    if (inited == 0) {
60
        inited = 1;
61
        res = av_read_frame(s, &pkt);
62
        if (res < 0) {
63
            fprintf(stderr, "First read failed: %d!!!\n", res);
64
65 a6862d0c luca
            return 0;
66 5b504d3f Csaba Kiraly
        }
67 a6862d0c luca
        if ((pkt.flags & PKT_FLAG_KEY) == 0) {
68 5b504d3f Csaba Kiraly
            fprintf(stderr, "First frame is not key frame!!!\n");
69
70 a6862d0c luca
            return 0;
71 5b504d3f Csaba Kiraly
        }
72
    }
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 a6862d0c luca
                c->frame[0] = static_buff;
81
                c->frame_len[0] = p - static_buff;
82
                c->stride_size = 1;
83
                return 1;
84 5b504d3f Csaba Kiraly
            }
85
            memcpy(p, pkt.data, pkt.size);
86
            p += pkt.size;
87
        } else {
88 a6862d0c luca
            if (p - static_buff > 0) {
89
                c->frame[0] = static_buff;
90
                c->frame_len[0] = p - static_buff;
91
                c->stride_size = 1;
92
                return 1;
93 5b504d3f Csaba Kiraly
            }
94 a6862d0c luca
            return 0;
95
        }
96
    }
97
98
    return 0;
99
}
100
101
int chunk_read_avs1(void *s_h, struct chunk *c)
102
{
103
    AVFormatContext *s = s_h;
104
    static AVPacket pkt;
105
    static int inited;
106
    AVStream *st;
107
    int res;
108
    int cnt;
109
    static uint8_t static_buff[STATIC_BUFF_SIZE];
110
    uint8_t *p, *pcurr;
111
    static uint8_t *p1;
112
    static struct chunk c2;
113
    int f1;
114
    static int f2;
115
116
    if (p1) {
117
        c2.id = c->id;
118
        *c = c2;
119
        p1 = NULL;
120
121
        return f2;
122
    }
123
124
    p = static_buff;
125
    p1 = static_buff + STATIC_BUFF_SIZE / 2;
126
    if (inited == 0) {
127
        inited = 1;
128
        res = av_read_frame(s, &pkt);
129
        if (res < 0) {
130
            fprintf(stderr, "First read failed: %d!!!\n", res);
131
132
            return 0;
133
        }
134
        if ((pkt.flags & PKT_FLAG_KEY) == 0) {
135
            fprintf(stderr, "First frame is not key frame!!!\n");
136
137
            return 0;
138
        }
139
    }
140
    cnt = 0; f1 = 0; f2 = 0;
141
    c->stride_size = 2;
142
    c2.stride_size = 2;
143
    pcurr = p1;
144
    if (pkt.size > 0) {
145
        memcpy(p, pkt.data, pkt.size);
146
        c->frame[0] = p;
147
        c->frame_len[0] = pkt.size;
148
        f1++;
149
        p += pkt.size;
150
    }
151
    while (1) {
152
        res = av_read_frame(s, &pkt);
153
        if (res >= 0) {
154
            st = s->streams[pkt.stream_index];
155
            if (pkt.flags & PKT_FLAG_KEY) {
156
                cnt++;
157
                if (cnt == 2) {
158
                    return f1;
159
                }
160
            }
161
            memcpy(pcurr, pkt.data, pkt.size);
162
            if (pcurr == p) {
163
                c->frame[f1] = pcurr;
164
                c->frame_len[f1] = pkt.size;
165
                p += pkt.size;
166
                pcurr = p1;
167
                f1++;
168
            } else {
169
                c2.frame[f2] = pcurr;
170
                c2.frame_len[f2] = pkt.size;
171
                p1 += pkt.size;
172
                pcurr = p;
173
                f2++;
174
            }
175
        } else {
176
            pkt.size = 0;
177
178
            return f1;
179 5b504d3f Csaba Kiraly
        }
180
    }
181
182 a6862d0c luca
    return 0;
183 5b504d3f Csaba Kiraly
}
184 491ea146 Csaba Kiraly
185
int chunk_read_avs2(void *s_h, struct chunk *c)
186
{
187
    AVFormatContext *s = s_h;
188
    static AVPacket pkt;
189
    int res;
190
    static uint8_t static_buff[STATIC_BUFF_SIZE];
191
192
    res = av_read_frame(s, &pkt);
193
    if (res >= 0) {
194
      memcpy(static_buff, pkt.data, pkt.size);
195
      c->frame[0] = static_buff;
196
      c->frame_len[0] = pkt.size;
197
      c->stride_size = 1;
198
      return 1;
199
    }
200
201
    return 0;
202
}
203