Libav
|
00001 /* 00002 * NSV demuxer 00003 * Copyright (c) 2004 The FFmpeg Project 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 #include "avformat.h" 00022 #include "riff.h" 00023 00024 //#define DEBUG 00025 //#define DEBUG_DUMP_INDEX // XXX dumbdriving-271.nsv breaks with it commented!! 00026 //#define DEBUG_SEEK 00027 #define CHECK_SUBSEQUENT_NSVS 00028 //#define DISABLE_AUDIO 00029 00030 /* max bytes to crawl for trying to resync 00031 * stupid streaming servers don't start at chunk boundaries... 00032 */ 00033 #define NSV_MAX_RESYNC (500*1024) 00034 #define NSV_MAX_RESYNC_TRIES 300 00035 00036 /* 00037 * First version by Francois Revol - revol@free.fr 00038 * References: 00039 * (1) http://www.multimedia.cx/nsv-format.txt 00040 * seems someone came to the same conclusions as me, and updated it: 00041 * (2) http://www.stud.ktu.lt/~vitslav/nsv/nsv-format.txt 00042 * http://www.stud.ktu.lt/~vitslav/nsv/ 00043 * official docs 00044 * (3) http://ultravox.aol.com/NSVFormat.rtf 00045 * Sample files: 00046 * (S1) http://www.nullsoft.com/nsv/samples/ 00047 * http://www.nullsoft.com/nsv/samples/faster.nsv 00048 * http://streamripper.sourceforge.net/openbb/read.php?TID=492&page=4 00049 */ 00050 00051 /* 00052 * notes on the header (Francois Revol): 00053 * 00054 * It is followed by strings, then a table, but nothing tells 00055 * where the table begins according to (1). After checking faster.nsv, 00056 * I believe NVSf[16-19] gives the size of the strings data 00057 * (that is the offset of the data table after the header). 00058 * After checking all samples from (S1) all confirms this. 00059 * 00060 * Then, about NSVf[12-15], faster.nsf has 179700. When veiwing it in VLC, 00061 * I noticed there was about 1 NVSs chunk/s, so I ran 00062 * strings faster.nsv | grep NSVs | wc -l 00063 * which gave me 180. That leads me to think that NSVf[12-15] might be the 00064 * file length in milliseconds. 00065 * Let's try that: 00066 * for f in *.nsv; do HTIME="$(od -t x4 "$f" | head -1 | sed 's/.* //')"; echo "'$f' $((0x$HTIME))s = $((0x$HTIME/1000/60)):$((0x$HTIME/1000%60))"; done 00067 * except for nstrailer (which doesn't have an NSVf header), it repports correct time. 00068 * 00069 * nsvtrailer.nsv (S1) does not have any NSVf header, only NSVs chunks, 00070 * so the header seems to not be mandatory. (for streaming). 00071 * 00072 * index slice duration check (excepts nsvtrailer.nsv): 00073 * for f in [^n]*.nsv; do DUR="$(ffmpeg -i "$f" 2>/dev/null | grep 'NSVf duration' | cut -d ' ' -f 4)"; IC="$(ffmpeg -i "$f" 2>/dev/null | grep 'INDEX ENTRIES' | cut -d ' ' -f 2)"; echo "duration $DUR, slite time $(($DUR/$IC))"; done 00074 */ 00075 00076 /* 00077 * TODO: 00078 * - handle timestamps !!! 00079 * - use index 00080 * - mime-type in probe() 00081 * - seek 00082 */ 00083 00084 #ifdef DEBUG 00085 #define PRINT(_v) printf _v 00086 #else 00087 #define PRINT(_v) 00088 #endif 00089 00090 #if 0 00091 struct NSVf_header { 00092 uint32_t chunk_tag; /* 'NSVf' */ 00093 uint32_t chunk_size; 00094 uint32_t file_size; /* max 4GB ??? no one learns anything it seems :^) */ 00095 uint32_t file_length; //unknown1; /* what about MSB of file_size ? */ 00096 uint32_t info_strings_size; /* size of the info strings */ //unknown2; 00097 uint32_t table_entries; 00098 uint32_t table_entries_used; /* the left ones should be -1 */ 00099 }; 00100 00101 struct NSVs_header { 00102 uint32_t chunk_tag; /* 'NSVs' */ 00103 uint32_t v4cc; /* or 'NONE' */ 00104 uint32_t a4cc; /* or 'NONE' */ 00105 uint16_t vwidth; /* assert(vwidth%16==0) */ 00106 uint16_t vheight; /* assert(vheight%16==0) */ 00107 uint8_t framerate; /* value = (framerate&0x80)?frtable[frameratex0x7f]:framerate */ 00108 uint16_t unknown; 00109 }; 00110 00111 struct nsv_avchunk_header { 00112 uint8_t vchunk_size_lsb; 00113 uint16_t vchunk_size_msb; /* value = (vchunk_size_msb << 4) | (vchunk_size_lsb >> 4) */ 00114 uint16_t achunk_size; 00115 }; 00116 00117 struct nsv_pcm_header { 00118 uint8_t bits_per_sample; 00119 uint8_t channel_count; 00120 uint16_t sample_rate; 00121 }; 00122 #endif 00123 00124 /* variation from avi.h */ 00125 /*typedef struct CodecTag { 00126 int id; 00127 unsigned int tag; 00128 } CodecTag;*/ 00129 00130 /* tags */ 00131 00132 #define T_NSVF MKTAG('N', 'S', 'V', 'f') /* file header */ 00133 #define T_NSVS MKTAG('N', 'S', 'V', 's') /* chunk header */ 00134 #define T_TOC2 MKTAG('T', 'O', 'C', '2') /* extra index marker */ 00135 #define T_NONE MKTAG('N', 'O', 'N', 'E') /* null a/v 4CC */ 00136 #define T_SUBT MKTAG('S', 'U', 'B', 'T') /* subtitle aux data */ 00137 #define T_ASYN MKTAG('A', 'S', 'Y', 'N') /* async a/v aux marker */ 00138 #define T_KEYF MKTAG('K', 'E', 'Y', 'F') /* video keyframe aux marker (addition) */ 00139 00140 #define TB_NSVF MKBETAG('N', 'S', 'V', 'f') 00141 #define TB_NSVS MKBETAG('N', 'S', 'V', 's') 00142 00143 /* hardcoded stream indexes */ 00144 #define NSV_ST_VIDEO 0 00145 #define NSV_ST_AUDIO 1 00146 #define NSV_ST_SUBT 2 00147 00148 enum NSVStatus { 00149 NSV_UNSYNC, 00150 NSV_FOUND_NSVF, 00151 NSV_HAS_READ_NSVF, 00152 NSV_FOUND_NSVS, 00153 NSV_HAS_READ_NSVS, 00154 NSV_FOUND_BEEF, 00155 NSV_GOT_VIDEO, 00156 NSV_GOT_AUDIO, 00157 }; 00158 00159 typedef struct NSVStream { 00160 int frame_offset; /* current frame (video) or byte (audio) counter 00161 (used to compute the pts) */ 00162 int scale; 00163 int rate; 00164 int sample_size; /* audio only data */ 00165 int start; 00166 00167 int new_frame_offset; /* temporary storage (used during seek) */ 00168 int cum_len; /* temporary storage (used during seek) */ 00169 } NSVStream; 00170 00171 typedef struct { 00172 int base_offset; 00173 int NSVf_end; 00174 uint32_t *nsvs_file_offset; 00175 int index_entries; 00176 enum NSVStatus state; 00177 AVPacket ahead[2]; /* [v, a] if .data is !NULL there is something */ 00178 /* cached */ 00179 int64_t duration; 00180 uint32_t vtag, atag; 00181 uint16_t vwidth, vheight; 00182 int16_t avsync; 00183 AVRational framerate; 00184 uint32_t *nsvs_timestamps; 00185 //DVDemuxContext* dv_demux; 00186 } NSVContext; 00187 00188 static const AVCodecTag nsv_codec_video_tags[] = { 00189 { CODEC_ID_VP3, MKTAG('V', 'P', '3', ' ') }, 00190 { CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') }, 00191 { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, 00192 { CODEC_ID_VP5, MKTAG('V', 'P', '5', ' ') }, 00193 { CODEC_ID_VP5, MKTAG('V', 'P', '5', '0') }, 00194 { CODEC_ID_VP6, MKTAG('V', 'P', '6', ' ') }, 00195 { CODEC_ID_VP6, MKTAG('V', 'P', '6', '0') }, 00196 { CODEC_ID_VP6, MKTAG('V', 'P', '6', '1') }, 00197 { CODEC_ID_VP6, MKTAG('V', 'P', '6', '2') }, 00198 /* 00199 { CODEC_ID_VP4, MKTAG('V', 'P', '4', ' ') }, 00200 { CODEC_ID_VP4, MKTAG('V', 'P', '4', '0') }, 00201 */ 00202 { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') }, /* cf sample xvid decoder from nsv_codec_sdk.zip */ 00203 { CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', '3') }, 00204 { CODEC_ID_NONE, 0 }, 00205 }; 00206 00207 static const AVCodecTag nsv_codec_audio_tags[] = { 00208 { CODEC_ID_MP3, MKTAG('M', 'P', '3', ' ') }, 00209 { CODEC_ID_AAC, MKTAG('A', 'A', 'C', ' ') }, 00210 { CODEC_ID_AAC, MKTAG('A', 'A', 'C', 'P') }, 00211 { CODEC_ID_SPEEX, MKTAG('S', 'P', 'X', ' ') }, 00212 { CODEC_ID_PCM_U16LE, MKTAG('P', 'C', 'M', ' ') }, 00213 { CODEC_ID_NONE, 0 }, 00214 }; 00215 00216 //static int nsv_load_index(AVFormatContext *s); 00217 static int nsv_read_chunk(AVFormatContext *s, int fill_header); 00218 00219 #ifdef DEBUG 00220 static void print_tag(const char *str, unsigned int tag, int size) 00221 { 00222 printf("%s: tag=%c%c%c%c\n", 00223 str, tag & 0xff, 00224 (tag >> 8) & 0xff, 00225 (tag >> 16) & 0xff, 00226 (tag >> 24) & 0xff); 00227 } 00228 #endif 00229 00230 /* try to find something we recognize, and set the state accordingly */ 00231 static int nsv_resync(AVFormatContext *s) 00232 { 00233 NSVContext *nsv = s->priv_data; 00234 ByteIOContext *pb = s->pb; 00235 uint32_t v = 0; 00236 int i; 00237 00238 PRINT(("%s(), offset = %"PRId64", state = %d\n", __FUNCTION__, url_ftell(pb), nsv->state)); 00239 00240 //nsv->state = NSV_UNSYNC; 00241 00242 for (i = 0; i < NSV_MAX_RESYNC; i++) { 00243 if (url_feof(pb)) { 00244 PRINT(("NSV EOF\n")); 00245 nsv->state = NSV_UNSYNC; 00246 return -1; 00247 } 00248 v <<= 8; 00249 v |= get_byte(pb); 00250 /* 00251 if (i < 8) { 00252 PRINT(("NSV resync: [%d] = %02x\n", i, v & 0x0FF)); 00253 } 00254 */ 00255 00256 if ((v & 0x0000ffff) == 0xefbe) { /* BEEF */ 00257 PRINT(("NSV resynced on BEEF after %d bytes\n", i+1)); 00258 nsv->state = NSV_FOUND_BEEF; 00259 return 0; 00260 } 00261 /* we read as big endian, thus the MK*BE* */ 00262 if (v == TB_NSVF) { /* NSVf */ 00263 PRINT(("NSV resynced on NSVf after %d bytes\n", i+1)); 00264 nsv->state = NSV_FOUND_NSVF; 00265 return 0; 00266 } 00267 if (v == MKBETAG('N', 'S', 'V', 's')) { /* NSVs */ 00268 PRINT(("NSV resynced on NSVs after %d bytes\n", i+1)); 00269 nsv->state = NSV_FOUND_NSVS; 00270 return 0; 00271 } 00272 00273 } 00274 PRINT(("NSV sync lost\n")); 00275 return -1; 00276 } 00277 00278 static int nsv_parse_NSVf_header(AVFormatContext *s, AVFormatParameters *ap) 00279 { 00280 NSVContext *nsv = s->priv_data; 00281 ByteIOContext *pb = s->pb; 00282 unsigned int file_size, size; 00283 int64_t duration; 00284 int strings_size; 00285 int table_entries; 00286 int table_entries_used; 00287 00288 PRINT(("%s()\n", __FUNCTION__)); 00289 00290 nsv->state = NSV_UNSYNC; /* in case we fail */ 00291 00292 size = get_le32(pb); 00293 if (size < 28) 00294 return -1; 00295 nsv->NSVf_end = size; 00296 00297 //s->file_size = (uint32_t)get_le32(pb); 00298 file_size = (uint32_t)get_le32(pb); 00299 PRINT(("NSV NSVf chunk_size %u\n", size)); 00300 PRINT(("NSV NSVf file_size %u\n", file_size)); 00301 00302 nsv->duration = duration = get_le32(pb); /* in ms */ 00303 PRINT(("NSV NSVf duration %"PRId64" ms\n", duration)); 00304 // XXX: store it in AVStreams 00305 00306 strings_size = get_le32(pb); 00307 table_entries = get_le32(pb); 00308 table_entries_used = get_le32(pb); 00309 PRINT(("NSV NSVf info-strings size: %d, table entries: %d, bis %d\n", 00310 strings_size, table_entries, table_entries_used)); 00311 if (url_feof(pb)) 00312 return -1; 00313 00314 PRINT(("NSV got header; filepos %"PRId64"\n", url_ftell(pb))); 00315 00316 if (strings_size > 0) { 00317 char *strings; /* last byte will be '\0' to play safe with str*() */ 00318 char *p, *endp; 00319 char *token, *value; 00320 char quote; 00321 00322 p = strings = av_mallocz(strings_size + 1); 00323 endp = strings + strings_size; 00324 get_buffer(pb, strings, strings_size); 00325 while (p < endp) { 00326 while (*p == ' ') 00327 p++; /* strip out spaces */ 00328 if (p >= endp-2) 00329 break; 00330 token = p; 00331 p = strchr(p, '='); 00332 if (!p || p >= endp-2) 00333 break; 00334 *p++ = '\0'; 00335 quote = *p++; 00336 value = p; 00337 p = strchr(p, quote); 00338 if (!p || p >= endp) 00339 break; 00340 *p++ = '\0'; 00341 PRINT(("NSV NSVf INFO: %s='%s'\n", token, value)); 00342 av_metadata_set2(&s->metadata, token, value, 0); 00343 } 00344 av_free(strings); 00345 } 00346 if (url_feof(pb)) 00347 return -1; 00348 00349 PRINT(("NSV got infos; filepos %"PRId64"\n", url_ftell(pb))); 00350 00351 if (table_entries_used > 0) { 00352 int i; 00353 nsv->index_entries = table_entries_used; 00354 if((unsigned)table_entries_used >= UINT_MAX / sizeof(uint32_t)) 00355 return -1; 00356 nsv->nsvs_file_offset = av_malloc((unsigned)table_entries_used * sizeof(uint32_t)); 00357 00358 for(i=0;i<table_entries_used;i++) 00359 nsv->nsvs_file_offset[i] = get_le32(pb) + size; 00360 00361 if(table_entries > table_entries_used && 00362 get_le32(pb) == MKTAG('T','O','C','2')) { 00363 nsv->nsvs_timestamps = av_malloc((unsigned)table_entries_used*sizeof(uint32_t)); 00364 for(i=0;i<table_entries_used;i++) { 00365 nsv->nsvs_timestamps[i] = get_le32(pb); 00366 } 00367 } 00368 } 00369 00370 PRINT(("NSV got index; filepos %"PRId64"\n", url_ftell(pb))); 00371 00372 #ifdef DEBUG_DUMP_INDEX 00373 #define V(v) ((v<0x20 || v > 127)?'.':v) 00374 /* dump index */ 00375 PRINT(("NSV %d INDEX ENTRIES:\n", table_entries)); 00376 PRINT(("NSV [dataoffset][fileoffset]\n", table_entries)); 00377 for (i = 0; i < table_entries; i++) { 00378 unsigned char b[8]; 00379 url_fseek(pb, size + nsv->nsvs_file_offset[i], SEEK_SET); 00380 get_buffer(pb, b, 8); 00381 PRINT(("NSV [0x%08lx][0x%08lx]: %02x %02x %02x %02x %02x %02x %02x %02x" 00382 "%c%c%c%c%c%c%c%c\n", 00383 nsv->nsvs_file_offset[i], size + nsv->nsvs_file_offset[i], 00384 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], 00385 V(b[0]), V(b[1]), V(b[2]), V(b[3]), V(b[4]), V(b[5]), V(b[6]), V(b[7]) )); 00386 } 00387 //url_fseek(pb, size, SEEK_SET); /* go back to end of header */ 00388 #undef V 00389 #endif 00390 00391 url_fseek(pb, nsv->base_offset + size, SEEK_SET); /* required for dumbdriving-271.nsv (2 extra bytes) */ 00392 00393 if (url_feof(pb)) 00394 return -1; 00395 nsv->state = NSV_HAS_READ_NSVF; 00396 return 0; 00397 } 00398 00399 static int nsv_parse_NSVs_header(AVFormatContext *s, AVFormatParameters *ap) 00400 { 00401 NSVContext *nsv = s->priv_data; 00402 ByteIOContext *pb = s->pb; 00403 uint32_t vtag, atag; 00404 uint16_t vwidth, vheight; 00405 AVRational framerate; 00406 int i; 00407 AVStream *st; 00408 NSVStream *nst; 00409 PRINT(("%s()\n", __FUNCTION__)); 00410 00411 vtag = get_le32(pb); 00412 atag = get_le32(pb); 00413 vwidth = get_le16(pb); 00414 vheight = get_le16(pb); 00415 i = get_byte(pb); 00416 00417 PRINT(("NSV NSVs framerate code %2x\n", i)); 00418 if(i&0x80) { /* odd way of giving native framerates from docs */ 00419 int t=(i & 0x7F)>>2; 00420 if(t<16) framerate = (AVRational){1, t+1}; 00421 else framerate = (AVRational){t-15, 1}; 00422 00423 if(i&1){ 00424 framerate.num *= 1000; 00425 framerate.den *= 1001; 00426 } 00427 00428 if((i&3)==3) framerate.num *= 24; 00429 else if((i&3)==2) framerate.num *= 25; 00430 else framerate.num *= 30; 00431 } 00432 else 00433 framerate= (AVRational){i, 1}; 00434 00435 nsv->avsync = get_le16(pb); 00436 nsv->framerate = framerate; 00437 #ifdef DEBUG 00438 print_tag("NSV NSVs vtag", vtag, 0); 00439 print_tag("NSV NSVs atag", atag, 0); 00440 PRINT(("NSV NSVs vsize %dx%d\n", vwidth, vheight)); 00441 #endif 00442 00443 /* XXX change to ap != NULL ? */ 00444 if (s->nb_streams == 0) { /* streams not yet published, let's do that */ 00445 nsv->vtag = vtag; 00446 nsv->atag = atag; 00447 nsv->vwidth = vwidth; 00448 nsv->vheight = vwidth; 00449 if (vtag != T_NONE) { 00450 int i; 00451 st = av_new_stream(s, NSV_ST_VIDEO); 00452 if (!st) 00453 goto fail; 00454 00455 nst = av_mallocz(sizeof(NSVStream)); 00456 if (!nst) 00457 goto fail; 00458 st->priv_data = nst; 00459 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00460 st->codec->codec_tag = vtag; 00461 st->codec->codec_id = ff_codec_get_id(nsv_codec_video_tags, vtag); 00462 st->codec->width = vwidth; 00463 st->codec->height = vheight; 00464 st->codec->bits_per_coded_sample = 24; /* depth XXX */ 00465 00466 av_set_pts_info(st, 64, framerate.den, framerate.num); 00467 st->start_time = 0; 00468 st->duration = av_rescale(nsv->duration, framerate.num, 1000*framerate.den); 00469 00470 for(i=0;i<nsv->index_entries;i++) { 00471 if(nsv->nsvs_timestamps) { 00472 av_add_index_entry(st, nsv->nsvs_file_offset[i], nsv->nsvs_timestamps[i], 00473 0, 0, AVINDEX_KEYFRAME); 00474 } else { 00475 int64_t ts = av_rescale(i*nsv->duration/nsv->index_entries, framerate.num, 1000*framerate.den); 00476 av_add_index_entry(st, nsv->nsvs_file_offset[i], ts, 0, 0, AVINDEX_KEYFRAME); 00477 } 00478 } 00479 } 00480 if (atag != T_NONE) { 00481 #ifndef DISABLE_AUDIO 00482 st = av_new_stream(s, NSV_ST_AUDIO); 00483 if (!st) 00484 goto fail; 00485 00486 nst = av_mallocz(sizeof(NSVStream)); 00487 if (!nst) 00488 goto fail; 00489 st->priv_data = nst; 00490 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00491 st->codec->codec_tag = atag; 00492 st->codec->codec_id = ff_codec_get_id(nsv_codec_audio_tags, atag); 00493 00494 st->need_parsing = AVSTREAM_PARSE_FULL; /* for PCM we will read a chunk later and put correct info */ 00495 00496 /* set timebase to common denominator of ms and framerate */ 00497 av_set_pts_info(st, 64, 1, framerate.num*1000); 00498 st->start_time = 0; 00499 st->duration = (int64_t)nsv->duration * framerate.num; 00500 #endif 00501 } 00502 #ifdef CHECK_SUBSEQUENT_NSVS 00503 } else { 00504 if (nsv->vtag != vtag || nsv->atag != atag || nsv->vwidth != vwidth || nsv->vheight != vwidth) { 00505 PRINT(("NSV NSVs header values differ from the first one!!!\n")); 00506 //return -1; 00507 } 00508 #endif /* CHECK_SUBSEQUENT_NSVS */ 00509 } 00510 00511 nsv->state = NSV_HAS_READ_NSVS; 00512 return 0; 00513 fail: 00514 /* XXX */ 00515 nsv->state = NSV_UNSYNC; 00516 return -1; 00517 } 00518 00519 static int nsv_read_header(AVFormatContext *s, AVFormatParameters *ap) 00520 { 00521 NSVContext *nsv = s->priv_data; 00522 int i, err; 00523 00524 PRINT(("%s()\n", __FUNCTION__)); 00525 PRINT(("filename '%s'\n", s->filename)); 00526 00527 nsv->state = NSV_UNSYNC; 00528 nsv->ahead[0].data = nsv->ahead[1].data = NULL; 00529 00530 for (i = 0; i < NSV_MAX_RESYNC_TRIES; i++) { 00531 if (nsv_resync(s) < 0) 00532 return -1; 00533 if (nsv->state == NSV_FOUND_NSVF) 00534 err = nsv_parse_NSVf_header(s, ap); 00535 /* we need the first NSVs also... */ 00536 if (nsv->state == NSV_FOUND_NSVS) { 00537 err = nsv_parse_NSVs_header(s, ap); 00538 break; /* we just want the first one */ 00539 } 00540 } 00541 if (s->nb_streams < 1) /* no luck so far */ 00542 return -1; 00543 /* now read the first chunk, so we can attempt to decode more info */ 00544 err = nsv_read_chunk(s, 1); 00545 00546 PRINT(("parsed header\n")); 00547 return 0; 00548 } 00549 00550 static int nsv_read_chunk(AVFormatContext *s, int fill_header) 00551 { 00552 NSVContext *nsv = s->priv_data; 00553 ByteIOContext *pb = s->pb; 00554 AVStream *st[2] = {NULL, NULL}; 00555 NSVStream *nst; 00556 AVPacket *pkt; 00557 int i, err = 0; 00558 uint8_t auxcount; /* number of aux metadata, also 4 bits of vsize */ 00559 uint32_t vsize; 00560 uint16_t asize; 00561 uint16_t auxsize; 00562 uint32_t auxtag; 00563 00564 PRINT(("%s(%d)\n", __FUNCTION__, fill_header)); 00565 00566 if (nsv->ahead[0].data || nsv->ahead[1].data) 00567 return 0; //-1; /* hey! eat what you've in your plate first! */ 00568 00569 null_chunk_retry: 00570 if (url_feof(pb)) 00571 return -1; 00572 00573 for (i = 0; i < NSV_MAX_RESYNC_TRIES && nsv->state < NSV_FOUND_NSVS && !err; i++) 00574 err = nsv_resync(s); 00575 if (err < 0) 00576 return err; 00577 if (nsv->state == NSV_FOUND_NSVS) 00578 err = nsv_parse_NSVs_header(s, NULL); 00579 if (err < 0) 00580 return err; 00581 if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF) 00582 return -1; 00583 00584 auxcount = get_byte(pb); 00585 vsize = get_le16(pb); 00586 asize = get_le16(pb); 00587 vsize = (vsize << 4) | (auxcount >> 4); 00588 auxcount &= 0x0f; 00589 PRINT(("NSV CHUNK %d aux, %u bytes video, %d bytes audio\n", auxcount, vsize, asize)); 00590 /* skip aux stuff */ 00591 for (i = 0; i < auxcount; i++) { 00592 auxsize = get_le16(pb); 00593 auxtag = get_le32(pb); 00594 PRINT(("NSV aux data: '%c%c%c%c', %d bytes\n", 00595 (auxtag & 0x0ff), 00596 ((auxtag >> 8) & 0x0ff), 00597 ((auxtag >> 16) & 0x0ff), 00598 ((auxtag >> 24) & 0x0ff), 00599 auxsize)); 00600 url_fskip(pb, auxsize); 00601 vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); /* that's becoming braindead */ 00602 } 00603 00604 if (url_feof(pb)) 00605 return -1; 00606 if (!vsize && !asize) { 00607 nsv->state = NSV_UNSYNC; 00608 goto null_chunk_retry; 00609 } 00610 00611 /* map back streams to v,a */ 00612 if (s->streams[0]) 00613 st[s->streams[0]->id] = s->streams[0]; 00614 if (s->streams[1]) 00615 st[s->streams[1]->id] = s->streams[1]; 00616 00617 if (vsize/* && st[NSV_ST_VIDEO]*/) { 00618 nst = st[NSV_ST_VIDEO]->priv_data; 00619 pkt = &nsv->ahead[NSV_ST_VIDEO]; 00620 av_get_packet(pb, pkt, vsize); 00621 pkt->stream_index = st[NSV_ST_VIDEO]->index;//NSV_ST_VIDEO; 00622 pkt->dts = nst->frame_offset; 00623 pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */ 00624 /* 00625 for (i = 0; i < MIN(8, vsize); i++) 00626 PRINT(("NSV video: [%d] = %02x\n", i, pkt->data[i])); 00627 */ 00628 } 00629 if(st[NSV_ST_VIDEO]) 00630 ((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++; 00631 00632 if (asize/*st[NSV_ST_AUDIO]*/) { 00633 nst = st[NSV_ST_AUDIO]->priv_data; 00634 pkt = &nsv->ahead[NSV_ST_AUDIO]; 00635 /* read raw audio specific header on the first audio chunk... */ 00636 /* on ALL audio chunks ?? seems so! */ 00637 if (asize && st[NSV_ST_AUDIO]->codec->codec_tag == MKTAG('P', 'C', 'M', ' ')/* && fill_header*/) { 00638 uint8_t bps; 00639 uint8_t channels; 00640 uint16_t samplerate; 00641 bps = get_byte(pb); 00642 channels = get_byte(pb); 00643 samplerate = get_le16(pb); 00644 asize-=4; 00645 PRINT(("NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate)); 00646 if (fill_header) { 00647 st[NSV_ST_AUDIO]->need_parsing = AVSTREAM_PARSE_NONE; /* we know everything */ 00648 if (bps != 16) { 00649 PRINT(("NSV AUDIO bit/sample != 16 (%d)!!!\n", bps)); 00650 } 00651 bps /= channels; // ??? 00652 if (bps == 8) 00653 st[NSV_ST_AUDIO]->codec->codec_id = CODEC_ID_PCM_U8; 00654 samplerate /= 4;/* UGH ??? XXX */ 00655 channels = 1; 00656 st[NSV_ST_AUDIO]->codec->channels = channels; 00657 st[NSV_ST_AUDIO]->codec->sample_rate = samplerate; 00658 PRINT(("NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate)); 00659 } 00660 } 00661 av_get_packet(pb, pkt, asize); 00662 pkt->stream_index = st[NSV_ST_AUDIO]->index;//NSV_ST_AUDIO; 00663 pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */ 00664 if( nsv->state == NSV_HAS_READ_NSVS && st[NSV_ST_VIDEO] ) { 00665 /* on a nsvs frame we have new information on a/v sync */ 00666 pkt->dts = (((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset-1); 00667 pkt->dts *= (int64_t)1000 * nsv->framerate.den; 00668 pkt->dts += (int64_t)nsv->avsync * nsv->framerate.num; 00669 PRINT(("NSV AUDIO: sync:%d, dts:%"PRId64, nsv->avsync, pkt->dts)); 00670 } 00671 nst->frame_offset++; 00672 } 00673 00674 nsv->state = NSV_UNSYNC; 00675 return 0; 00676 } 00677 00678 00679 static int nsv_read_packet(AVFormatContext *s, AVPacket *pkt) 00680 { 00681 NSVContext *nsv = s->priv_data; 00682 int i, err = 0; 00683 00684 PRINT(("%s()\n", __FUNCTION__)); 00685 00686 /* in case we don't already have something to eat ... */ 00687 if (nsv->ahead[0].data == NULL && nsv->ahead[1].data == NULL) 00688 err = nsv_read_chunk(s, 0); 00689 if (err < 0) 00690 return err; 00691 00692 /* now pick one of the plates */ 00693 for (i = 0; i < 2; i++) { 00694 if (nsv->ahead[i].data) { 00695 PRINT(("%s: using cached packet[%d]\n", __FUNCTION__, i)); 00696 /* avoid the cost of new_packet + memcpy(->data) */ 00697 memcpy(pkt, &nsv->ahead[i], sizeof(AVPacket)); 00698 nsv->ahead[i].data = NULL; /* we ate that one */ 00699 return pkt->size; 00700 } 00701 } 00702 00703 /* this restaurant is not approvisionned :^] */ 00704 return -1; 00705 } 00706 00707 static int nsv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) 00708 { 00709 NSVContext *nsv = s->priv_data; 00710 AVStream *st = s->streams[stream_index]; 00711 NSVStream *nst = st->priv_data; 00712 int index; 00713 00714 index = av_index_search_timestamp(st, timestamp, flags); 00715 if(index < 0) 00716 return -1; 00717 00718 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET); 00719 nst->frame_offset = st->index_entries[index].timestamp; 00720 nsv->state = NSV_UNSYNC; 00721 return 0; 00722 } 00723 00724 static int nsv_read_close(AVFormatContext *s) 00725 { 00726 /* int i; */ 00727 NSVContext *nsv = s->priv_data; 00728 00729 av_freep(&nsv->nsvs_file_offset); 00730 av_freep(&nsv->nsvs_timestamps); 00731 if (nsv->ahead[0].data) 00732 av_free_packet(&nsv->ahead[0]); 00733 if (nsv->ahead[1].data) 00734 av_free_packet(&nsv->ahead[1]); 00735 00736 #if 0 00737 00738 for(i=0;i<s->nb_streams;i++) { 00739 AVStream *st = s->streams[i]; 00740 NSVStream *ast = st->priv_data; 00741 if(ast){ 00742 av_free(ast->index_entries); 00743 av_free(ast); 00744 } 00745 av_free(st->codec->palctrl); 00746 } 00747 00748 #endif 00749 return 0; 00750 } 00751 00752 static int nsv_probe(AVProbeData *p) 00753 { 00754 int i; 00755 // PRINT(("nsv_probe(), buf_size %d\n", p->buf_size)); 00756 /* check file header */ 00757 /* streamed files might not have any header */ 00758 if (p->buf[0] == 'N' && p->buf[1] == 'S' && 00759 p->buf[2] == 'V' && (p->buf[3] == 'f' || p->buf[3] == 's')) 00760 return AVPROBE_SCORE_MAX; 00761 /* XXX: do streamed files always start at chunk boundary ?? */ 00762 /* or do we need to search NSVs in the byte stream ? */ 00763 /* seems the servers don't bother starting clean chunks... */ 00764 /* sometimes even the first header is at 9KB or something :^) */ 00765 for (i = 1; i < p->buf_size - 3; i++) { 00766 if (p->buf[i+0] == 'N' && p->buf[i+1] == 'S' && 00767 p->buf[i+2] == 'V' && p->buf[i+3] == 's') 00768 return AVPROBE_SCORE_MAX-20; 00769 } 00770 /* so we'll have more luck on extension... */ 00771 if (av_match_ext(p->filename, "nsv")) 00772 return AVPROBE_SCORE_MAX/2; 00773 /* FIXME: add mime-type check */ 00774 return 0; 00775 } 00776 00777 AVInputFormat nsv_demuxer = { 00778 "nsv", 00779 NULL_IF_CONFIG_SMALL("Nullsoft Streaming Video"), 00780 sizeof(NSVContext), 00781 nsv_probe, 00782 nsv_read_header, 00783 nsv_read_packet, 00784 nsv_read_close, 00785 nsv_read_seek, 00786 };