Statistics
| Branch: | Revision:

psnr-tools / Chunker / chunker.c @ 5199de4e

History | View | Annotate | Download (3.85 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)
25
{
26
  int i, chunk_id = 0;
27
  int p_chunk_dimension = 0, b_chunk_dimension = 0;
28
  int p_chunk_size = 0, b_chunk_size = 0;
29
  double p_min_time = 0, b_min_time = 0;
30
  double p_max_time = 0, b_max_time = 0;
31

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

    
35
    switch (ftypes[i]) {
36
      case 1:
37
        /* I Type */
38
        if (p_chunk_dimension) {
39
          print_chunk(chunk_id++, p_chunk_frame, p_chunk_dimension, p_min_time, p_max_time, p_chunk_size, 2);
40
        }
41
        if (b_chunk_dimension) {
42
          print_chunk(chunk_id++, b_chunk_frame, b_chunk_dimension, b_min_time, b_max_time, b_chunk_size, 1);
43
        }
44
        p_chunk_size = b_chunk_size = 0;
45
        p_chunk_dimension = b_chunk_dimension = 0;
46
        p_min_time = b_min_time = DBL_MAX;
47
        p_max_time = b_max_time = 0;
48
        printf("%d\t%f\t%f\t%d\t1\t3 :\t%d\n", chunk_id++, ftimes[i], ftimes[i], fsizes[i], i);
49
        break;
50
      case 2:
51
        /* P Type */
52
        p_chunk_size += fsizes[i];
53
        p_chunk_frame[p_chunk_dimension++] = i;
54
        p_min_time = p_min_time<ftimes[i] ? p_min_time : ftimes[i];
55
        p_max_time = p_max_time>ftimes[i] ? p_max_time : ftimes[i];
56
        break;
57
      case 3:
58
        /* B Type */
59
        b_chunk_size += fsizes[i];
60
        b_chunk_frame[b_chunk_dimension++] = i;
61
        b_min_time = b_min_time<ftimes[i] ? b_min_time : ftimes[i];
62
        b_max_time = b_max_time>ftimes[i] ? b_max_time : ftimes[i];
63
        break;
64
      default:
65
        return;
66
    }
67
  }
68
}
69

    
70
void chunkise_gop(int n)
71
{
72
  int i, chunk_id = 0;
73
  int chunk_dimension = 0;
74
  int chunk_size = 0;
75
  double min_time = 0;
76
  double max_time = 0;
77

    
78
  for (i = 0; i < n; i++) {
79
    int chunk_frame[150];
80

    
81
    if (ftypes[i] == 1) {
82
      /* I Type */
83
      if (chunk_dimension) {
84
        print_chunk(chunk_id++, chunk_frame, chunk_dimension, min_time, max_time, chunk_size, 1);
85
      }
86
      chunk_size = 0;
87
      chunk_dimension = 0;
88
      min_time = DBL_MAX;
89
      max_time = 0;
90
    }
91
    chunk_size += fsizes[i];
92
    chunk_frame[chunk_dimension++] = i;
93
    min_time = min_time<ftimes[i] ? min_time : ftimes[i];
94
    max_time = max_time>ftimes[i] ? max_time : ftimes[i];
95
  }
96
}
97

    
98
int frames(const char* sfile){
99

    
100
  AVFormatContext *s;
101
  int done, cnt;
102
  int i;
103

    
104
  avcodec_register_all();
105
  av_register_all();
106

    
107
  s = open_input_stream(sfile);
108
  if (s == NULL) {
109
    fprintf(stderr, "Cannot open input file %s\n", sfile);
110

    
111
    return -1;
112
  }
113
  codec_open(s);
114

    
115
  done = 0; cnt = 0;
116
  while (!done) {
117
    AVPacket *pkt;
118

    
119
    pkt = read_input_packet(s);
120
    if (pkt == NULL) {
121
      done = 1;
122
    } else {
123
      if (s->streams[pkt->stream_index]->codec->codec_type == CODEC_TYPE_VIDEO) {
124
        uint8_t type;
125

    
126
        fsizes[cnt] = pkt->size;
127
        ftimes[cnt] = (double)pkt->dts/AV_TIME_BASE;
128
        i = pkt_decode(s, pkt, cnt, &type);
129
        if (i >= 0) {
130
          ftypes[i] = type;
131
        }
132
        cnt++;
133
      }
134
      av_free_packet(pkt);
135
    }
136
  }
137

    
138
  for (i = 0; i < cnt; i++) {
139
    fprintf(stderr,"%d\t%d %d\n", i, fsizes[i], ftypes[i]);
140
  }
141

    
142
  return cnt;
143
}
144

    
145
//usage: videofile chunksize
146
int main(int argc, char *argv[])
147
{
148
  int numframes = frames(argv[1]);
149
  
150
  int csize = atoi(argv[2]);
151
  switch (csize) {
152
  case -2:
153
    chunkise_ipb(numframes);
154
    break;
155
  case 0:
156
    chunkise_gop(numframes);
157
    break;
158
  default:
159
   //TBD support fixed sized chunks
160
   break;
161
  }
162

    
163
  return 0;
164
}