~ubuntu-branches/ubuntu/trusty/libav/trusty

« back to all changes in this revision

Viewing changes to libavformat/matroskaenc.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2013-10-22 23:24:08 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: package-import@ubuntu.com-20131022232408-b8tvvn4pyzri9mi3
Tags: 6:9.10-1ubuntu1
* Build all -extra flavors from this source package, as libav got demoted
  from main to universe, cf LP: #1243235
* Simplify debian/rules to follow exactly the code that debian executes
* New upstream (LP: #1180288) fixes lots of security issues (LP: #1242802)
* Merge from unstable, remaining changes:
  - build-depend on libtiff5-dev rather than libtiff4-dev,
    avoids FTBFS caused by imlib
  - follow the regular debian codepaths

Show diffs side-by-side

added added

removed removed

Lines of Context:
368
368
{
369
369
    mkv_cuepoint *entries = cues->entries;
370
370
 
 
371
    if (ts < 0)
 
372
        return 0;
 
373
 
371
374
    entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));
372
375
    if (entries == NULL)
373
376
        return AVERROR(ENOMEM);
374
377
 
375
 
    if (ts < 0)
376
 
        return 0;
377
 
 
378
378
    entries[cues->num_entries  ].pts = ts;
379
379
    entries[cues->num_entries  ].tracknum = stream + 1;
380
380
    entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset;
423
423
    int first_header_size;
424
424
    int j;
425
425
 
426
 
    if (codec->codec_id == CODEC_ID_VORBIS)
 
426
    if (codec->codec_id == AV_CODEC_ID_VORBIS)
427
427
        first_header_size = 30;
428
428
    else
429
429
        first_header_size = 42;
469
469
        return ret;
470
470
 
471
471
    if (native_id) {
472
 
        if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA)
 
472
        if (codec->codec_id == AV_CODEC_ID_VORBIS || codec->codec_id == AV_CODEC_ID_THEORA)
473
473
            ret = put_xiph_codecpriv(s, dyn_cp, codec);
474
 
        else if (codec->codec_id == CODEC_ID_FLAC)
 
474
        else if (codec->codec_id == AV_CODEC_ID_FLAC)
475
475
            ret = ff_flac_write_header(dyn_cp, codec, 1);
476
 
        else if (codec->codec_id == CODEC_ID_H264)
 
476
        else if (codec->codec_id == AV_CODEC_ID_H264)
477
477
            ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
 
478
        else if (codec->codec_id == AV_CODEC_ID_ALAC) {
 
479
            if (codec->extradata_size < 36) {
 
480
                av_log(s, AV_LOG_ERROR,
 
481
                       "Invalid extradata found, ALAC expects a 36-byte "
 
482
                       "QuickTime atom.");
 
483
                ret = AVERROR_INVALIDDATA;
 
484
            } else
 
485
                avio_write(dyn_cp, codec->extradata + 12,
 
486
                                   codec->extradata_size - 12);
 
487
        }
478
488
        else if (codec->extradata_size)
479
489
            avio_write(dyn_cp, codec->extradata, codec->extradata_size);
480
490
    } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
481
491
        if (qt_id) {
482
492
            if (!codec->codec_tag)
483
 
                codec->codec_tag = ff_codec_get_tag(codec_movvideo_tags, codec->codec_id);
 
493
                codec->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id);
484
494
            if (codec->extradata_size)
485
495
                avio_write(dyn_cp, codec->extradata, codec->extradata_size);
486
496
        } else {
544
554
        if (!bit_depth)
545
555
            bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
546
556
 
547
 
        if (codec->codec_id == CODEC_ID_AAC)
 
557
        if (codec->codec_id == AV_CODEC_ID_AAC)
548
558
            get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
549
559
 
550
560
        track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
562
572
 
563
573
        // look for a codec ID string specific to mkv to use,
564
574
        // if none are found, use AVI codes
565
 
        for (j = 0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++) {
 
575
        for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
566
576
            if (ff_mkv_codec_tags[j].id == codec->codec_id) {
567
577
                put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
568
578
                native_id = 1;
570
580
            }
571
581
        }
572
582
 
573
 
        if (mkv->mode == MODE_WEBM && !(codec->codec_id == CODEC_ID_VP8 ||
574
 
                                        codec->codec_id == CODEC_ID_VORBIS)) {
 
583
        if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
 
584
                                        codec->codec_id == AV_CODEC_ID_VORBIS)) {
575
585
            av_log(s, AV_LOG_ERROR,
576
586
                   "Only VP8 video and Vorbis audio are supported for WebM.\n");
577
587
            return AVERROR(EINVAL);
583
593
                put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
584
594
 
585
595
                if (!native_id &&
586
 
                      ff_codec_get_tag(codec_movvideo_tags, codec->codec_id) &&
 
596
                      ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) &&
587
597
                    (!ff_codec_get_tag(ff_codec_bmp_tags,   codec->codec_id)
588
 
                     || codec->codec_id == CODEC_ID_SVQ1
589
 
                     || codec->codec_id == CODEC_ID_SVQ3
590
 
                     || codec->codec_id == CODEC_ID_CINEPAK))
 
598
                     || codec->codec_id == AV_CODEC_ID_SVQ1
 
599
                     || codec->codec_id == AV_CODEC_ID_SVQ3
 
600
                     || codec->codec_id == AV_CODEC_ID_CINEPAK))
591
601
                    qt_id = 1;
592
602
 
593
603
                if (qt_id)
848
858
        put_ebml_string(pb, MATROSKA_ID_FILENAME, t->value);
849
859
        if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
850
860
            mimetype = t->value;
851
 
        else if (st->codec->codec_id != CODEC_ID_NONE ) {
 
861
        else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
852
862
            int i;
853
 
            for (i = 0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++)
 
863
            for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
854
864
                if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
855
865
                    mimetype = ff_mkv_mime_tags[i].str;
856
866
                    break;
1040
1050
    MatroskaMuxContext *mkv = s->priv_data;
1041
1051
    AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1042
1052
    uint8_t *data = NULL;
1043
 
    int size = pkt->size;
 
1053
    int offset = 0, size = pkt->size;
1044
1054
    int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1045
1055
 
1046
1056
    av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1047
1057
           "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1048
1058
           avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1049
 
    if (codec->codec_id == CODEC_ID_H264 && codec->extradata_size > 0 &&
 
1059
    if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1050
1060
        (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1051
1061
        ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1052
1062
    else
1053
1063
        data = pkt->data;
 
1064
 
 
1065
    if (codec->codec_id == AV_CODEC_ID_PRORES) {
 
1066
        /* Matroska specification requires to remove the first QuickTime atom
 
1067
         */
 
1068
        size -= 8;
 
1069
        offset = 8;
 
1070
    }
 
1071
 
1054
1072
    put_ebml_id(pb, blockid);
1055
1073
    put_ebml_num(pb, size+4, 0);
1056
1074
    avio_w8(pb, 0x80 | (pkt->stream_index + 1));     // this assumes stream_index is less than 126
1057
1075
    avio_wb16(pb, ts - mkv->cluster_pts);
1058
1076
    avio_w8(pb, flags);
1059
 
    avio_write(pb, data, size);
 
1077
    avio_write(pb, data + offset, size);
1060
1078
    if (data != pkt->data)
1061
1079
        av_free(data);
1062
1080
}
1141
1159
 
1142
1160
    if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1143
1161
        mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1144
 
    } else if (codec->codec_id == CODEC_ID_SSA) {
 
1162
    } else if (codec->codec_id == AV_CODEC_ID_SSA) {
1145
1163
        duration = mkv_write_ass_blocks(s, pb, pkt);
1146
 
    } else if (codec->codec_id == CODEC_ID_SRT) {
 
1164
    } else if (codec->codec_id == AV_CODEC_ID_SRT) {
1147
1165
        duration = mkv_write_srt_blocks(s, pb, pkt);
1148
1166
    } else {
1149
1167
        ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size));
1265
1283
    av_freep(&mkv->cues->entries);
1266
1284
    av_freep(&mkv->cues);
1267
1285
    av_destruct_packet(&mkv->cur_audio_pkt);
1268
 
    avio_flush(pb);
 
1286
 
1269
1287
    return 0;
1270
1288
}
1271
1289
 
1272
 
static int mkv_query_codec(enum CodecID codec_id, int std_compliance)
 
1290
static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
1273
1291
{
1274
1292
    int i;
1275
 
    for (i = 0; ff_mkv_codec_tags[i].id != CODEC_ID_NONE; i++)
 
1293
    for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
1276
1294
        if (ff_mkv_codec_tags[i].id == codec_id)
1277
1295
            return 1;
1278
1296
 
1288
1306
#if CONFIG_MATROSKA_MUXER
1289
1307
AVOutputFormat ff_matroska_muxer = {
1290
1308
    .name              = "matroska",
1291
 
    .long_name         = NULL_IF_CONFIG_SMALL("Matroska file format"),
 
1309
    .long_name         = NULL_IF_CONFIG_SMALL("Matroska"),
1292
1310
    .mime_type         = "video/x-matroska",
1293
1311
    .extensions        = "mkv",
1294
1312
    .priv_data_size    = sizeof(MatroskaMuxContext),
1295
 
#if CONFIG_LIBVORBIS_ENCODER
1296
 
    .audio_codec       = CODEC_ID_VORBIS,
1297
 
#else
1298
 
    .audio_codec       = CODEC_ID_AC3,
1299
 
#endif
1300
 
#if CONFIG_LIBX264_ENCODER
1301
 
    .video_codec       = CODEC_ID_H264,
1302
 
#else
1303
 
    .video_codec       = CODEC_ID_MPEG4,
1304
 
#endif
 
1313
    .audio_codec       = CONFIG_LIBVORBIS_ENCODER ?
 
1314
                         AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
 
1315
    .video_codec       = CONFIG_LIBX264_ENCODER ?
 
1316
                         AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
1305
1317
    .write_header      = mkv_write_header,
1306
1318
    .write_packet      = mkv_write_packet,
1307
1319
    .write_trailer     = mkv_write_trailer,
1308
 
    .flags             = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
1309
 
    .codec_tag         = (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
1310
 
    .subtitle_codec    = CODEC_ID_SSA,
 
1320
    .flags             = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
 
1321
                         AVFMT_TS_NONSTRICT,
 
1322
    .codec_tag         = (const AVCodecTag* const []){
 
1323
         ff_codec_bmp_tags, ff_codec_wav_tags, 0
 
1324
    },
 
1325
    .subtitle_codec    = AV_CODEC_ID_SSA,
1311
1326
    .query_codec       = mkv_query_codec,
1312
1327
};
1313
1328
#endif
1315
1330
#if CONFIG_WEBM_MUXER
1316
1331
AVOutputFormat ff_webm_muxer = {
1317
1332
    .name              = "webm",
1318
 
    .long_name         = NULL_IF_CONFIG_SMALL("WebM file format"),
 
1333
    .long_name         = NULL_IF_CONFIG_SMALL("WebM"),
1319
1334
    .mime_type         = "video/webm",
1320
1335
    .extensions        = "webm",
1321
1336
    .priv_data_size    = sizeof(MatroskaMuxContext),
1322
 
    .audio_codec       = CODEC_ID_VORBIS,
1323
 
    .video_codec       = CODEC_ID_VP8,
 
1337
    .audio_codec       = AV_CODEC_ID_VORBIS,
 
1338
    .video_codec       = AV_CODEC_ID_VP8,
1324
1339
    .write_header      = mkv_write_header,
1325
1340
    .write_packet      = mkv_write_packet,
1326
1341
    .write_trailer     = mkv_write_trailer,
1327
 
    .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
 
1342
    .flags             = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
 
1343
                         AVFMT_TS_NONSTRICT,
1328
1344
};
1329
1345
#endif
1330
1346
 
1331
1347
#if CONFIG_MATROSKA_AUDIO_MUXER
1332
1348
AVOutputFormat ff_matroska_audio_muxer = {
1333
1349
    .name              = "matroska",
1334
 
    .long_name         = NULL_IF_CONFIG_SMALL("Matroska file format"),
 
1350
    .long_name         = NULL_IF_CONFIG_SMALL("Matroska"),
1335
1351
    .mime_type         = "audio/x-matroska",
1336
1352
    .extensions        = "mka",
1337
1353
    .priv_data_size    = sizeof(MatroskaMuxContext),
1338
 
#if CONFIG_LIBVORBIS_ENCODER
1339
 
    .audio_codec       = CODEC_ID_VORBIS,
1340
 
#else
1341
 
    .audio_codec       = CODEC_ID_AC3,
1342
 
#endif
1343
 
    .video_codec       = CODEC_ID_NONE,
 
1354
    .audio_codec       = CONFIG_LIBVORBIS_ENCODER ?
 
1355
                         AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
 
1356
    .video_codec       = AV_CODEC_ID_NONE,
1344
1357
    .write_header      = mkv_write_header,
1345
1358
    .write_packet      = mkv_write_packet,
1346
1359
    .write_trailer     = mkv_write_trailer,
1347
 
    .flags = AVFMT_GLOBALHEADER,
1348
 
    .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
 
1360
    .flags             = AVFMT_GLOBALHEADER | AVFMT_TS_NONSTRICT,
 
1361
    .codec_tag         = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
1349
1362
};
1350
1363
#endif