| 1 | /*
|
|---|
| 2 | * ASF compatible demuxer
|
|---|
| 3 | * Copyright (c) 2000, 2001 Fabrice Bellard
|
|---|
| 4 | *
|
|---|
| 5 | * This file is part of FFmpeg.
|
|---|
| 6 | *
|
|---|
| 7 | * FFmpeg is free software; you can redistribute it and/or
|
|---|
| 8 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 9 | * License as published by the Free Software Foundation; either
|
|---|
| 10 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 11 | *
|
|---|
| 12 | * FFmpeg is distributed in the hope that it will be useful,
|
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 15 | * Lesser General Public License for more details.
|
|---|
| 16 | *
|
|---|
| 17 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 18 | * License along with FFmpeg; if not, write to the Free Software
|
|---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | //#define DEBUG
|
|---|
| 23 |
|
|---|
| 24 | #include "libavutil/bswap.h"
|
|---|
| 25 | #include "libavutil/common.h"
|
|---|
| 26 | #include "libavutil/avstring.h"
|
|---|
| 27 | #include "libavutil/dict.h"
|
|---|
| 28 | #include "libavutil/mathematics.h"
|
|---|
| 29 | #include "libavcodec/mpegaudio.h"
|
|---|
| 30 | #include "avformat.h"
|
|---|
| 31 | #include "avio_internal.h"
|
|---|
| 32 | #include "riff.h"
|
|---|
| 33 | #include "asf.h"
|
|---|
| 34 | #include "asfcrypt.h"
|
|---|
| 35 | #include "avlanguage.h"
|
|---|
| 36 |
|
|---|
| 37 | typedef struct {
|
|---|
| 38 | int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
|
|---|
| 39 | ASFStream streams[128]; ///< it's max number and it's not that big
|
|---|
| 40 | uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
|
|---|
| 41 | AVRational dar[128];
|
|---|
| 42 | char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
|
|---|
| 43 | /* non streamed additonnal info */
|
|---|
| 44 | /* packet filling */
|
|---|
| 45 | int packet_size_left;
|
|---|
| 46 | /* only for reading */
|
|---|
| 47 | uint64_t data_offset; ///< beginning of the first data packet
|
|---|
| 48 | uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
|
|---|
| 49 | uint64_t data_object_size; ///< size of the data object
|
|---|
| 50 | int index_read;
|
|---|
| 51 |
|
|---|
| 52 | ASFMainHeader hdr;
|
|---|
| 53 |
|
|---|
| 54 | int packet_flags;
|
|---|
| 55 | int packet_property;
|
|---|
| 56 | int packet_timestamp;
|
|---|
| 57 | int packet_segsizetype;
|
|---|
| 58 | int packet_segments;
|
|---|
| 59 | int packet_seq;
|
|---|
| 60 | int packet_replic_size;
|
|---|
| 61 | int packet_key_frame;
|
|---|
| 62 | int packet_padsize;
|
|---|
| 63 | unsigned int packet_frag_offset;
|
|---|
| 64 | unsigned int packet_frag_size;
|
|---|
| 65 | int64_t packet_frag_timestamp;
|
|---|
| 66 | int packet_multi_size;
|
|---|
| 67 | int packet_obj_size;
|
|---|
| 68 | int packet_time_delta;
|
|---|
| 69 | int packet_time_start;
|
|---|
| 70 | int64_t packet_pos;
|
|---|
| 71 |
|
|---|
| 72 | int stream_index;
|
|---|
| 73 |
|
|---|
| 74 | ASFStream* asf_st; ///< currently decoded stream
|
|---|
| 75 | } ASFContext;
|
|---|
| 76 |
|
|---|
| 77 | #undef NDEBUG
|
|---|
| 78 | #include <assert.h>
|
|---|
| 79 |
|
|---|
| 80 | #define ASF_MAX_STREAMS 127
|
|---|
| 81 | #define FRAME_HEADER_SIZE 17
|
|---|
| 82 | // Fix Me! FRAME_HEADER_SIZE may be different.
|
|---|
| 83 |
|
|---|
| 84 | static const ff_asf_guid index_guid = {
|
|---|
| 85 | 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
|
|---|
| 86 | };
|
|---|
| 87 |
|
|---|
| 88 | #ifdef DEBUG
|
|---|
| 89 | static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
|
|---|
| 90 | 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | #define PRINT_IF_GUID(g,cmp) \
|
|---|
| 94 | if (!ff_guidcmp(g, &cmp)) \
|
|---|
| 95 | av_dlog(NULL, "(GUID: %s) ", #cmp)
|
|---|
| 96 |
|
|---|
| 97 | static void print_guid(const ff_asf_guid *g)
|
|---|
| 98 | {
|
|---|
| 99 | int i;
|
|---|
| 100 | PRINT_IF_GUID(g, ff_asf_header);
|
|---|
| 101 | else PRINT_IF_GUID(g, ff_asf_file_header);
|
|---|
| 102 | else PRINT_IF_GUID(g, ff_asf_stream_header);
|
|---|
| 103 | else PRINT_IF_GUID(g, ff_asf_audio_stream);
|
|---|
| 104 | else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
|
|---|
| 105 | else PRINT_IF_GUID(g, ff_asf_video_stream);
|
|---|
| 106 | else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
|
|---|
| 107 | else PRINT_IF_GUID(g, ff_asf_command_stream);
|
|---|
| 108 | else PRINT_IF_GUID(g, ff_asf_comment_header);
|
|---|
| 109 | else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
|
|---|
| 110 | else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
|
|---|
| 111 | else PRINT_IF_GUID(g, ff_asf_data_header);
|
|---|
| 112 | else PRINT_IF_GUID(g, index_guid);
|
|---|
| 113 | else PRINT_IF_GUID(g, ff_asf_head1_guid);
|
|---|
| 114 | else PRINT_IF_GUID(g, ff_asf_head2_guid);
|
|---|
| 115 | else PRINT_IF_GUID(g, ff_asf_my_guid);
|
|---|
| 116 | else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
|
|---|
| 117 | else PRINT_IF_GUID(g, ff_asf_extended_content_header);
|
|---|
| 118 | else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
|
|---|
| 119 | else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
|
|---|
| 120 | else PRINT_IF_GUID(g, ff_asf_metadata_header);
|
|---|
| 121 | else PRINT_IF_GUID(g, ff_asf_marker_header);
|
|---|
| 122 | else PRINT_IF_GUID(g, stream_bitrate_guid);
|
|---|
| 123 | else PRINT_IF_GUID(g, ff_asf_language_guid);
|
|---|
| 124 | else
|
|---|
| 125 | av_dlog(NULL, "(GUID: unknown) ");
|
|---|
| 126 | for(i=0;i<16;i++)
|
|---|
| 127 | av_dlog(NULL, " 0x%02x,", (*g)[i]);
|
|---|
| 128 | av_dlog(NULL, "}\n");
|
|---|
| 129 | }
|
|---|
| 130 | #undef PRINT_IF_GUID
|
|---|
| 131 | #else
|
|---|
| 132 | #define print_guid(g)
|
|---|
| 133 | #endif
|
|---|
| 134 |
|
|---|
| 135 | static int asf_probe(AVProbeData *pd)
|
|---|
| 136 | {
|
|---|
| 137 | /* check file header */
|
|---|
| 138 | if (!ff_guidcmp(pd->buf, &ff_asf_header))
|
|---|
| 139 | return AVPROBE_SCORE_MAX;
|
|---|
| 140 | else
|
|---|
| 141 | return 0;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | static int get_value(AVIOContext *pb, int type){
|
|---|
| 145 | switch(type){
|
|---|
| 146 | case 2: return avio_rl32(pb);
|
|---|
| 147 | case 3: return avio_rl32(pb);
|
|---|
| 148 | case 4: return avio_rl64(pb);
|
|---|
| 149 | case 5: return avio_rl16(pb);
|
|---|
| 150 | default:return INT_MIN;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | static void get_tag(AVFormatContext *s, const char *key, int type, int len)
|
|---|
| 155 | {
|
|---|
| 156 | char *value;
|
|---|
| 157 | int64_t off = avio_tell(s->pb);
|
|---|
| 158 |
|
|---|
| 159 | if ((unsigned)len >= (UINT_MAX - 1)/2)
|
|---|
| 160 | return;
|
|---|
| 161 |
|
|---|
| 162 | value = av_malloc(2*len+1);
|
|---|
| 163 | if (!value)
|
|---|
| 164 | goto finish;
|
|---|
| 165 |
|
|---|
| 166 | if (type == 0) { // UTF16-LE
|
|---|
| 167 | avio_get_str16le(s->pb, len, value, 2*len + 1);
|
|---|
| 168 | } else if (type == -1) { // ASCII
|
|---|
| 169 | get_buffer(s->pb, value, len);
|
|---|
| 170 | value[len]=0;
|
|---|
| 171 | } else if (type > 1 && type <= 5) { // boolean or DWORD or QWORD or WORD
|
|---|
| 172 | uint64_t num = get_value(s->pb, type);
|
|---|
| 173 | snprintf(value, len, "%"PRIu64, num);
|
|---|
| 174 | } else {
|
|---|
| 175 | av_log(s, AV_LOG_DEBUG, "Unsupported value type %d in tag %s.\n", type, key);
|
|---|
| 176 | goto finish;
|
|---|
| 177 | }
|
|---|
| 178 | if (*value)
|
|---|
| 179 | av_dict_set(&s->metadata, key, value, 0);
|
|---|
| 180 | finish:
|
|---|
| 181 | av_freep(&value);
|
|---|
| 182 | avio_seek(s->pb, off + len, SEEK_SET);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | static int asf_read_file_properties(AVFormatContext *s, int64_t size)
|
|---|
| 186 | {
|
|---|
| 187 | ASFContext *asf = s->priv_data;
|
|---|
| 188 | AVIOContext *pb = s->pb;
|
|---|
| 189 |
|
|---|
| 190 | ff_get_guid(pb, &asf->hdr.guid);
|
|---|
| 191 | asf->hdr.file_size = avio_rl64(pb);
|
|---|
| 192 | asf->hdr.create_time = avio_rl64(pb);
|
|---|
| 193 | avio_rl64(pb); /* number of packets */
|
|---|
| 194 | asf->hdr.play_time = avio_rl64(pb);
|
|---|
| 195 | asf->hdr.send_time = avio_rl64(pb);
|
|---|
| 196 | asf->hdr.preroll = avio_rl32(pb);
|
|---|
| 197 | asf->hdr.ignore = avio_rl32(pb);
|
|---|
| 198 | asf->hdr.flags = avio_rl32(pb);
|
|---|
| 199 | asf->hdr.min_pktsize = avio_rl32(pb);
|
|---|
| 200 | asf->hdr.max_pktsize = avio_rl32(pb);
|
|---|
| 201 | asf->hdr.max_bitrate = avio_rl32(pb);
|
|---|
| 202 | s->packet_size = asf->hdr.max_pktsize;
|
|---|
| 203 |
|
|---|
| 204 | return 0;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
|
|---|
| 208 | {
|
|---|
| 209 | ASFContext *asf = s->priv_data;
|
|---|
| 210 | AVIOContext *pb = s->pb;
|
|---|
| 211 | AVStream *st;
|
|---|
| 212 | ASFStream *asf_st;
|
|---|
| 213 | ff_asf_guid g;
|
|---|
| 214 | enum AVMediaType type;
|
|---|
| 215 | int type_specific_size, sizeX;
|
|---|
| 216 | unsigned int tag1;
|
|---|
| 217 | int64_t pos1, pos2, start_time;
|
|---|
| 218 | int test_for_ext_stream_audio, is_dvr_ms_audio=0;
|
|---|
| 219 |
|
|---|
| 220 | if (s->nb_streams == ASF_MAX_STREAMS) {
|
|---|
| 221 | av_log(s, AV_LOG_ERROR, "too many streams\n");
|
|---|
| 222 | return AVERROR(EINVAL);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | pos1 = avio_tell(pb);
|
|---|
| 226 |
|
|---|
| 227 | st = avformat_new_stream(s, NULL);
|
|---|
| 228 | if (!st)
|
|---|
| 229 | return AVERROR(ENOMEM);
|
|---|
| 230 | av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
|
|---|
| 231 | asf_st = av_mallocz(sizeof(ASFStream));
|
|---|
| 232 | if (!asf_st)
|
|---|
| 233 | return AVERROR(ENOMEM);
|
|---|
| 234 | st->priv_data = asf_st;
|
|---|
| 235 | start_time = asf->hdr.preroll;
|
|---|
| 236 |
|
|---|
| 237 | asf_st->stream_language_index = 128; // invalid stream index means no language info
|
|---|
| 238 |
|
|---|
| 239 | if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
|
|---|
| 240 | st->duration = asf->hdr.play_time /
|
|---|
| 241 | (10000000 / 1000) - start_time;
|
|---|
| 242 | }
|
|---|
| 243 | ff_get_guid(pb, &g);
|
|---|
| 244 |
|
|---|
| 245 | test_for_ext_stream_audio = 0;
|
|---|
| 246 | if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
|
|---|
| 247 | type = AVMEDIA_TYPE_AUDIO;
|
|---|
| 248 | } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
|
|---|
| 249 | type = AVMEDIA_TYPE_VIDEO;
|
|---|
| 250 | } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
|
|---|
| 251 | type = AVMEDIA_TYPE_VIDEO;
|
|---|
| 252 | st->codec->codec_id = CODEC_ID_MJPEG;
|
|---|
| 253 | } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
|
|---|
| 254 | type = AVMEDIA_TYPE_DATA;
|
|---|
| 255 | } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
|
|---|
| 256 | test_for_ext_stream_audio = 1;
|
|---|
| 257 | type = AVMEDIA_TYPE_UNKNOWN;
|
|---|
| 258 | } else {
|
|---|
| 259 | return -1;
|
|---|
| 260 | }
|
|---|
| 261 | ff_get_guid(pb, &g);
|
|---|
| 262 | avio_skip(pb, 8); /* total_size */
|
|---|
| 263 | type_specific_size = avio_rl32(pb);
|
|---|
| 264 | avio_rl32(pb);
|
|---|
| 265 | st->id = avio_rl16(pb) & 0x7f; /* stream id */
|
|---|
| 266 | // mapping of asf ID to AV stream ID;
|
|---|
| 267 | asf->asfid2avid[st->id] = s->nb_streams - 1;
|
|---|
| 268 |
|
|---|
| 269 | avio_rl32(pb);
|
|---|
| 270 |
|
|---|
| 271 | if (test_for_ext_stream_audio) {
|
|---|
| 272 | ff_get_guid(pb, &g);
|
|---|
| 273 | if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
|
|---|
| 274 | type = AVMEDIA_TYPE_AUDIO;
|
|---|
| 275 | is_dvr_ms_audio=1;
|
|---|
| 276 | ff_get_guid(pb, &g);
|
|---|
| 277 | avio_rl32(pb);
|
|---|
| 278 | avio_rl32(pb);
|
|---|
| 279 | avio_rl32(pb);
|
|---|
| 280 | ff_get_guid(pb, &g);
|
|---|
| 281 | avio_rl32(pb);
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | st->codec->codec_type = type;
|
|---|
| 286 | if (type == AVMEDIA_TYPE_AUDIO) {
|
|---|
| 287 | int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
|
|---|
| 288 | if (ret < 0)
|
|---|
| 289 | return ret;
|
|---|
| 290 | if (is_dvr_ms_audio) {
|
|---|
| 291 | // codec_id and codec_tag are unreliable in dvr_ms
|
|---|
| 292 | // files. Set them later by probing stream.
|
|---|
| 293 | st->request_probe= 1;
|
|---|
| 294 | st->codec->codec_tag = 0;
|
|---|
| 295 | }
|
|---|
| 296 | if (st->codec->codec_id == CODEC_ID_AAC) {
|
|---|
| 297 | st->need_parsing = AVSTREAM_PARSE_NONE;
|
|---|
| 298 | } else {
|
|---|
| 299 | st->need_parsing = AVSTREAM_PARSE_FULL;
|
|---|
| 300 | }
|
|---|
| 301 | /* We have to init the frame size at some point .... */
|
|---|
| 302 | pos2 = avio_tell(pb);
|
|---|
| 303 | if (size >= (pos2 + 8 - pos1 + 24)) {
|
|---|
| 304 | asf_st->ds_span = avio_r8(pb);
|
|---|
| 305 | asf_st->ds_packet_size = avio_rl16(pb);
|
|---|
| 306 | asf_st->ds_chunk_size = avio_rl16(pb);
|
|---|
| 307 | avio_rl16(pb); //ds_data_size
|
|---|
| 308 | avio_r8(pb); //ds_silence_data
|
|---|
| 309 | }
|
|---|
| 310 | //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
|
|---|
| 311 | // asf_st->ds_packet_size, asf_st->ds_chunk_size,
|
|---|
| 312 | // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
|
|---|
| 313 | if (asf_st->ds_span > 1) {
|
|---|
| 314 | if (!asf_st->ds_chunk_size
|
|---|
| 315 | || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
|
|---|
| 316 | || asf_st->ds_packet_size % asf_st->ds_chunk_size)
|
|---|
| 317 | asf_st->ds_span = 0; // disable descrambling
|
|---|
| 318 | }
|
|---|
| 319 | switch (st->codec->codec_id) {
|
|---|
| 320 | case CODEC_ID_MP3:
|
|---|
| 321 | st->codec->frame_size = MPA_FRAME_SIZE;
|
|---|
| 322 | break;
|
|---|
| 323 | case CODEC_ID_PCM_S16LE:
|
|---|
| 324 | case CODEC_ID_PCM_S16BE:
|
|---|
| 325 | case CODEC_ID_PCM_U16LE:
|
|---|
| 326 | case CODEC_ID_PCM_U16BE:
|
|---|
| 327 | case CODEC_ID_PCM_S8:
|
|---|
| 328 | case CODEC_ID_PCM_U8:
|
|---|
| 329 | case CODEC_ID_PCM_ALAW:
|
|---|
| 330 | case CODEC_ID_PCM_MULAW:
|
|---|
| 331 | st->codec->frame_size = 1;
|
|---|
| 332 | break;
|
|---|
| 333 | default:
|
|---|
| 334 | /* This is probably wrong, but it prevents a crash later */
|
|---|
| 335 | st->codec->frame_size = 1;
|
|---|
| 336 | break;
|
|---|
| 337 | }
|
|---|
| 338 | } else if (type == AVMEDIA_TYPE_VIDEO &&
|
|---|
| 339 | size - (avio_tell(pb) - pos1 + 24) >= 51) {
|
|---|
| 340 | avio_rl32(pb);
|
|---|
| 341 | avio_rl32(pb);
|
|---|
| 342 | avio_r8(pb);
|
|---|
| 343 | avio_rl16(pb); /* size */
|
|---|
| 344 | sizeX= avio_rl32(pb); /* size */
|
|---|
| 345 | st->codec->width = avio_rl32(pb);
|
|---|
| 346 | st->codec->height = avio_rl32(pb);
|
|---|
| 347 | /* not available for asf */
|
|---|
| 348 | avio_rl16(pb); /* panes */
|
|---|
| 349 | st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
|
|---|
| 350 | tag1 = avio_rl32(pb);
|
|---|
| 351 | avio_skip(pb, 20);
|
|---|
| 352 | // av_log(s, AV_LOG_DEBUG, "size:%d tsize:%d sizeX:%d\n", size, total_size, sizeX);
|
|---|
| 353 | if (sizeX > 40) {
|
|---|
| 354 | st->codec->extradata_size = sizeX - 40;
|
|---|
| 355 | st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|---|
| 356 | avio_read(pb, st->codec->extradata, st->codec->extradata_size);
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | /* Extract palette from extradata if bpp <= 8 */
|
|---|
| 360 | /* This code assumes that extradata contains only palette */
|
|---|
| 361 | /* This is true for all paletted codecs implemented in ffmpeg */
|
|---|
| 362 | if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
|
|---|
| 363 | int av_unused i;
|
|---|
| 364 | #if HAVE_BIGENDIAN
|
|---|
| 365 | for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
|
|---|
| 366 | asf_st->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
|
|---|
| 367 | #else
|
|---|
| 368 | memcpy(asf_st->palette, st->codec->extradata,
|
|---|
| 369 | FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
|
|---|
| 370 | #endif
|
|---|
| 371 | asf_st->palette_changed = 1;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | st->codec->codec_tag = tag1;
|
|---|
| 375 | st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
|
|---|
| 376 | if(tag1 == MKTAG('D', 'V', 'R', ' ')){
|
|---|
| 377 | st->need_parsing = AVSTREAM_PARSE_FULL;
|
|---|
| 378 | // issue658 containse wrong w/h and MS even puts a fake seq header with wrong w/h in extradata while a correct one is in te stream. maximum lameness
|
|---|
| 379 | st->codec->width =
|
|---|
| 380 | st->codec->height = 0;
|
|---|
| 381 | av_freep(&st->codec->extradata);
|
|---|
| 382 | st->codec->extradata_size=0;
|
|---|
| 383 | }
|
|---|
| 384 | if(st->codec->codec_id == CODEC_ID_H264)
|
|---|
| 385 | st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
|
|---|
| 386 | }
|
|---|
| 387 | pos2 = avio_tell(pb);
|
|---|
| 388 | avio_skip(pb, size - (pos2 - pos1 + 24));
|
|---|
| 389 |
|
|---|
| 390 | return 0;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
|
|---|
| 394 | {
|
|---|
| 395 | ASFContext *asf = s->priv_data;
|
|---|
| 396 | AVIOContext *pb = s->pb;
|
|---|
| 397 | ff_asf_guid g;
|
|---|
| 398 | int ext_len, payload_ext_ct, stream_ct, i;
|
|---|
| 399 | uint32_t leak_rate, stream_num;
|
|---|
| 400 | unsigned int stream_languageid_index;
|
|---|
| 401 |
|
|---|
| 402 | avio_rl64(pb); // starttime
|
|---|
| 403 | avio_rl64(pb); // endtime
|
|---|
| 404 | leak_rate = avio_rl32(pb); // leak-datarate
|
|---|
| 405 | avio_rl32(pb); // bucket-datasize
|
|---|
| 406 | avio_rl32(pb); // init-bucket-fullness
|
|---|
| 407 | avio_rl32(pb); // alt-leak-datarate
|
|---|
| 408 | avio_rl32(pb); // alt-bucket-datasize
|
|---|
| 409 | avio_rl32(pb); // alt-init-bucket-fullness
|
|---|
| 410 | avio_rl32(pb); // max-object-size
|
|---|
| 411 | avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
|
|---|
| 412 | stream_num = avio_rl16(pb); // stream-num
|
|---|
| 413 |
|
|---|
| 414 | stream_languageid_index = avio_rl16(pb); // stream-language-id-index
|
|---|
| 415 | if (stream_num < 128)
|
|---|
| 416 | asf->streams[stream_num].stream_language_index = stream_languageid_index;
|
|---|
| 417 |
|
|---|
| 418 | avio_rl64(pb); // avg frametime in 100ns units
|
|---|
| 419 | stream_ct = avio_rl16(pb); //stream-name-count
|
|---|
| 420 | payload_ext_ct = avio_rl16(pb); //payload-extension-system-count
|
|---|
| 421 |
|
|---|
| 422 | if (stream_num < 128)
|
|---|
| 423 | asf->stream_bitrates[stream_num] = leak_rate;
|
|---|
| 424 |
|
|---|
| 425 | for (i=0; i<stream_ct; i++){
|
|---|
| 426 | avio_rl16(pb);
|
|---|
| 427 | ext_len = avio_rl16(pb);
|
|---|
| 428 | avio_skip(pb, ext_len);
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | for (i=0; i<payload_ext_ct; i++){
|
|---|
| 432 | ff_get_guid(pb, &g);
|
|---|
| 433 | avio_skip(pb, 2);
|
|---|
| 434 | ext_len=avio_rl32(pb);
|
|---|
| 435 | avio_skip(pb, ext_len);
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | return 0;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | static int asf_read_content_desc(AVFormatContext *s, int64_t size)
|
|---|
| 442 | {
|
|---|
| 443 | AVIOContext *pb = s->pb;
|
|---|
| 444 | int len1, len2, len3, len4, len5;
|
|---|
| 445 |
|
|---|
| 446 | len1 = avio_rl16(pb);
|
|---|
| 447 | len2 = avio_rl16(pb);
|
|---|
| 448 | len3 = avio_rl16(pb);
|
|---|
| 449 | len4 = avio_rl16(pb);
|
|---|
| 450 | len5 = avio_rl16(pb);
|
|---|
| 451 | get_tag(s, "title" , 0, len1);
|
|---|
| 452 | get_tag(s, "author" , 0, len2);
|
|---|
| 453 | get_tag(s, "copyright", 0, len3);
|
|---|
| 454 | get_tag(s, "comment" , 0, len4);
|
|---|
| 455 | avio_skip(pb, len5);
|
|---|
| 456 |
|
|---|
| 457 | return 0;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
|
|---|
| 461 | {
|
|---|
| 462 | AVIOContext *pb = s->pb;
|
|---|
| 463 | ASFContext *asf = s->priv_data;
|
|---|
| 464 | int desc_count, i, ret;
|
|---|
| 465 |
|
|---|
| 466 | desc_count = avio_rl16(pb);
|
|---|
| 467 | for(i=0;i<desc_count;i++) {
|
|---|
| 468 | int name_len,value_type,value_len;
|
|---|
| 469 | char name[1024];
|
|---|
| 470 |
|
|---|
| 471 | name_len = avio_rl16(pb);
|
|---|
| 472 | if (name_len%2) // must be even, broken lavf versions wrote len-1
|
|---|
| 473 | name_len += 1;
|
|---|
| 474 | if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
|
|---|
| 475 | avio_skip(pb, name_len - ret);
|
|---|
| 476 | value_type = avio_rl16(pb);
|
|---|
| 477 | value_len = avio_rl16(pb);
|
|---|
| 478 | if (!value_type && value_len%2)
|
|---|
| 479 | value_len += 1;
|
|---|
| 480 | /**
|
|---|
| 481 | * My sample has that stream set to 0 maybe that mean the container.
|
|---|
| 482 | * Asf stream count start at 1. I am using 0 to the container value since it's unused
|
|---|
| 483 | */
|
|---|
| 484 | if (!strcmp(name, "AspectRatioX")){
|
|---|
| 485 | asf->dar[0].num= get_value(s->pb, value_type);
|
|---|
| 486 | } else if(!strcmp(name, "AspectRatioY")){
|
|---|
| 487 | asf->dar[0].den= get_value(s->pb, value_type);
|
|---|
| 488 | } else
|
|---|
| 489 | get_tag(s, name, value_type, value_len);
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | return 0;
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | static int asf_read_language_list(AVFormatContext *s, int64_t size)
|
|---|
| 496 | {
|
|---|
| 497 | AVIOContext *pb = s->pb;
|
|---|
| 498 | ASFContext *asf = s->priv_data;
|
|---|
| 499 | int j, ret;
|
|---|
| 500 | int stream_count = avio_rl16(pb);
|
|---|
| 501 | for(j = 0; j < stream_count; j++) {
|
|---|
| 502 | char lang[6];
|
|---|
| 503 | unsigned int lang_len = avio_r8(pb);
|
|---|
| 504 | if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
|
|---|
| 505 | avio_skip(pb, lang_len - ret);
|
|---|
| 506 | if (j < 128)
|
|---|
| 507 | av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | return 0;
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | static int asf_read_metadata(AVFormatContext *s, int64_t size)
|
|---|
| 514 | {
|
|---|
| 515 | AVIOContext *pb = s->pb;
|
|---|
| 516 | ASFContext *asf = s->priv_data;
|
|---|
| 517 | int n, stream_num, name_len, value_len, value_num;
|
|---|
| 518 | int ret, i;
|
|---|
| 519 | n = avio_rl16(pb);
|
|---|
| 520 |
|
|---|
| 521 | for(i=0;i<n;i++) {
|
|---|
| 522 | char name[1024];
|
|---|
| 523 |
|
|---|
| 524 | avio_rl16(pb); //lang_list_index
|
|---|
| 525 | stream_num= avio_rl16(pb);
|
|---|
| 526 | name_len= avio_rl16(pb);
|
|---|
| 527 | avio_skip(pb, 2); /* value_type */
|
|---|
| 528 | value_len= avio_rl32(pb);
|
|---|
| 529 |
|
|---|
| 530 | if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
|
|---|
| 531 | avio_skip(pb, name_len - ret);
|
|---|
| 532 | //av_log(s, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
|
|---|
| 533 | value_num= avio_rl16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
|
|---|
| 534 | avio_skip(pb, value_len - 2);
|
|---|
| 535 |
|
|---|
| 536 | if(stream_num<128){
|
|---|
| 537 | if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
|
|---|
| 538 | else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
|
|---|
| 539 | }
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | return 0;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | static int asf_read_marker(AVFormatContext *s, int64_t size)
|
|---|
| 546 | {
|
|---|
| 547 | AVIOContext *pb = s->pb;
|
|---|
| 548 | int i, count, name_len, ret;
|
|---|
| 549 | char name[1024];
|
|---|
| 550 |
|
|---|
| 551 | avio_rl64(pb); // reserved 16 bytes
|
|---|
| 552 | avio_rl64(pb); // ...
|
|---|
| 553 | count = avio_rl32(pb); // markers count
|
|---|
| 554 | avio_rl16(pb); // reserved 2 bytes
|
|---|
| 555 | name_len = avio_rl16(pb); // name length
|
|---|
| 556 | for(i=0;i<name_len;i++){
|
|---|
| 557 | avio_r8(pb); // skip the name
|
|---|
| 558 | }
|
|---|
| 559 |
|
|---|
| 560 | for(i=0;i<count;i++){
|
|---|
| 561 | int64_t pres_time;
|
|---|
| 562 | int name_len;
|
|---|
| 563 |
|
|---|
| 564 | avio_rl64(pb); // offset, 8 bytes
|
|---|
| 565 | pres_time = avio_rl64(pb); // presentation time
|
|---|
| 566 | avio_rl16(pb); // entry length
|
|---|
| 567 | avio_rl32(pb); // send time
|
|---|
| 568 | avio_rl32(pb); // flags
|
|---|
| 569 | name_len = avio_rl32(pb); // name length
|
|---|
| 570 | if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
|
|---|
| 571 | avio_skip(pb, name_len - ret);
|
|---|
| 572 | avpriv_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | return 0;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
|
|---|
| 579 | {
|
|---|
| 580 | ASFContext *asf = s->priv_data;
|
|---|
| 581 | ff_asf_guid g;
|
|---|
| 582 | AVIOContext *pb = s->pb;
|
|---|
| 583 | int i;
|
|---|
| 584 | int64_t gsize;
|
|---|
| 585 |
|
|---|
| 586 | ff_get_guid(pb, &g);
|
|---|
| 587 | if (ff_guidcmp(&g, &ff_asf_header))
|
|---|
| 588 | return -1;
|
|---|
| 589 | avio_rl64(pb);
|
|---|
| 590 | avio_rl32(pb);
|
|---|
| 591 | avio_r8(pb);
|
|---|
| 592 | avio_r8(pb);
|
|---|
| 593 | memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
|
|---|
| 594 | for(;;) {
|
|---|
| 595 | uint64_t gpos= avio_tell(pb);
|
|---|
| 596 | ff_get_guid(pb, &g);
|
|---|
| 597 | gsize = avio_rl64(pb);
|
|---|
| 598 | av_dlog(s, "%08"PRIx64": ", gpos);
|
|---|
| 599 | print_guid(&g);
|
|---|
| 600 | av_dlog(s, " size=0x%"PRIx64"\n", gsize);
|
|---|
| 601 | if (!ff_guidcmp(&g, &ff_asf_data_header)) {
|
|---|
| 602 | asf->data_object_offset = avio_tell(pb);
|
|---|
| 603 | // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
|
|---|
| 604 | if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
|
|---|
| 605 | asf->data_object_size = gsize - 24;
|
|---|
| 606 | } else {
|
|---|
| 607 | asf->data_object_size = (uint64_t)-1;
|
|---|
| 608 | }
|
|---|
| 609 | break;
|
|---|
| 610 | }
|
|---|
| 611 | if (gsize < 24)
|
|---|
| 612 | return -1;
|
|---|
| 613 | if (!ff_guidcmp(&g, &ff_asf_file_header)) {
|
|---|
| 614 | asf_read_file_properties(s, gsize);
|
|---|
| 615 | } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
|
|---|
| 616 | asf_read_stream_properties(s, gsize);
|
|---|
| 617 | } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
|
|---|
| 618 | asf_read_content_desc(s, gsize);
|
|---|
| 619 | } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
|
|---|
| 620 | asf_read_language_list(s, gsize);
|
|---|
| 621 | } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
|
|---|
| 622 | asf_read_ext_content_desc(s, gsize);
|
|---|
| 623 | } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
|
|---|
| 624 | asf_read_metadata(s, gsize);
|
|---|
| 625 | } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
|
|---|
| 626 | asf_read_ext_stream_properties(s, gsize);
|
|---|
| 627 |
|
|---|
| 628 | // there could be a optional stream properties object to follow
|
|---|
| 629 | // if so the next iteration will pick it up
|
|---|
| 630 | continue;
|
|---|
| 631 | } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
|
|---|
| 632 | ff_get_guid(pb, &g);
|
|---|
| 633 | avio_skip(pb, 6);
|
|---|
| 634 | continue;
|
|---|
| 635 | } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
|
|---|
| 636 | asf_read_marker(s, gsize);
|
|---|
| 637 | } else if (url_feof(pb)) {
|
|---|
| 638 | return -1;
|
|---|
| 639 | } else {
|
|---|
| 640 | if (!s->keylen) {
|
|---|
| 641 | if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
|
|---|
| 642 | unsigned int len;
|
|---|
| 643 | AVPacket pkt;
|
|---|
| 644 | av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
|
|---|
| 645 | len= avio_rl32(pb);
|
|---|
| 646 | av_log(s, AV_LOG_DEBUG, "Secret data:\n");
|
|---|
| 647 | av_get_packet(pb, &pkt, len); av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size); av_free_packet(&pkt);
|
|---|
| 648 | len= avio_rl32(pb);
|
|---|
| 649 | get_tag(s, "ASF_Protection_Type", -1, len);
|
|---|
| 650 | len= avio_rl32(pb);
|
|---|
| 651 | get_tag(s, "ASF_Key_ID", -1, len);
|
|---|
| 652 | len= avio_rl32(pb);
|
|---|
| 653 | get_tag(s, "ASF_License_URL", -1, len);
|
|---|
| 654 | } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
|
|---|
| 655 | av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
|
|---|
| 656 | av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
|
|---|
| 657 | } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
|
|---|
| 658 | av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
|
|---|
| 659 | }
|
|---|
| 660 | }
|
|---|
| 661 | }
|
|---|
| 662 | if(avio_tell(pb) != gpos + gsize)
|
|---|
| 663 | av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", avio_tell(pb)-gpos, gsize);
|
|---|
| 664 | avio_seek(pb, gpos + gsize, SEEK_SET);
|
|---|
| 665 | }
|
|---|
| 666 | ff_get_guid(pb, &g);
|
|---|
| 667 | avio_rl64(pb);
|
|---|
| 668 | avio_r8(pb);
|
|---|
| 669 | avio_r8(pb);
|
|---|
| 670 | if (url_feof(pb))
|
|---|
| 671 | return -1;
|
|---|
| 672 | asf->data_offset = avio_tell(pb);
|
|---|
| 673 | asf->packet_size_left = 0;
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 | for(i=0; i<128; i++){
|
|---|
| 677 | int stream_num= asf->asfid2avid[i];
|
|---|
| 678 | if(stream_num>=0){
|
|---|
| 679 | AVStream *st = s->streams[stream_num];
|
|---|
| 680 | if (!st->codec->bit_rate)
|
|---|
| 681 | st->codec->bit_rate = asf->stream_bitrates[i];
|
|---|
| 682 | if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
|
|---|
| 683 | av_reduce(&st->sample_aspect_ratio.num,
|
|---|
| 684 | &st->sample_aspect_ratio.den,
|
|---|
| 685 | asf->dar[i].num, asf->dar[i].den, INT_MAX);
|
|---|
| 686 | } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) && (st->codec->codec_type==AVMEDIA_TYPE_VIDEO)) // Use ASF container value if the stream doesn't AR set.
|
|---|
| 687 | av_reduce(&st->sample_aspect_ratio.num,
|
|---|
| 688 | &st->sample_aspect_ratio.den,
|
|---|
| 689 | asf->dar[0].num, asf->dar[0].den, INT_MAX);
|
|---|
| 690 |
|
|---|
| 691 | //av_log(s, AV_LOG_INFO, "i=%d, st->codec->codec_type:%d, dar %d:%d sar=%d:%d\n", i, st->codec->codec_type, dar[i].num, dar[i].den, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
|
|---|
| 692 |
|
|---|
| 693 | // copy and convert language codes to the frontend
|
|---|
| 694 | if (asf->streams[i].stream_language_index < 128) {
|
|---|
| 695 | const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
|
|---|
| 696 | if (rfc1766 && strlen(rfc1766) > 1) {
|
|---|
| 697 | const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
|
|---|
| 698 | const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
|
|---|
| 699 | if (iso6392)
|
|---|
| 700 | av_dict_set(&st->metadata, "language", iso6392, 0);
|
|---|
| 701 | }
|
|---|
| 702 | }
|
|---|
| 703 | }
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
|
|---|
| 707 |
|
|---|
| 708 | return 0;
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | #define DO_2BITS(bits, var, defval) \
|
|---|
| 712 | switch (bits & 3) \
|
|---|
| 713 | { \
|
|---|
| 714 | case 3: var = avio_rl32(pb); rsize += 4; break; \
|
|---|
| 715 | case 2: var = avio_rl16(pb); rsize += 2; break; \
|
|---|
| 716 | case 1: var = avio_r8(pb); rsize++; break; \
|
|---|
| 717 | default: var = defval; break; \
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | /**
|
|---|
| 721 | * Load a single ASF packet into the demuxer.
|
|---|
| 722 | * @param s demux context
|
|---|
| 723 | * @param pb context to read data from
|
|---|
| 724 | * @return 0 on success, <0 on error
|
|---|
| 725 | */
|
|---|
| 726 | static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
|
|---|
| 727 | {
|
|---|
| 728 | ASFContext *asf = s->priv_data;
|
|---|
| 729 | uint32_t packet_length, padsize;
|
|---|
| 730 | int rsize = 8;
|
|---|
| 731 | int c, d, e, off;
|
|---|
| 732 |
|
|---|
| 733 | // if we do not know packet size, allow skipping up to 32 kB
|
|---|
| 734 | off= 32768;
|
|---|
| 735 | if (s->packet_size > 0)
|
|---|
| 736 | off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
|
|---|
| 737 |
|
|---|
| 738 | c=d=e=-1;
|
|---|
| 739 | while(off-- > 0){
|
|---|
| 740 | c=d; d=e;
|
|---|
| 741 | e= avio_r8(pb);
|
|---|
| 742 | if(c == 0x82 && !d && !e)
|
|---|
| 743 | break;
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | if (c != 0x82) {
|
|---|
| 747 | /**
|
|---|
| 748 | * This code allows handling of -EAGAIN at packet boundaries (i.e.
|
|---|
| 749 | * if the packet sync code above triggers -EAGAIN). This does not
|
|---|
| 750 | * imply complete -EAGAIN handling support at random positions in
|
|---|
| 751 | * the stream.
|
|---|
| 752 | */
|
|---|
| 753 | if (pb->error == AVERROR(EAGAIN))
|
|---|
| 754 | return AVERROR(EAGAIN);
|
|---|
| 755 | if (!url_feof(pb))
|
|---|
| 756 | av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
|
|---|
| 757 | }
|
|---|
| 758 | if ((c & 0x8f) == 0x82) {
|
|---|
| 759 | if (d || e) {
|
|---|
| 760 | if (!url_feof(pb))
|
|---|
| 761 | av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
|
|---|
| 762 | return -1;
|
|---|
| 763 | }
|
|---|
| 764 | c= avio_r8(pb);
|
|---|
| 765 | d= avio_r8(pb);
|
|---|
| 766 | rsize+=3;
|
|---|
| 767 | }else{
|
|---|
| 768 | avio_seek(pb, -1, SEEK_CUR); //FIXME
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | asf->packet_flags = c;
|
|---|
| 772 | asf->packet_property = d;
|
|---|
| 773 |
|
|---|
| 774 | DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
|
|---|
| 775 | DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
|
|---|
| 776 | DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
|
|---|
| 777 |
|
|---|
| 778 | //the following checks prevent overflows and infinite loops
|
|---|
| 779 | if(!packet_length || packet_length >= (1U<<29)){
|
|---|
| 780 | av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
|
|---|
| 781 | return -1;
|
|---|
| 782 | }
|
|---|
| 783 | if(padsize >= packet_length){
|
|---|
| 784 | av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
|
|---|
| 785 | return -1;
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 | asf->packet_timestamp = avio_rl32(pb);
|
|---|
| 789 | avio_rl16(pb); /* duration */
|
|---|
| 790 | // rsize has at least 11 bytes which have to be present
|
|---|
| 791 |
|
|---|
| 792 | if (asf->packet_flags & 0x01) {
|
|---|
| 793 | asf->packet_segsizetype = avio_r8(pb); rsize++;
|
|---|
| 794 | asf->packet_segments = asf->packet_segsizetype & 0x3f;
|
|---|
| 795 | } else {
|
|---|
| 796 | asf->packet_segments = 1;
|
|---|
| 797 | asf->packet_segsizetype = 0x80;
|
|---|
| 798 | }
|
|---|
| 799 | asf->packet_size_left = packet_length - padsize - rsize;
|
|---|
| 800 | if (packet_length < asf->hdr.min_pktsize)
|
|---|
| 801 | padsize += asf->hdr.min_pktsize - packet_length;
|
|---|
| 802 | asf->packet_padsize = padsize;
|
|---|
| 803 | av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
|
|---|
| 804 | return 0;
|
|---|
| 805 | }
|
|---|
| 806 |
|
|---|
| 807 | /**
|
|---|
| 808 | *
|
|---|
| 809 | * @return <0 if error
|
|---|
| 810 | */
|
|---|
| 811 | static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
|
|---|
| 812 | ASFContext *asf = s->priv_data;
|
|---|
| 813 | int rsize = 1;
|
|---|
| 814 | int num = avio_r8(pb);
|
|---|
| 815 | int64_t ts0;
|
|---|
| 816 |
|
|---|
| 817 | asf->packet_segments--;
|
|---|
| 818 | asf->packet_key_frame = num >> 7;
|
|---|
| 819 | asf->stream_index = asf->asfid2avid[num & 0x7f];
|
|---|
| 820 | // sequence should be ignored!
|
|---|
| 821 | DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
|
|---|
| 822 | DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
|
|---|
| 823 | DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
|
|---|
| 824 | //printf("key:%d stream:%d seq:%d offset:%d replic_size:%d\n", asf->packet_key_frame, asf->stream_index, asf->packet_seq, //asf->packet_frag_offset, asf->packet_replic_size);
|
|---|
| 825 | if (asf->packet_replic_size >= 8) {
|
|---|
| 826 | asf->packet_obj_size = avio_rl32(pb);
|
|---|
| 827 | if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
|
|---|
| 828 | av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
|
|---|
| 829 | return -1;
|
|---|
| 830 | }
|
|---|
| 831 | asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
|
|---|
| 832 | if(asf->packet_replic_size >= 8+38+4){
|
|---|
| 833 | // for(i=0; i<asf->packet_replic_size-8; i++)
|
|---|
| 834 | // av_log(s, AV_LOG_DEBUG, "%02X ",avio_r8(pb));
|
|---|
| 835 | // av_log(s, AV_LOG_DEBUG, "\n");
|
|---|
| 836 | avio_skip(pb, 10);
|
|---|
| 837 | ts0= avio_rl64(pb);
|
|---|
| 838 | avio_skip(pb, 8);;
|
|---|
| 839 | avio_skip(pb, 12);
|
|---|
| 840 | avio_rl32(pb);
|
|---|
| 841 | avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
|
|---|
| 842 | if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
|
|---|
| 843 | else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
|
|---|
| 844 | }else
|
|---|
| 845 | avio_skip(pb, asf->packet_replic_size - 8);
|
|---|
| 846 | rsize += asf->packet_replic_size; // FIXME - check validity
|
|---|
| 847 | } else if (asf->packet_replic_size==1){
|
|---|
| 848 | // multipacket - frag_offset is beginning timestamp
|
|---|
| 849 | asf->packet_time_start = asf->packet_frag_offset;
|
|---|
| 850 | asf->packet_frag_offset = 0;
|
|---|
| 851 | asf->packet_frag_timestamp = asf->packet_timestamp;
|
|---|
| 852 |
|
|---|
| 853 | asf->packet_time_delta = avio_r8(pb);
|
|---|
| 854 | rsize++;
|
|---|
| 855 | }else if(asf->packet_replic_size!=0){
|
|---|
| 856 | av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
|
|---|
| 857 | return -1;
|
|---|
| 858 | }
|
|---|
| 859 | if (rsize > asf->packet_size_left) {
|
|---|
| 860 | av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
|
|---|
| 861 | return -1;
|
|---|
| 862 | }
|
|---|
| 863 | if (asf->packet_flags & 0x01) {
|
|---|
| 864 | DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
|
|---|
| 865 | if (rsize > asf->packet_size_left) {
|
|---|
| 866 | av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
|
|---|
| 867 | return -1;
|
|---|
| 868 | } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
|
|---|
| 869 | if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
|
|---|
| 870 | av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
|
|---|
| 871 | return -1;
|
|---|
| 872 | } else {
|
|---|
| 873 | int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
|
|---|
| 874 | asf->packet_size_left += diff;
|
|---|
| 875 | asf->packet_padsize -= diff;
|
|---|
| 876 | }
|
|---|
| 877 | }
|
|---|
| 878 | //printf("Fragsize %d\n", asf->packet_frag_size);
|
|---|
| 879 | } else {
|
|---|
| 880 | asf->packet_frag_size = asf->packet_size_left - rsize;
|
|---|
| 881 | //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
|
|---|
| 882 | }
|
|---|
| 883 | if (asf->packet_replic_size == 1) {
|
|---|
| 884 | asf->packet_multi_size = asf->packet_frag_size;
|
|---|
| 885 | if (asf->packet_multi_size > asf->packet_size_left)
|
|---|
| 886 | return -1;
|
|---|
| 887 | }
|
|---|
| 888 | asf->packet_size_left -= rsize;
|
|---|
| 889 | //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
|
|---|
| 890 |
|
|---|
| 891 | return 0;
|
|---|
| 892 | }
|
|---|
| 893 |
|
|---|
| 894 | /**
|
|---|
| 895 | * Parse data from individual ASF packets (which were previously loaded
|
|---|
| 896 | * with asf_get_packet()).
|
|---|
| 897 | * @param s demux context
|
|---|
| 898 | * @param pb context to read data from
|
|---|
| 899 | * @param pkt pointer to store packet data into
|
|---|
| 900 | * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
|
|---|
| 901 | * packets need to be loaded (through asf_get_packet())
|
|---|
| 902 | */
|
|---|
| 903 | static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
|
|---|
| 904 | {
|
|---|
| 905 | ASFContext *asf = s->priv_data;
|
|---|
| 906 | ASFStream *asf_st = 0;
|
|---|
| 907 | for (;;) {
|
|---|
| 908 | int ret;
|
|---|
| 909 | if(url_feof(pb))
|
|---|
| 910 | return AVERROR_EOF;
|
|---|
| 911 | if (asf->packet_size_left < FRAME_HEADER_SIZE
|
|---|
| 912 | || asf->packet_segments < 1) {
|
|---|
| 913 | //asf->packet_size_left <= asf->packet_padsize) {
|
|---|
| 914 | int ret = asf->packet_size_left + asf->packet_padsize;
|
|---|
| 915 | //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, avio_tell(pb));
|
|---|
| 916 | assert(ret>=0);
|
|---|
| 917 | /* fail safe */
|
|---|
| 918 | avio_skip(pb, ret);
|
|---|
| 919 |
|
|---|
| 920 | asf->packet_pos= avio_tell(pb);
|
|---|
| 921 | if (asf->data_object_size != (uint64_t)-1 &&
|
|---|
| 922 | (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
|
|---|
| 923 | return AVERROR_EOF; /* Do not exceed the size of the data object */
|
|---|
| 924 | return 1;
|
|---|
| 925 | }
|
|---|
| 926 | if (asf->packet_time_start == 0) {
|
|---|
| 927 | if(asf_read_frame_header(s, pb) < 0){
|
|---|
| 928 | asf->packet_segments= 0;
|
|---|
| 929 | continue;
|
|---|
| 930 | }
|
|---|
| 931 | if (asf->stream_index < 0
|
|---|
| 932 | || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
|
|---|
| 933 | || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
|
|---|
| 934 | ) {
|
|---|
| 935 | asf->packet_time_start = 0;
|
|---|
| 936 | /* unhandled packet (should not happen) */
|
|---|
| 937 | avio_skip(pb, asf->packet_frag_size);
|
|---|
| 938 | asf->packet_size_left -= asf->packet_frag_size;
|
|---|
| 939 | if(asf->stream_index < 0)
|
|---|
| 940 | av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
|
|---|
| 941 | continue;
|
|---|
| 942 | }
|
|---|
| 943 | asf->asf_st = s->streams[asf->stream_index]->priv_data;
|
|---|
| 944 | }
|
|---|
| 945 | asf_st = asf->asf_st;
|
|---|
| 946 |
|
|---|
| 947 | if (asf->packet_replic_size == 1) {
|
|---|
| 948 | // frag_offset is here used as the beginning timestamp
|
|---|
| 949 | asf->packet_frag_timestamp = asf->packet_time_start;
|
|---|
| 950 | asf->packet_time_start += asf->packet_time_delta;
|
|---|
| 951 | asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
|
|---|
| 952 | asf->packet_size_left--;
|
|---|
| 953 | asf->packet_multi_size--;
|
|---|
| 954 | if (asf->packet_multi_size < asf->packet_obj_size)
|
|---|
| 955 | {
|
|---|
| 956 | asf->packet_time_start = 0;
|
|---|
| 957 | avio_skip(pb, asf->packet_multi_size);
|
|---|
| 958 | asf->packet_size_left -= asf->packet_multi_size;
|
|---|
| 959 | continue;
|
|---|
| 960 | }
|
|---|
| 961 | asf->packet_multi_size -= asf->packet_obj_size;
|
|---|
| 962 | //printf("COMPRESS size %d %d %d ms:%d\n", asf->packet_obj_size, asf->packet_frag_timestamp, asf->packet_size_left, asf->packet_multi_size);
|
|---|
| 963 | }
|
|---|
| 964 | if( /*asf->packet_frag_size == asf->packet_obj_size*/
|
|---|
| 965 | asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
|
|---|
| 966 | && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
|
|---|
| 967 | av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
|
|---|
| 968 | asf_st->frag_offset, asf->packet_frag_size,
|
|---|
| 969 | asf->packet_obj_size, asf_st->pkt.size);
|
|---|
| 970 | asf->packet_obj_size= asf_st->pkt.size;
|
|---|
| 971 | }
|
|---|
| 972 |
|
|---|
| 973 | if ( asf_st->pkt.size != asf->packet_obj_size
|
|---|
| 974 | || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
|
|---|
| 975 | if(asf_st->pkt.data){
|
|---|
| 976 | av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
|
|---|
| 977 | asf_st->frag_offset = 0;
|
|---|
| 978 | av_free_packet(&asf_st->pkt);
|
|---|
| 979 | }
|
|---|
| 980 | /* new packet */
|
|---|
| 981 | av_new_packet(&asf_st->pkt, asf->packet_obj_size);
|
|---|
| 982 | asf_st->seq = asf->packet_seq;
|
|---|
| 983 | asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
|
|---|
| 984 | asf_st->pkt.stream_index = asf->stream_index;
|
|---|
| 985 | asf_st->pkt.pos =
|
|---|
| 986 | asf_st->packet_pos= asf->packet_pos;
|
|---|
| 987 | if (asf_st->pkt.data && asf_st->palette_changed) {
|
|---|
| 988 | uint8_t *pal;
|
|---|
| 989 | pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
|
|---|
| 990 | AVPALETTE_SIZE);
|
|---|
| 991 | if (!pal) {
|
|---|
| 992 | av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
|
|---|
| 993 | } else {
|
|---|
| 994 | memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
|
|---|
| 995 | asf_st->palette_changed = 0;
|
|---|
| 996 | }
|
|---|
| 997 | }
|
|---|
| 998 | //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
|
|---|
| 999 | //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & AV_PKT_FLAG_KEY,
|
|---|
| 1000 | //s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO, asf->packet_obj_size);
|
|---|
| 1001 | if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
|
|---|
| 1002 | asf->packet_key_frame = 1;
|
|---|
| 1003 | if (asf->packet_key_frame)
|
|---|
| 1004 | asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
|
|---|
| 1005 | }
|
|---|
| 1006 |
|
|---|
| 1007 | /* read data */
|
|---|
| 1008 | //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
|
|---|
| 1009 | // s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
|
|---|
| 1010 | // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
|
|---|
| 1011 | asf->packet_size_left -= asf->packet_frag_size;
|
|---|
| 1012 | if (asf->packet_size_left < 0)
|
|---|
| 1013 | continue;
|
|---|
| 1014 |
|
|---|
| 1015 | if( asf->packet_frag_offset >= asf_st->pkt.size
|
|---|
| 1016 | || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
|
|---|
| 1017 | av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
|
|---|
| 1018 | asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
|
|---|
| 1019 | continue;
|
|---|
| 1020 | }
|
|---|
| 1021 |
|
|---|
| 1022 | ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
|
|---|
| 1023 | asf->packet_frag_size);
|
|---|
| 1024 | if (ret != asf->packet_frag_size) {
|
|---|
| 1025 | if (ret < 0 || asf->packet_frag_offset + ret == 0)
|
|---|
| 1026 | return ret < 0 ? ret : AVERROR_EOF;
|
|---|
| 1027 | if (asf_st->ds_span > 1) {
|
|---|
| 1028 | // scrambling, we can either drop it completely or fill the remainder
|
|---|
| 1029 | // TODO: should we fill the whole packet instead of just the current
|
|---|
| 1030 | // fragment?
|
|---|
| 1031 | memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
|
|---|
| 1032 | asf->packet_frag_size - ret);
|
|---|
| 1033 | ret = asf->packet_frag_size;
|
|---|
| 1034 | } else
|
|---|
| 1035 | // no scrambling, so we can return partial packets
|
|---|
| 1036 | av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
|
|---|
| 1037 | }
|
|---|
| 1038 | if (s->key && s->keylen == 20)
|
|---|
| 1039 | ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
|
|---|
| 1040 | ret);
|
|---|
| 1041 | asf_st->frag_offset += ret;
|
|---|
| 1042 | /* test if whole packet is read */
|
|---|
| 1043 | if (asf_st->frag_offset == asf_st->pkt.size) {
|
|---|
| 1044 | //workaround for macroshit radio DVR-MS files
|
|---|
| 1045 | if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
|
|---|
| 1046 | && asf_st->pkt.size > 100){
|
|---|
| 1047 | int i;
|
|---|
| 1048 | for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
|
|---|
| 1049 | if(i == asf_st->pkt.size){
|
|---|
| 1050 | av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
|
|---|
| 1051 | asf_st->frag_offset = 0;
|
|---|
| 1052 | av_free_packet(&asf_st->pkt);
|
|---|
| 1053 | continue;
|
|---|
| 1054 | }
|
|---|
| 1055 | }
|
|---|
| 1056 |
|
|---|
| 1057 | /* return packet */
|
|---|
| 1058 | if (asf_st->ds_span > 1) {
|
|---|
| 1059 | if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
|
|---|
| 1060 | av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * ds_span (%d %d %d)\n", asf_st->pkt.size, asf_st->ds_packet_size, asf_st->ds_span);
|
|---|
| 1061 | }else{
|
|---|
| 1062 | /* packet descrambling */
|
|---|
| 1063 | uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|---|
| 1064 | if (newdata) {
|
|---|
| 1065 | int offset = 0;
|
|---|
| 1066 | memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
|
|---|
| 1067 | while (offset < asf_st->pkt.size) {
|
|---|
| 1068 | int off = offset / asf_st->ds_chunk_size;
|
|---|
| 1069 | int row = off / asf_st->ds_span;
|
|---|
| 1070 | int col = off % asf_st->ds_span;
|
|---|
| 1071 | int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
|
|---|
| 1072 | //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
|
|---|
| 1073 |
|
|---|
| 1074 | assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
|
|---|
| 1075 | assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
|
|---|
| 1076 | memcpy(newdata + offset,
|
|---|
| 1077 | asf_st->pkt.data + idx * asf_st->ds_chunk_size,
|
|---|
| 1078 | asf_st->ds_chunk_size);
|
|---|
| 1079 | offset += asf_st->ds_chunk_size;
|
|---|
| 1080 | }
|
|---|
| 1081 | av_free(asf_st->pkt.data);
|
|---|
| 1082 | asf_st->pkt.data = newdata;
|
|---|
| 1083 | }
|
|---|
| 1084 | }
|
|---|
| 1085 | }
|
|---|
| 1086 | asf_st->frag_offset = 0;
|
|---|
| 1087 | *pkt= asf_st->pkt;
|
|---|
| 1088 | //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
|
|---|
| 1089 | asf_st->pkt.size = 0;
|
|---|
| 1090 | asf_st->pkt.data = 0;
|
|---|
| 1091 | break; // packet completed
|
|---|
| 1092 | }
|
|---|
| 1093 | }
|
|---|
| 1094 | return 0;
|
|---|
| 1095 | }
|
|---|
| 1096 |
|
|---|
| 1097 | static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|---|
| 1098 | {
|
|---|
| 1099 | ASFContext *asf = s->priv_data;
|
|---|
| 1100 |
|
|---|
| 1101 | for (;;) {
|
|---|
| 1102 | int ret;
|
|---|
| 1103 |
|
|---|
| 1104 | /* parse cached packets, if any */
|
|---|
| 1105 | if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
|
|---|
| 1106 | return ret;
|
|---|
| 1107 | if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
|
|---|
| 1108 | assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
|
|---|
| 1109 | asf->packet_time_start = 0;
|
|---|
| 1110 | }
|
|---|
| 1111 | }
|
|---|
| 1112 |
|
|---|
| 1113 | // Added to support seeking after packets have been read
|
|---|
| 1114 | // If information is not reset, read_packet fails due to
|
|---|
| 1115 | // leftover information from previous reads
|
|---|
| 1116 | static void asf_reset_header(AVFormatContext *s)
|
|---|
| 1117 | {
|
|---|
| 1118 | ASFContext *asf = s->priv_data;
|
|---|
| 1119 | ASFStream *asf_st;
|
|---|
| 1120 | int i;
|
|---|
| 1121 |
|
|---|
| 1122 | asf->packet_size_left = 0;
|
|---|
| 1123 | asf->packet_segments = 0;
|
|---|
| 1124 | asf->packet_flags = 0;
|
|---|
| 1125 | asf->packet_property = 0;
|
|---|
| 1126 | asf->packet_timestamp = 0;
|
|---|
| 1127 | asf->packet_segsizetype = 0;
|
|---|
| 1128 | asf->packet_segments = 0;
|
|---|
| 1129 | asf->packet_seq = 0;
|
|---|
| 1130 | asf->packet_replic_size = 0;
|
|---|
| 1131 | asf->packet_key_frame = 0;
|
|---|
| 1132 | asf->packet_padsize = 0;
|
|---|
| 1133 | asf->packet_frag_offset = 0;
|
|---|
| 1134 | asf->packet_frag_size = 0;
|
|---|
| 1135 | asf->packet_frag_timestamp = 0;
|
|---|
| 1136 | asf->packet_multi_size = 0;
|
|---|
| 1137 | asf->packet_obj_size = 0;
|
|---|
| 1138 | asf->packet_time_delta = 0;
|
|---|
| 1139 | asf->packet_time_start = 0;
|
|---|
| 1140 |
|
|---|
| 1141 | for(i=0; i<s->nb_streams; i++){
|
|---|
| 1142 | asf_st= s->streams[i]->priv_data;
|
|---|
| 1143 | av_free_packet(&asf_st->pkt);
|
|---|
| 1144 | asf_st->frag_offset=0;
|
|---|
| 1145 | asf_st->seq=0;
|
|---|
| 1146 | }
|
|---|
| 1147 | asf->asf_st= NULL;
|
|---|
| 1148 | }
|
|---|
| 1149 |
|
|---|
| 1150 | static int asf_read_close(AVFormatContext *s)
|
|---|
| 1151 | {
|
|---|
| 1152 | asf_reset_header(s);
|
|---|
| 1153 |
|
|---|
| 1154 | return 0;
|
|---|
| 1155 | }
|
|---|
| 1156 |
|
|---|
| 1157 | static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
|
|---|
| 1158 | {
|
|---|
| 1159 | AVPacket pkt1, *pkt = &pkt1;
|
|---|
| 1160 | ASFStream *asf_st;
|
|---|
| 1161 | int64_t pts;
|
|---|
| 1162 | int64_t pos= *ppos;
|
|---|
| 1163 | int i;
|
|---|
| 1164 | int64_t start_pos[ASF_MAX_STREAMS];
|
|---|
| 1165 |
|
|---|
| 1166 | for(i=0; i<s->nb_streams; i++){
|
|---|
| 1167 | start_pos[i]= pos;
|
|---|
| 1168 | }
|
|---|
| 1169 |
|
|---|
| 1170 | if (s->packet_size > 0)
|
|---|
| 1171 | pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
|
|---|
| 1172 | *ppos= pos;
|
|---|
| 1173 | if (avio_seek(s->pb, pos, SEEK_SET) < 0)
|
|---|
| 1174 | return AV_NOPTS_VALUE;
|
|---|
| 1175 |
|
|---|
| 1176 | //printf("asf_read_pts\n");
|
|---|
| 1177 | asf_reset_header(s);
|
|---|
| 1178 | for(;;){
|
|---|
| 1179 | if (av_read_frame(s, pkt) < 0){
|
|---|
| 1180 | av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
|
|---|
| 1181 | return AV_NOPTS_VALUE;
|
|---|
| 1182 | }
|
|---|
| 1183 |
|
|---|
| 1184 | pts= pkt->pts;
|
|---|
| 1185 |
|
|---|
| 1186 | av_free_packet(pkt);
|
|---|
| 1187 | if(pkt->flags&AV_PKT_FLAG_KEY){
|
|---|
| 1188 | i= pkt->stream_index;
|
|---|
| 1189 |
|
|---|
| 1190 | asf_st= s->streams[i]->priv_data;
|
|---|
| 1191 |
|
|---|
| 1192 | // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
|
|---|
| 1193 | pos= asf_st->packet_pos;
|
|---|
| 1194 |
|
|---|
| 1195 | av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
|
|---|
| 1196 | start_pos[i]= asf_st->packet_pos + 1;
|
|---|
| 1197 |
|
|---|
| 1198 | if(pkt->stream_index == stream_index)
|
|---|
| 1199 | break;
|
|---|
| 1200 | }
|
|---|
| 1201 | }
|
|---|
| 1202 |
|
|---|
| 1203 | *ppos= pos;
|
|---|
| 1204 | //printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
|
|---|
| 1205 |
|
|---|
| 1206 | return pts;
|
|---|
| 1207 | }
|
|---|
| 1208 |
|
|---|
| 1209 | static void asf_build_simple_index(AVFormatContext *s, int stream_index)
|
|---|
| 1210 | {
|
|---|
| 1211 | ff_asf_guid g;
|
|---|
| 1212 | ASFContext *asf = s->priv_data;
|
|---|
| 1213 | int64_t current_pos= avio_tell(s->pb);
|
|---|
| 1214 | int i;
|
|---|
| 1215 |
|
|---|
| 1216 | if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0) {
|
|---|
| 1217 | asf->index_read= -1;
|
|---|
| 1218 | return;
|
|---|
| 1219 | }
|
|---|
| 1220 |
|
|---|
| 1221 | ff_get_guid(s->pb, &g);
|
|---|
| 1222 |
|
|---|
| 1223 | /* the data object can be followed by other top-level objects,
|
|---|
| 1224 | skip them until the simple index object is reached */
|
|---|
| 1225 | while (ff_guidcmp(&g, &index_guid)) {
|
|---|
| 1226 | int64_t gsize= avio_rl64(s->pb);
|
|---|
| 1227 | if (gsize < 24 || url_feof(s->pb)) {
|
|---|
| 1228 | avio_seek(s->pb, current_pos, SEEK_SET);
|
|---|
| 1229 | asf->index_read= -1;
|
|---|
| 1230 | return;
|
|---|
| 1231 | }
|
|---|
| 1232 | avio_skip(s->pb, gsize-24);
|
|---|
| 1233 | ff_get_guid(s->pb, &g);
|
|---|
| 1234 | }
|
|---|
| 1235 |
|
|---|
| 1236 | {
|
|---|
| 1237 | int64_t itime, last_pos=-1;
|
|---|
| 1238 | int pct, ict;
|
|---|
| 1239 | int64_t av_unused gsize= avio_rl64(s->pb);
|
|---|
| 1240 | ff_get_guid(s->pb, &g);
|
|---|
| 1241 | itime=avio_rl64(s->pb);
|
|---|
| 1242 | pct=avio_rl32(s->pb);
|
|---|
| 1243 | ict=avio_rl32(s->pb);
|
|---|
| 1244 | av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
|
|---|
| 1245 |
|
|---|
| 1246 | for (i=0;i<ict;i++){
|
|---|
| 1247 | int pktnum=avio_rl32(s->pb);
|
|---|
| 1248 | int pktct =avio_rl16(s->pb);
|
|---|
| 1249 | int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
|
|---|
| 1250 | int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
|
|---|
| 1251 |
|
|---|
| 1252 | if(pos != last_pos){
|
|---|
| 1253 | av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n", pktnum, pktct, index_pts);
|
|---|
| 1254 | av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
|
|---|
| 1255 | last_pos=pos;
|
|---|
| 1256 | }
|
|---|
| 1257 | }
|
|---|
| 1258 | asf->index_read= 1;
|
|---|
| 1259 | }
|
|---|
| 1260 | avio_seek(s->pb, current_pos, SEEK_SET);
|
|---|
| 1261 | }
|
|---|
| 1262 |
|
|---|
| 1263 | static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
|
|---|
| 1264 | {
|
|---|
| 1265 | ASFContext *asf = s->priv_data;
|
|---|
| 1266 | AVStream *st = s->streams[stream_index];
|
|---|
| 1267 | int64_t pos;
|
|---|
| 1268 | int index;
|
|---|
| 1269 |
|
|---|
| 1270 | if (s->packet_size <= 0)
|
|---|
| 1271 | return -1;
|
|---|
| 1272 |
|
|---|
| 1273 | /* Try using the protocol's read_seek if available */
|
|---|
| 1274 | if(s->pb) {
|
|---|
| 1275 | int ret = avio_seek_time(s->pb, stream_index, pts, flags);
|
|---|
| 1276 | if(ret >= 0)
|
|---|
| 1277 | asf_reset_header(s);
|
|---|
| 1278 | if (ret != AVERROR(ENOSYS))
|
|---|
| 1279 | return ret;
|
|---|
| 1280 | }
|
|---|
| 1281 |
|
|---|
| 1282 | if (!asf->index_read)
|
|---|
| 1283 | asf_build_simple_index(s, stream_index);
|
|---|
| 1284 |
|
|---|
| 1285 | if((asf->index_read > 0 && st->index_entries)){
|
|---|
| 1286 | index= av_index_search_timestamp(st, pts, flags);
|
|---|
| 1287 | if(index >= 0) {
|
|---|
| 1288 | /* find the position */
|
|---|
| 1289 | pos = st->index_entries[index].pos;
|
|---|
| 1290 |
|
|---|
| 1291 | /* do the seek */
|
|---|
| 1292 | av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
|
|---|
| 1293 | if(avio_seek(s->pb, pos, SEEK_SET) < 0)
|
|---|
| 1294 | return -1;
|
|---|
| 1295 | asf_reset_header(s);
|
|---|
| 1296 | return 0;
|
|---|
| 1297 | }
|
|---|
| 1298 | }
|
|---|
| 1299 | /* no index or seeking by index failed */
|
|---|
| 1300 | if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
|
|---|
| 1301 | return -1;
|
|---|
| 1302 | asf_reset_header(s);
|
|---|
| 1303 | return 0;
|
|---|
| 1304 | }
|
|---|
| 1305 |
|
|---|
| 1306 | AVInputFormat ff_asf_demuxer = {
|
|---|
| 1307 | .name = "asf",
|
|---|
| 1308 | .long_name = NULL_IF_CONFIG_SMALL("ASF format"),
|
|---|
| 1309 | .priv_data_size = sizeof(ASFContext),
|
|---|
| 1310 | .read_probe = asf_probe,
|
|---|
| 1311 | .read_header = asf_read_header,
|
|---|
| 1312 | .read_packet = asf_read_packet,
|
|---|
| 1313 | .read_close = asf_read_close,
|
|---|
| 1314 | .read_seek = asf_read_seek,
|
|---|
| 1315 | .read_timestamp = asf_read_pts,
|
|---|
| 1316 | //.flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
|
|---|
| 1317 | };
|
|---|