~ubuntu-branches/ubuntu/feisty/avidemux/feisty

« back to all changes in this revision

Viewing changes to avidemux/ADM_lavformat/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2006-12-15 17:13:20 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20061215171320-w79pvpehxx2fr217
Tags: 1:2.3.0-0.0ubuntu1
* Merge from debian-multimedia.org, remaining Ubuntu change:
  - desktop file,
  - no support for ccache and make -j.
* Closes Ubuntu: #69614.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 *
15
15
 * You should have received a copy of the GNU Lesser General Public
16
16
 * License along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
 */
19
19
#include "avformat.h"
20
20
 
465
465
{
466
466
    int err;
467
467
    AVFormatContext *ic;
 
468
    AVFormatParameters default_ap;
 
469
 
 
470
    if(!ap){
 
471
        ap=&default_ap;
 
472
        memset(ap, 0, sizeof(default_ap));
 
473
    }
468
474
 
469
475
    ic = av_alloc_format_context();
470
476
    if (!ic) {
508
514
}
509
515
 
510
516
/** Size of probe buffer, for guessing file type from file contents. */
511
 
#define PROBE_BUF_SIZE 2048
 
517
#define PROBE_BUF_MIN 2048
 
518
#define PROBE_BUF_MAX 131072
512
519
 
513
520
/**
514
521
 * Open a media file as input. The codec are not opened. Only the file
526
533
                       int buf_size,
527
534
                       AVFormatParameters *ap)
528
535
{
529
 
    int err, must_open_file, file_opened;
530
 
    uint8_t buf[PROBE_BUF_SIZE];
 
536
    int err, must_open_file, file_opened, probe_size;
531
537
    AVProbeData probe_data, *pd = &probe_data;
532
538
    ByteIOContext pb1, *pb = &pb1;
533
539
 
535
541
    pd->filename = "";
536
542
    if (filename)
537
543
        pd->filename = filename;
538
 
    pd->buf = buf;
 
544
    pd->buf = NULL;
539
545
    pd->buf_size = 0;
540
546
 
541
547
    if (!fmt) {
561
567
        if (buf_size > 0) {
562
568
            url_setbufsize(pb, buf_size);
563
569
        }
564
 
        if (!fmt) {
 
570
 
 
571
        for(probe_size= PROBE_BUF_MIN; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){
565
572
            /* read probe data */
566
 
            pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE);
 
573
            pd->buf= av_realloc(pd->buf, probe_size);
 
574
            pd->buf_size = get_buffer(pb, pd->buf, probe_size);
567
575
            if (url_fseek(pb, 0, SEEK_SET) == (offset_t)-EPIPE) {
568
576
                url_fclose(pb);
569
577
                if (url_fopen(pb, filename, URL_RDONLY) < 0) {
 
578
                    file_opened = 0;
570
579
                    err = AVERROR_IO;
571
580
                    goto fail;
572
581
                }
573
582
            }
 
583
            /* guess file format */
 
584
            fmt = av_probe_input_format(pd, 1);
574
585
        }
575
 
    }
576
 
 
577
 
    /* guess file format */
578
 
    if (!fmt) {
579
 
        fmt = av_probe_input_format(pd, 1);
 
586
        av_freep(&pd->buf);
580
587
    }
581
588
 
582
589
    /* if still no format found, error */
606
613
        goto fail;
607
614
    return 0;
608
615
 fail:
 
616
    av_freep(&pd->buf);
609
617
    if (file_opened)
610
618
        url_fclose(pb);
611
619
    *ic_ptr = NULL;
643
651
        /* specific hack for pcm codecs because no frame size is
644
652
           provided */
645
653
        switch(enc->codec_id) {
646
 
#if 0 //MEANX
 
654
#if 0 //MEANX           
647
655
        case CODEC_ID_PCM_S32LE:
648
656
        case CODEC_ID_PCM_S32BE:
649
657
        case CODEC_ID_PCM_U32LE:
661
669
                return -1;
662
670
            frame_size = size / (3 * enc->channels);
663
671
            break;
664
 
#endif
 
672
#endif //MEANX      
665
673
        case CODEC_ID_PCM_S16LE:
666
674
        case CODEC_ID_PCM_S16BE:
667
675
        case CODEC_ID_PCM_U16LE:
1131
1139
 * @param timestamp timestamp in the timebase of the given stream
1132
1140
 */
1133
1141
int av_add_index_entry(AVStream *st,
1134
 
                            int64_t pos, int64_t timestamp, int distance, int flags)
 
1142
                            int64_t pos, int64_t timestamp, int size, int distance, int flags)
1135
1143
{
1136
1144
    AVIndexEntry *entries, *ie;
1137
1145
    int index;
1168
1176
    ie->pos = pos;
1169
1177
    ie->timestamp = timestamp;
1170
1178
    ie->min_distance= distance;
 
1179
    ie->size= size;
1171
1180
    ie->flags = flags;
1172
1181
 
1173
1182
    return index;
1193
1202
        if (pkt->stream_index == 0 && st->parser &&
1194
1203
            (pkt->flags & PKT_FLAG_KEY)) {
1195
1204
            av_add_index_entry(st, st->parser->frame_offset, pkt->dts,
1196
 
                            0, AVINDEX_KEYFRAME);
 
1205
                            0, 0, AVINDEX_KEYFRAME);
1197
1206
        }
1198
1207
        av_free_packet(pkt);
1199
1208
    }
1349
1358
        pos_limit= pos_max;
1350
1359
    }
1351
1360
 
 
1361
    if(ts_min > ts_max){
 
1362
        return -1;
 
1363
    }else if(ts_min == ts_max){
 
1364
        pos_limit= pos_min;
 
1365
    }
 
1366
 
1352
1367
    no_change=0;
1353
1368
    while (pos_min < pos_limit) {
1354
1369
#ifdef DEBUG_SEEK
1848
1863
 */
1849
1864
int av_find_stream_info(AVFormatContext *ic)
1850
1865
{
1851
 
    int i, count, ret, read_size;
 
1866
    int i, count, ret, read_size, j;
1852
1867
    AVStream *st;
1853
1868
    AVPacket pkt1, *pkt;
1854
1869
    AVPacketList *pktl=NULL, **ppktl;
1888
1903
            if (!has_codec_parameters(st->codec))
1889
1904
                break;
1890
1905
            /* variable fps and no guess at the real fps */
1891
 
            if(   st->codec->time_base.den >= 1000LL*st->codec->time_base.num
 
1906
            if(   st->codec->time_base.den >= 101LL*st->codec->time_base.num
1892
1907
               && duration_count[i]<20 && st->codec->codec_type == CODEC_TYPE_VIDEO)
1893
1908
                break;
1894
1909
            if(st->parser && st->parser->parser->split && !st->codec->extradata)
1919
1934
            ret = -1; /* we could not have all the codec parameters before EOF */
1920
1935
            for(i=0;i<ic->nb_streams;i++) {
1921
1936
                st = ic->streams[i];
1922
 
                if (!has_codec_parameters(st->codec))
 
1937
                if (!has_codec_parameters(st->codec)){
 
1938
                    char buf[256];
 
1939
                    avcodec_string(buf, sizeof(buf), st->codec, 0);
 
1940
                    av_log(ic, AV_LOG_INFO, "Could not find codec parameters (%s)\n", buf);
1923
1941
                    break;
 
1942
                }
1924
1943
            }
1925
1944
            if (i == ic->nb_streams)
1926
1945
                ret = 0;
2018
2037
        st = ic->streams[i];
2019
2038
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
2020
2039
            if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_sample)
2021
 
                // MEANX st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
 
2040
                st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
2022
2041
 
2023
 
            if(duration_count[i] && st->codec->time_base.num*1000LL <= st->codec->time_base.den &&
2024
 
               st->time_base.num*duration_sum[i]/duration_count[i]*1000LL > st->time_base.den){
2025
 
                AVRational fps1;
2026
 
                int64_t num, den;
 
2042
            if(duration_count[i] && st->codec->time_base.num*101LL <= st->codec->time_base.den &&
 
2043
               st->time_base.num*duration_sum[i]/duration_count[i]*101LL > st->time_base.den){
 
2044
                int64_t num, den, error, best_error;
2027
2045
 
2028
2046
                num= st->time_base.den*duration_count[i];
2029
2047
                den= st->time_base.num*duration_sum[i];
2030
2048
 
2031
 
                av_reduce(&fps1.num, &fps1.den, num*1001, den*1000, FFMAX(st->time_base.den, st->time_base.num)/4);
2032
 
                av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, den, FFMAX(st->time_base.den, st->time_base.num)/4);
2033
 
                if(fps1.num < st->r_frame_rate.num && fps1.den == 1 && (fps1.num==24 || fps1.num==30)){ //FIXME better decission
2034
 
                    st->r_frame_rate.num= fps1.num*1000;
2035
 
                    st->r_frame_rate.den= fps1.den*1001;
 
2049
                best_error= INT64_MAX;
 
2050
                for(j=1; j<60*12; j++){
 
2051
                    error= ABS(1001*12*num - 1001*j*den);
 
2052
                    if(error < best_error){
 
2053
                        best_error= error;
 
2054
                        av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, j, 12, INT_MAX);
 
2055
                    }
 
2056
                }
 
2057
                for(j=24; j<=30; j+=6){
 
2058
                    error= ABS(1001*12*num - 1000*12*j*den);
 
2059
                    if(error < best_error){
 
2060
                        best_error= error;
 
2061
                        av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, j*1000, 1001, INT_MAX);
 
2062
                    }
2036
2063
                }
2037
2064
            }
2038
2065
 
2718
2745
    }
2719
2746
    else {
2720
2747
        /* Finally we give up and parse it as double */
2721
 
        *frame_rate_base = DEFAULT_FRAME_RATE_BASE; //FIXME use av_d2q()
2722
 
        *frame_rate = (int)(strtod(arg, 0) * (*frame_rate_base) + 0.5);
 
2748
        AVRational time_base = av_d2q(strtod(arg, 0), DEFAULT_FRAME_RATE_BASE);
 
2749
        *frame_rate_base = time_base.den;
 
2750
        *frame_rate = time_base.num;
2723
2751
    }
2724
2752
    if (!*frame_rate || !*frame_rate_base)
2725
2753
        return -1;
2742
2770
 *  S+[.m...]
2743
2771
 * @endcode
2744
2772
 */
 
2773
#ifndef CONFIG_WINCE
2745
2774
int64_t parse_date(const char *datestr, int duration)
2746
2775
{
2747
2776
    const char *p;
2849
2878
    }
2850
2879
    return negative ? -t : t;
2851
2880
}
 
2881
#endif /* CONFIG_WINCE */
2852
2882
 
2853
2883
/**
2854
2884
 * Attempts to find a specific tag in a URL.
3151
3181
 
3152
3182
    num = f->num + incr;
3153
3183
    den = f->den;
 
3184
    ///MEANX
 
3185
    if(!den)
 
3186
    {
 
3187
                return;
 
3188
    }
 
3189
    ///MEANX
3154
3190
    if (num < 0) {
3155
3191
        f->val += num / den;
3156
3192
        num = num % den;
3224
3260
                  AVImageFormat *fmt,
3225
3261
                  int (*alloc_cb)(void *, AVImageInfo *info), void *opaque)
3226
3262
{
3227
 
    char buf[PROBE_BUF_SIZE];
 
3263
    uint8_t buf[PROBE_BUF_MIN];
3228
3264
    AVProbeData probe_data, *pd = &probe_data;
3229
3265
    offset_t pos;
3230
3266
    int ret;
3233
3269
        pd->filename = filename;
3234
3270
        pd->buf = buf;
3235
3271
        pos = url_ftell(pb);
3236
 
        pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE);
 
3272
        pd->buf_size = get_buffer(pb, buf, PROBE_BUF_MIN);
3237
3273
        url_fseek(pb, pos, SEEK_SET);
3238
3274
        fmt = av_probe_image_format(pd);
3239
3275
    }