Statistics
| Branch: | Revision:

psnr-tools / Chunker / chunker.c @ 812005f2

History | View | Annotate | Download (5.22 KB)

1
#include <avformat.h>
2

    
3
#include "input.h"
4
#include "decode.h"
5
#include <float.h>
6

    
7
#define N 100000
8

    
9
static int fsizes[N];
10
static double ftimes[N];
11
static uint8_t ftypes[N];
12

    
13
void print_chunk(int chunk_id, int *chunk_frame, int chunk_dimension, double min_time, double max_time, int chunk_size, int chunk_prio)
14
{
15
  int j;
16
    
17
  printf("%d\t%f\t%f\t%d\t1\t%d :\t", chunk_id, min_time, max_time, chunk_size, chunk_prio);
18
  for (j = 0; j < chunk_dimension; j++) {
19
    printf("%d ", chunk_frame[j]);
20
  }
21
  printf("\n");
22
}
23

    
24
void chunkise_ipb(int n, int ngops)
25
{
26
  int i, chunk_id = 0;
27
  int i_chunk_dimension = 0, p_chunk_dimension = 0, b_chunk_dimension = 0;
28
  int i_chunk_size = 0, p_chunk_size = 0, b_chunk_size = 0;
29
  double i_min_time = 0, p_min_time = 0, b_min_time = 0;
30
  double i_max_time = 0, p_max_time = 0, b_max_time = 0;
31
  int igops=0;
32

    
33
  int i_chunk_frame[50], b_chunk_frame[50], p_chunk_frame[50];
34
  for (i = 0; i < n; i++) {
35

    
36
    switch (ftypes[i]) {
37
      case 1:
38
        /* I Type */
39
        if (igops == ngops) {
40
          if (i_chunk_dimension) {
41
            print_chunk(chunk_id++, i_chunk_frame, i_chunk_dimension, i_min_time, i_max_time, i_chunk_size, 3);
42
          }
43
          if (p_chunk_dimension) {
44
            print_chunk(chunk_id++, p_chunk_frame, p_chunk_dimension, p_min_time, p_max_time, p_chunk_size, 2);
45
          }
46
          if (b_chunk_dimension) {
47
            print_chunk(chunk_id++, b_chunk_frame, b_chunk_dimension, b_min_time, b_max_time, b_chunk_size, 1);
48
          }
49
          i_chunk_size = p_chunk_size = b_chunk_size = 0;
50
          i_chunk_dimension = p_chunk_dimension = b_chunk_dimension = 0;
51
          i_min_time = p_min_time = b_min_time = DBL_MAX;
52
          i_max_time = p_max_time = b_max_time = 0;
53
          igops = 0;
54
        }
55
        i_chunk_size += fsizes[i];
56
        i_chunk_frame[i_chunk_dimension++] = i;
57
        i_min_time = i_min_time<ftimes[i] ? i_min_time : ftimes[i];
58
        i_max_time = i_max_time>ftimes[i] ? i_max_time : ftimes[i];
59
        igops++;
60
        break;
61
      case 2:
62
        /* P Type */
63
        p_chunk_size += fsizes[i];
64
        p_chunk_frame[p_chunk_dimension++] = i;
65
        p_min_time = p_min_time<ftimes[i] ? p_min_time : ftimes[i];
66
        p_max_time = p_max_time>ftimes[i] ? p_max_time : ftimes[i];
67
        break;
68
      case 3:
69
        /* B Type */
70
        b_chunk_size += fsizes[i];
71
        b_chunk_frame[b_chunk_dimension++] = i;
72
        b_min_time = b_min_time<ftimes[i] ? b_min_time : ftimes[i];
73
        b_max_time = b_max_time>ftimes[i] ? b_max_time : ftimes[i];
74
        break;
75
      default:
76
        i=n; //TODO: check what frame type=0 means
77
    }
78
  }
79
  if (i_chunk_dimension) {
80
    print_chunk(chunk_id++, i_chunk_frame, i_chunk_dimension, i_min_time, i_max_time, i_chunk_size, 3);
81
  }
82
  if (p_chunk_dimension) {
83
    print_chunk(chunk_id++, p_chunk_frame, p_chunk_dimension, p_min_time, p_max_time, p_chunk_size, 2);
84
  }
85
  if (b_chunk_dimension) {
86
    print_chunk(chunk_id++, b_chunk_frame, b_chunk_dimension, b_min_time, b_max_time, b_chunk_size, 1);
87
  }
88
}
89

    
90
void chunkise_gop(int n, int ngops)
91
{
92
  int i, chunk_id = 0;
93
  int chunk_dimension = 0;
94
  int chunk_size = 0;
95
  double min_time = 0;
96
  double max_time = 0;
97
  int igops=0;
98

    
99
  int chunk_frame[150];
100
  for (i = 0; i < n; i++) {
101

    
102
    if (ftypes[i] == 1) {
103
      /* I Type */
104
      if (igops == ngops) {
105
        if (chunk_dimension) {
106
          print_chunk(chunk_id++, chunk_frame, chunk_dimension, min_time, max_time, chunk_size, 1);
107
        }
108
        chunk_size = 0;
109
        chunk_dimension = 0;
110
        min_time = DBL_MAX;
111
        max_time = 0;
112
        igops = 0;
113
      }
114
      igops++;
115
    }
116
    chunk_size += fsizes[i];
117
    chunk_frame[chunk_dimension++] = i;
118
    min_time = min_time<ftimes[i] ? min_time : ftimes[i];
119
    max_time = max_time>ftimes[i] ? max_time : ftimes[i];
120
  }
121
  if (chunk_dimension) {
122
    print_chunk(chunk_id++, chunk_frame, chunk_dimension, min_time, max_time, chunk_size, 1);
123
  }
124
}
125

    
126
int frames(const char* sfile){
127

    
128
  AVFormatContext *s;
129
  int done, cnt;
130
  int i;
131

    
132
  avcodec_register_all();
133
  av_register_all();
134

    
135
  s = open_input_stream(sfile);
136
  if (s == NULL) {
137
    fprintf(stderr, "Cannot open input file %s\n", sfile);
138

    
139
    return -1;
140
  }
141
  codec_open(s);
142

    
143
  done = 0; cnt = 0;
144
  while (!done) {
145
    AVPacket *pkt;
146

    
147
    pkt = read_input_packet(s);
148
    if (pkt == NULL) {
149
      done = 1;
150
    } else {
151
      if (s->streams[pkt->stream_index]->codec->codec_type == CODEC_TYPE_VIDEO) {
152
        uint8_t type;
153

    
154
        fsizes[cnt] = pkt->size;
155
        ftimes[cnt] = (double)pkt->dts/AV_TIME_BASE;
156
        i = pkt_decode(s, pkt, cnt, &type);
157
        if (i >= 0) {
158
          ftypes[i] = type;
159
        }
160
        cnt++;
161
      }
162
      av_free_packet(pkt);
163
    }
164
  }
165

    
166
  for (i = 0; i < cnt; i++) {
167
    fprintf(stderr,"%d\t%d %d\n", i, fsizes[i], ftypes[i]);
168
  }
169

    
170
  return cnt;
171
}
172

    
173
//usage: videofile chunksize
174
int main(int argc, char *argv[])
175
{
176
  int numframes = frames(argv[1]);
177
  
178
  int cmode = atoi(argv[2]);
179
  int cparam = atoi(argv[3]);
180
  switch (cmode) {
181
  case 0:
182
   //TBD support fixed sized chunks
183
    break;
184
  case 1:
185
    chunkise_gop(numframes,cparam);
186
    break;
187
  case 2:
188
    //chunkise_frame(numframes,cparam);
189
    break;
190
  case 3:
191
    chunkise_ipb(numframes,cparam);
192
    break;
193
  default:
194
    fprintf(stderr,"unsupported mode %d\n", cmode);
195
   break;
196
  }
197

    
198
  return 0;
199
}