Mercurial > libavformat.hg
comparison nutenc.c @ 2771:d52c718e83f9 libavformat
Use dynamically allocated ByteIOContext in AVFormatContext
patch by: Bj«Órn Axelsson, bjorn d axelsson a intinor d se
thread: [PATCH] Remove static ByteIOContexts, 06 nov 2007
author | andoma |
---|---|
date | Wed, 21 Nov 2007 07:41:00 +0000 |
parents | 153d6efc05b8 |
children | 7e246e837176 |
comparison
equal
deleted
inserted
replaced
2770:a7e42cf4b364 | 2771:d52c718e83f9 |
---|---|
322 put_s(bc, -1); | 322 put_s(bc, -1); |
323 put_str(bc, value); | 323 put_str(bc, value); |
324 return 1; | 324 return 1; |
325 } | 325 } |
326 | 326 |
327 static void write_globalinfo(NUTContext *nut, ByteIOContext *bc){ | 327 static int write_globalinfo(NUTContext *nut, ByteIOContext *bc){ |
328 AVFormatContext *s= nut->avf; | 328 AVFormatContext *s= nut->avf; |
329 ByteIOContext dyn_bc; | 329 ByteIOContext *dyn_bc; |
330 uint8_t *dyn_buf=NULL; | 330 uint8_t *dyn_buf=NULL; |
331 int count=0, dyn_size; | 331 int count=0, dyn_size; |
332 | 332 int ret = url_open_dyn_buf(&dyn_bc); |
333 url_open_dyn_buf(&dyn_bc); | 333 if(ret < 0) |
334 | 334 return ret; |
335 if(s->title [0]) count+= add_info(&dyn_bc, "Title" , s->title); | 335 |
336 if(s->author [0]) count+= add_info(&dyn_bc, "Author" , s->author); | 336 if(s->title [0]) count+= add_info(dyn_bc, "Title" , s->title); |
337 if(s->copyright[0]) count+= add_info(&dyn_bc, "Copyright", s->copyright); | 337 if(s->author [0]) count+= add_info(dyn_bc, "Author" , s->author); |
338 if(s->copyright[0]) count+= add_info(dyn_bc, "Copyright", s->copyright); | |
338 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) | 339 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) |
339 count+= add_info(&dyn_bc, "Encoder" , LIBAVFORMAT_IDENT); | 340 count+= add_info(dyn_bc, "Encoder" , LIBAVFORMAT_IDENT); |
340 | 341 |
341 put_v(bc, 0); //stream_if_plus1 | 342 put_v(bc, 0); //stream_if_plus1 |
342 put_v(bc, 0); //chapter_id | 343 put_v(bc, 0); //chapter_id |
343 put_v(bc, 0); //timestamp_start | 344 put_v(bc, 0); //timestamp_start |
344 put_v(bc, 0); //length | 345 put_v(bc, 0); //length |
345 | 346 |
346 put_v(bc, count); | 347 put_v(bc, count); |
347 | 348 |
348 dyn_size= url_close_dyn_buf(&dyn_bc, &dyn_buf); | 349 dyn_size= url_close_dyn_buf(dyn_bc, &dyn_buf); |
349 put_buffer(bc, dyn_buf, dyn_size); | 350 put_buffer(bc, dyn_buf, dyn_size); |
350 av_free(dyn_buf); | 351 av_free(dyn_buf); |
351 } | 352 return 0; |
352 | 353 } |
353 static void write_headers(NUTContext *nut, ByteIOContext *bc){ | 354 |
354 ByteIOContext dyn_bc; | 355 static int write_headers(NUTContext *nut, ByteIOContext *bc){ |
355 int i; | 356 ByteIOContext *dyn_bc; |
356 | 357 int i, ret; |
357 url_open_dyn_buf(&dyn_bc); | 358 |
358 write_mainheader(nut, &dyn_bc); | 359 ret = url_open_dyn_buf(&dyn_bc); |
359 put_packet(nut, bc, &dyn_bc, 1, MAIN_STARTCODE); | 360 if(ret < 0) |
361 return ret; | |
362 write_mainheader(nut, dyn_bc); | |
363 put_packet(nut, bc, dyn_bc, 1, MAIN_STARTCODE); | |
360 | 364 |
361 for (i=0; i < nut->avf->nb_streams; i++){ | 365 for (i=0; i < nut->avf->nb_streams; i++){ |
362 AVCodecContext *codec = nut->avf->streams[i]->codec; | 366 AVCodecContext *codec = nut->avf->streams[i]->codec; |
363 | 367 |
364 url_open_dyn_buf(&dyn_bc); | 368 ret = url_open_dyn_buf(&dyn_bc); |
365 write_streamheader(nut, &dyn_bc, codec, i); | 369 if(ret < 0) |
366 put_packet(nut, bc, &dyn_bc, 1, STREAM_STARTCODE); | 370 return ret; |
367 } | 371 write_streamheader(nut, dyn_bc, codec, i); |
368 | 372 put_packet(nut, bc, dyn_bc, 1, STREAM_STARTCODE); |
369 url_open_dyn_buf(&dyn_bc); | 373 } |
370 write_globalinfo(nut, &dyn_bc); | 374 |
371 put_packet(nut, bc, &dyn_bc, 1, INFO_STARTCODE); | 375 ret = url_open_dyn_buf(&dyn_bc); |
376 if(ret < 0) | |
377 return ret; | |
378 write_globalinfo(nut, dyn_bc); | |
379 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE); | |
372 | 380 |
373 nut->last_syncpoint_pos= INT_MIN; | 381 nut->last_syncpoint_pos= INT_MIN; |
374 nut->header_count++; | 382 nut->header_count++; |
383 return 0; | |
375 } | 384 } |
376 | 385 |
377 static int write_header(AVFormatContext *s){ | 386 static int write_header(AVFormatContext *s){ |
378 NUTContext *nut = s->priv_data; | 387 NUTContext *nut = s->priv_data; |
379 ByteIOContext *bc = &s->pb; | 388 ByteIOContext *bc = s->pb; |
380 int i, j; | 389 int i, j; |
381 | 390 |
382 nut->avf= s; | 391 nut->avf= s; |
383 | 392 |
384 nut->stream = av_mallocz(sizeof(StreamContext)*s->nb_streams); | 393 nut->stream = av_mallocz(sizeof(StreamContext)*s->nb_streams); |
439 } | 448 } |
440 | 449 |
441 static int write_packet(AVFormatContext *s, AVPacket *pkt){ | 450 static int write_packet(AVFormatContext *s, AVPacket *pkt){ |
442 NUTContext *nut = s->priv_data; | 451 NUTContext *nut = s->priv_data; |
443 StreamContext *nus= &nut->stream[pkt->stream_index]; | 452 StreamContext *nus= &nut->stream[pkt->stream_index]; |
444 ByteIOContext *bc = &s->pb, dyn_bc; | 453 ByteIOContext *bc = s->pb, *dyn_bc; |
445 FrameCode *fc; | 454 FrameCode *fc; |
446 int64_t coded_pts; | 455 int64_t coded_pts; |
447 int best_length, frame_code, flags, needed_flags, i; | 456 int best_length, frame_code, flags, needed_flags, i; |
448 int key_frame = !!(pkt->flags & PKT_FLAG_KEY); | 457 int key_frame = !!(pkt->flags & PKT_FLAG_KEY); |
449 int store_sp=0; | 458 int store_sp=0; |
459 int ret; | |
450 | 460 |
451 if(1LL<<(20+3*nut->header_count) <= url_ftell(bc)) | 461 if(1LL<<(20+3*nut->header_count) <= url_ftell(bc)) |
452 write_headers(nut, bc); | 462 write_headers(nut, bc); |
453 | 463 |
454 if(key_frame && !!(nus->last_flags & FLAG_KEY)) | 464 if(key_frame && !!(nus->last_flags & FLAG_KEY)) |
470 else dummy.pos= FFMIN(dummy.pos, st->index_entries[index].pos); | 480 else dummy.pos= FFMIN(dummy.pos, st->index_entries[index].pos); |
471 } | 481 } |
472 sp= av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, NULL); | 482 sp= av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, NULL); |
473 | 483 |
474 nut->last_syncpoint_pos= url_ftell(bc); | 484 nut->last_syncpoint_pos= url_ftell(bc); |
475 url_open_dyn_buf(&dyn_bc); | 485 ret = url_open_dyn_buf(&dyn_bc); |
476 put_t(nut, nus, &dyn_bc, pkt->dts); | 486 if(ret < 0) |
477 put_v(&dyn_bc, sp ? (nut->last_syncpoint_pos - sp->pos)>>4 : 0); | 487 return ret; |
478 put_packet(nut, bc, &dyn_bc, 1, SYNCPOINT_STARTCODE); | 488 put_t(nut, nus, dyn_bc, pkt->dts); |
489 put_v(dyn_bc, sp ? (nut->last_syncpoint_pos - sp->pos)>>4 : 0); | |
490 put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE); | |
479 | 491 |
480 ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0/*unused*/, pkt->dts); | 492 ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0/*unused*/, pkt->dts); |
481 } | 493 } |
482 assert(nus->last_pts != AV_NOPTS_VALUE); | 494 assert(nus->last_pts != AV_NOPTS_VALUE); |
483 | 495 |
564 return 0; | 576 return 0; |
565 } | 577 } |
566 | 578 |
567 static int write_trailer(AVFormatContext *s){ | 579 static int write_trailer(AVFormatContext *s){ |
568 NUTContext *nut= s->priv_data; | 580 NUTContext *nut= s->priv_data; |
569 ByteIOContext *bc= &s->pb; | 581 ByteIOContext *bc= s->pb; |
570 | 582 |
571 while(nut->header_count<3) | 583 while(nut->header_count<3) |
572 write_headers(nut, bc); | 584 write_headers(nut, bc); |
573 put_flush_packet(bc); | 585 put_flush_packet(bc); |
574 | 586 |