Revision 203504d9

View differences:

libavformat/nut.c
27 27
 * - checksumming
28 28
 * - correct rate denom/nom and sample_mul
29 29
 * - correct timestamp handling
30
 * - correct startcodes
31 30
 * - index writing
32 31
 * - info and index packet reading support
33 32
 * - startcode searching for broken streams
......
114 113
    return 0;
115 114
}
116 115

  
117
static int get_padding(NUTContext *nut, ByteIOContext *bc)
118
{
119
    int i, tmp, len = nut->curr_frame_size - (url_ftell(bc) - nut->curr_frame_start);
116
/**
117
 * 
118
 */
119
static int get_length(uint64_t val){
120
    int i;
120 121

  
121
    for (i = 0; i < len; i++)
122
    {
123
	tmp = get_byte(bc);
124
	if (tmp != 0)
125
	    fprintf(stderr, "bad padding\n");
126
    }
122
    for (i=7; ; i+=7)
123
	if ((val>>i) == 0)
124
	    return i;
127 125

  
128
    return 0;
126
    return 7; //not reached
129 127
}
130 128

  
131 129
static int put_v(ByteIOContext *bc, uint64_t val)
......
139 137
	return -1;
140 138

  
141 139
    val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
142
    for (i=7; ; i+=7)
143
	if ((val>>i) == 0)
144
	    break;
140
    i= get_length(val);
145 141

  
146 142
    for (i-=7; i>0; i-=7){
147 143
	put_byte(bc, 0x80 | (val>>i));
......
171 167
    return 0;
172 168
}
173 169

  
174
static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int est_size)
170
static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size)
175 171
{
176 172
    put_flush_packet(bc);
177 173
    nut->curr_frame_start = url_ftell(bc);
178
    nut->curr_frame_size = est_size;
174
    nut->curr_frame_size = max_size;
179 175
    
180 176
    /* packet header */
181 177
    put_v(bc, nut->curr_frame_size); /* forward ptr */
......
188 184
    return 0;
189 185
}
190 186

  
191
static int put_padding(NUTContext *nut, ByteIOContext *bc)
192
{
193
    int i, len = nut->curr_frame_size - (url_ftell(bc) - nut->curr_frame_start);
187
static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size){
188
    offset_t start= nut->curr_frame_start;
189
    offset_t cur= url_ftell(bc);
190
    int size= cur - start + additional_size;
191
    
192
    assert( size <= nut->curr_frame_size );
193
    
194
    url_fseek(bc, start, SEEK_SET);
195
    put_v(bc, size);
196
    if(get_length(size) < get_length(nut->curr_frame_size))
197
        put_byte(bc, 0x80);
198
    nut->curr_frame_size= size;
199
    dprintf("Packet update: size: %d\n", size);
200

  
201
    url_fseek(bc, cur, SEEK_SET);    
194 202
    
195
    put_flush_packet(bc);
196
    for (i = 0; i < len; i++)
197
	put_byte(bc, 0);
198

  
199
    dprintf("padded %d bytes\n", i);
200

  
201 203
    return 0;
202 204
}
203 205

  
......
215 217
	    stream_length = s->streams[i]->duration * (AV_TIME_BASE / 1000);
216 218
    }
217 219

  
218
    put_packetheader(nut, bc, 16); /* FIXME: estimation */
220
    put_packetheader(nut, bc, 120);
219 221
    
220 222
    /* main header */
221 223
    put_be64(bc, MAIN_STARTCODE);
......
223 225
    put_v(bc, s->nb_streams);
224 226
    put_v(bc, 0); /* file size */
225 227
    put_v(bc, stream_length); /* len in msec */
226
    put_padding(nut, bc);
227 228
    put_be32(bc, 0); /* FIXME: checksum */
228 229
    
230
    update_packetheader(nut, bc, 0);
231
    
229 232
    /* stream headers */
230 233
    for (i = 0; i < s->nb_streams; i++)
231 234
    {
232 235
	codec = &s->streams[i]->codec;
233 236
	
234
	put_packetheader(nut, bc, 64); /* FIXME: estimation */
237
	put_packetheader(nut, bc, 120);
235 238
	put_be64(bc, STREAM_STARTCODE);
236 239
	put_v(bc, s->streams[i]->index);
237 240
	put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0);
......
266 269
	    case CODEC_TYPE_AUDIO:
267 270
		put_v(bc, codec->sample_rate / (double)(codec->frame_rate_base / codec->frame_rate));
268 271
		put_v(bc, codec->channels);
269
		put_padding(nut, bc);
270 272
		put_be32(bc, 0); /* FIXME: checksum */
271 273
		break;
272 274
	    case CODEC_TYPE_VIDEO:
......
275 277
		put_v(bc, 0); /* aspected w */
276 278
		put_v(bc, 0); /* aspected h */
277 279
		put_v(bc, 0); /* csp type -- unknown */
278
		put_padding(nut, bc);
279 280
		put_be32(bc, 0); /* FIXME: checksum */
280 281
		break;
281 282
	}
283
        update_packetheader(nut, bc, 0);
282 284
    }
283 285

  
284 286
#if 0
285 287
    /* info header */
286 288
    put_packetheader(nut, bc, 16+strlen(s->author)+strlen(s->title)+
287
        strlen(s->comment)+strlen(s->copyright)); /* FIXME: estimation */
289
        strlen(s->comment)+strlen(s->copyright)); 
288 290
    put_be64(bc, INFO_STARTCODE);
289 291
    if (s->author[0])
290 292
    {
......
310 312
    put_v(bc, 9); /* type */
311 313
    put_b(bc, LIBAVFORMAT_IDENT, strlen(LIBAVFORMAT_IDENT));
312 314

  
313
    put_padding(nut, bc);
314 315
    put_be32(bc, 0); /* FIXME: checksum */
316
    update_packetheader(nut, bc, 0);
315 317
#endif
316 318
        
317 319
    put_flush_packet(bc);
......
335 337
    if (enc->codec_type == CODEC_TYPE_VIDEO)
336 338
	key_frame = enc->coded_frame->key_frame;
337 339

  
338
    put_packetheader(nut, bc, size+(key_frame?16:8)+4); /* FIXME: estimation */
340
    put_packetheader(nut, bc, size+(key_frame?8:0)+20);
339 341

  
340 342
    if (key_frame)
341 343
	put_be64(bc, KEYFRAME_STARTCODE);
......
350 352
    put_byte(bc, flags);
351 353
    put_v(bc, stream_index);
352 354
    put_s(bc, 0); /* lsb_timestamp */
355
    update_packetheader(nut, bc, size);
353 356
    
354 357
    put_buffer(bc, buf, size);
355 358
    
356
    put_padding(nut, bc);
357

  
358 359
    put_flush_packet(bc);
359 360

  
360 361
    return 0;
......
371 372

  
372 373
    for (i = 0; s->nb_streams; i++)
373 374
    {
374
	put_packetheader(nut, bc, 64); /* FIXME: estimation */
375
	put_packetheader(nut, bc, 64);
375 376
	put_be64(bc, INDEX_STARTCODE);
376 377
	put_v(bc, s->streams[i]->id);
377 378
	put_v(bc, ...);
378
	put_padding(nut, bc);
379 379
	put_be32(bc, 0); /* FIXME: checksum */
380
        update_packetheader(nut, bc, 0);
380 381
    }
381 382
#endif
382 383

  
......
423 424
    s->file_size = get_v(bc);
424 425
    s->duration = get_v(bc) / (AV_TIME_BASE / 1000);
425 426

  
426
    get_padding(nut, bc);
427 427
    get_be32(bc); /* checkusm */
428 428
    
429 429
    s->bit_rate = 0;
......
485 485
	    get_v(bc); /* aspected w */
486 486
	    get_v(bc); /* aspected h */
487 487
	    get_v(bc); /* csp type */
488
	    get_padding(nut, bc);
489 488
	    get_le32(bc); /* checksum */
490 489
	}
491 490
	if (class == 32) /* AUDIO */
492 491
	{
493 492
	    st->codec.sample_rate = get_v(bc) * (double)(st->codec.frame_rate_base / st->codec.frame_rate);
494 493
	    st->codec.channels = get_v(bc);
495
	    get_padding(nut, bc);
496 494
	    get_le32(bc); /* checksum */
497 495
	}
498 496
    }    

Also available in: Unified diff