Revision f9a86917 Chunkiser/input-avs.c

View differences:

Chunkiser/input-avs.c
8 8

  
9 9
#include <chunk.h>
10 10

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

  
14
struct input_desc {
15
  AVFormatContext *s;
16
  int audio_stream;
17
  int video_stream;
18
};
19

  
15 20
struct input_desc *input_open(const char *fname)
16 21
{
17
    AVFormatContext *s;
18
    int res;
22
  struct input_desc *desc;
23
  int i, res;
19 24

  
20 25
  avcodec_register_all();
21 26
  av_register_all();
22 27

  
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
  desc = malloc(sizeof(struct input_desc));
29
  if (desc == NULL) {
30
    return NULL;
31
  }
32
  res = av_open_input_file(&desc->s, fname, NULL, 0, NULL);
33
  if (res < 0) {
34
    fprintf(stderr, "Error opening %s: %d\n", fname, res);
35

  
36
    return NULL;
37
  }
38

  
39
  res = av_find_stream_info(desc->s);
40
  if (res < 0) {
41
    fprintf(stderr, "Cannot find codec parameters for %s\n", fname);
42

  
43
    return NULL;
44
  }
45
  desc->video_stream = -1;
46
  desc->audio_stream = -1;
47
  for (i = 0; i < desc->s->nb_streams; i++) {
48
    if (desc->video_stream == -1 && desc->s->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
49
      desc->video_stream = i;
28 50
    }
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;
51
    if (desc->audio_stream == -1 && desc->s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
52
      desc->audio_stream = i;
35 53
    }
54
  }
36 55

  
37
    dump_format(s, 0, fname, 0);
56
  dump_format(desc->s, 0, fname, 0);
38 57

  
39
    return s;
58
  return desc;
40 59
}
41 60

  
42 61
void input_close(struct input_desc *s)
43 62
{
44
    av_close_input_file(s);
63
    av_close_input_file(s->s);
64
    free(s);
45 65
}
46 66

  
47 67
int input_get_1(struct input_desc *s, struct chunk *c)
......
57 77
    p = static_buff;
58 78
    if (inited == 0) {
59 79
        inited = 1;
60
        res = av_read_frame(s, &pkt);
80
        res = av_read_frame(s->s, &pkt);
61 81
        if (res < 0) {
62 82
            fprintf(stderr, "First read failed: %d!!!\n", res);
63 83

  
......
73 93
    memcpy(p, pkt.data, pkt.size);
74 94
    p += pkt.size;
75 95
    while (1) {
76
        res = av_read_frame(s, &pkt);
96
        res = av_read_frame(s->s, &pkt);
77 97
        if (res >= 0) {
78
            st = s->streams[pkt.stream_index];
98
            st = s->s->streams[pkt.stream_index];
79 99
            if (pkt.flags & PKT_FLAG_KEY) {
80 100
                c->size = p - static_buff;
81 101
                c->data = malloc(c->size);
......
116 136
    int res;
117 137
    static int cid;
118 138

  
119
    res = av_read_frame(s, &pkt);
139
    res = av_read_frame(s->s, &pkt);
120 140
    if (res < 0) {
121
      fprintf(stderr, "First read failed: %d!!!\n", res);
141
      fprintf(stderr, "AVPacket read failed: %d!!!\n", res);
142

  
143
      return -1;
144
    }
145
    if (pkt.stream_index != s->video_stream) {
146
      c->size = 0;
147
      c->data = NULL;
122 148

  
123 149
      return 0;
124 150
    }
125
    
151

  
126 152
    c->size = pkt.size;
127 153
    c->data = malloc(c->size);
128 154
    if (c->data == NULL) {
129
      return 0;
155
      return -1;
130 156
    }
131 157
    memcpy(c->data, pkt.data, c->size);
132 158
    c->attributes_size = 0;

Also available in: Unified diff