Revision a6497b4b src/Chunkiser/input-stream-avf.c

View differences:

src/Chunkiser/input-stream-avf.c
14 14
#include "chunkiser_iface.h"
15 15

  
16 16
#define STATIC_BUFF_SIZE 1000 * 1024
17
static int header_refresh_period;
18

  
19 17
struct input_stream {
20 18
  AVFormatContext *s;
21 19
  bool loop;	//loop on input file infinitely
......
23 21
  int video_stream;
24 22
  int64_t last_ts;
25 23
  int64_t base_ts;
26
  int frames_since_global_headers;
24
  AVBitStreamFilterContext *bsf[MAX_STREAMS];
27 25
};
28 26

  
29 27
static uint8_t codec_type(enum CodecID cid)
......
40 38
    case CODEC_ID_MJPEG:
41 39
      return 4;
42 40
    case CODEC_ID_MPEG4:
43
      header_refresh_period = 50;
44 41
      return 5;
45 42
    case CODEC_ID_FLV1:
46 43
      return 6;
......
49 46
    case CODEC_ID_DVVIDEO:
50 47
      return 8;
51 48
    case CODEC_ID_H264:
52
      header_refresh_period = 50;
53 49
      return 9;
54 50
    case CODEC_ID_THEORA:
55 51
    case CODEC_ID_VP3:
......
154 150
  desc->audio_stream = -1;
155 151
  desc->last_ts = 0;
156 152
  desc->base_ts = 0;
157
  desc->frames_since_global_headers = 0;
158 153
  desc->loop = 0;	// FIXME: Check config!
159 154
  for (i = 0; i < desc->s->nb_streams; i++) {
160 155
    if (desc->video_stream == -1 && desc->s->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
......
168 163
    if (desc->audio_stream == -1 && desc->s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
169 164
      desc->audio_stream = i;
170 165
    }
166
    if (desc->s->streams[i]->codec->codec_id == CODEC_ID_MPEG4) {
167
      desc->bsf[i] = av_bitstream_filter_init("dump_extra");
168
    } else {
169
      desc->bsf[i] = NULL;
170
    }
171 171
  }
172 172

  
173 173
  dump_format(desc->s, 0, fname, 0);
......
186 186
    AVPacket pkt;
187 187
    int res;
188 188
    uint8_t *data;
189
    int header_out, header_size;
189
    int header_size;
190 190

  
191 191
    res = av_read_frame(s->s, &pkt);
192 192
    if (res < 0) {
......
210 210

  
211 211
      return NULL;
212 212
    }
213
    if (s->bsf[pkt.stream_index]) {
214
      AVPacket new_pkt= pkt;
215
      int res;
216

  
217
      res = av_bitstream_filter_filter(s->bsf[pkt.stream_index],
218
                                       s->s->streams[pkt.stream_index]->codec,
219
                                       NULL, &new_pkt.data, &new_pkt.size,
220
                                       pkt.data, pkt.size, pkt.flags & AV_PKT_FLAG_KEY);
221
      if(res > 0){
222
        av_free_packet(&pkt);
223
        new_pkt.destruct= av_destruct_packet;
224
      } else if(res < 0){
225
        fprintf(stderr, "%s failed for stream %d, codec %s: ",
226
                        s->bsf[pkt.stream_index]->filter->name,
227
                        pkt.stream_index,
228
                        s->s->streams[pkt.stream_index]->codec->codec->name);
229
        fprintf(stderr, "%d\n", res);
230
        *size = 0;
231

  
232
        return NULL;
233
      }
234
      pkt= new_pkt;
235
    }
213 236

  
214 237
    if (s->s->streams[pkt.stream_index]->codec->codec_type == CODEC_TYPE_VIDEO) {
215 238
      header_size = VIDEO_PAYLOAD_HEADER_SIZE;
216 239
    }
217
    if (header_refresh_period) {
218
      header_out = (pkt.flags & PKT_FLAG_KEY) != 0;
219
      if (header_out == 0) {
220
        s->frames_since_global_headers++;
221
        if (s->frames_since_global_headers == header_refresh_period) {
222
          s->frames_since_global_headers = 0;
223
          header_out = 1;
224
        }
225
      }
226
    } else {
227
      header_out = 0;
228
    }
229
    *size = pkt.size + s->s->streams[pkt.stream_index]->codec->extradata_size * header_out + header_size + FRAME_HEADER_SIZE;
240
    *size = pkt.size + header_size + FRAME_HEADER_SIZE;
230 241
    data = malloc(*size);
231 242
    if (data == NULL) {
232 243
      *size = -1;
......
240 251
    data[VIDEO_PAYLOAD_HEADER_SIZE - 1] = 1;
241 252
    frame_header_fill(data + VIDEO_PAYLOAD_HEADER_SIZE, *size - header_size - FRAME_HEADER_SIZE, &pkt, s->s->streams[pkt.stream_index], s->base_ts);
242 253

  
243
    if (header_out && s->s->streams[pkt.stream_index]->codec->extradata_size) {
244
      memcpy(data + header_size + FRAME_HEADER_SIZE, s->s->streams[pkt.stream_index]->codec->extradata, s->s->streams[pkt.stream_index]->codec->extradata_size);
245
      memcpy(data + header_size + FRAME_HEADER_SIZE + s->s->streams[pkt.stream_index]->codec->extradata_size, pkt.data, pkt.size);
246
    } else {
247
      memcpy(data + header_size + FRAME_HEADER_SIZE, pkt.data, pkt.size);
248
    }
254
    memcpy(data + header_size + FRAME_HEADER_SIZE, pkt.data, pkt.size);
249 255
    *ts = av_rescale_q(pkt.dts, s->s->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
250 256
    //dprintf("pkt.dts=%ld TS1=%lu" , pkt.dts, *ts);
251 257
    *ts += s->base_ts;

Also available in: Unified diff