?? mov.c
字號(hào):
dprintf("unknown compression for cmov atom !"); return -1; } get_be32(pb); /* cmvd atom */ if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' )) return -1; moov_len = get_be32(pb); /* uncompressed size */ cmov_len = atom.size - 6 * 4; cmov_data = (uint8_t *) av_malloc(cmov_len); if (!cmov_data) return -1; moov_data = (uint8_t *) av_malloc(moov_len); if (!moov_data) { av_free(cmov_data); return -1; } get_buffer(pb, cmov_data, cmov_len); if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK) return -1; if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0) return -1; ctx.buf_end = ctx.buffer + moov_len; atom.type = MKTAG( 'm', 'o', 'o', 'v' ); atom.offset = 0; atom.size = moov_len;#ifdef DEBUG { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }#endif ret = mov_read_default(c, &ctx, atom); av_free(moov_data); av_free(cmov_data); return ret;}#endifstatic const MOVParseTableEntry mov_default_parse_table[] = {/* mp4 atoms */{ MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },{ MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },{ MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },{ MKTAG( 'c', 't', 't', 's' ), mov_read_leaf }, /* composition time to sample */{ MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */{ MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },{ MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },{ MKTAG( 'e', 'd', 't', 's' ), mov_read_default },{ MKTAG( 'e', 'l', 's', 't' ), mov_read_leaf },{ MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },{ MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },{ MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },{ MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },{ MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },{ MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },{ MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },{ MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },{ MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },{ MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },{ MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },{ MKTAG( 'm', 'p', '4', 's' ), mov_read_default },{ MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },{ MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },{ MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },{ MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },{ MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },{ MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },{ MKTAG( 's', 'k', 'i', 'p' ), mov_read_default },{ MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */{ MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorrenson extension ??? */{ MKTAG( 's', 't', 'b', 'l' ), mov_read_default },{ MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },{ MKTAG( 's', 't', 'd', 'p' ), mov_read_default },{ MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },{ MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */{ MKTAG( 's', 't', 's', 'h' ), mov_read_default },{ MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */{ MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */{ MKTAG( 's', 't', 't', 's' ), mov_read_stts },{ MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */{ MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },{ MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */{ MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },{ MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },{ MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },{ MKTAG( 'u', 'u', 'i', 'd' ), mov_read_default },{ MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */{ MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },/* extra mp4 */{ MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },/* QT atoms */{ MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },{ MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },{ MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },{ MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },{ MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },{ MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },{ MKTAG( 'm', 'a', 't', 't' ), mov_read_default },{ MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },{ MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },{ MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },{ MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },{ MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },{ MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },{ MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },{ MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },{ MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder *///{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },#ifdef CONFIG_ZLIB{ MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },#else{ MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },#endif{ 0L, mov_read_leaf }};static void mov_free_stream_context(MOVStreamContext *sc){ if(sc) { av_free(sc->chunk_offsets); av_free(sc->sample_to_chunk); av_free(sc->sample_sizes); av_free(sc->keyframes); av_free(sc->header_data); av_free(sc); }}static inline uint32_t mov_to_tag(uint8_t *buf){ return MKTAG(buf[0], buf[1], buf[2], buf[3]);}static inline uint32_t to_be32(uint8_t *buf){ return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];}/* XXX: is it sufficient ? */static int mov_probe(AVProbeData *p){ unsigned int offset; uint32_t tag; /* check file header */ if (p->buf_size <= 12) return 0; offset = 0; for(;;) { /* ignore invalid offset */ if ((offset + 8) > (unsigned int)p->buf_size) return 0; tag = mov_to_tag(p->buf + offset + 4); switch(tag) { case MKTAG( 'm', 'o', 'o', 'v' ): case MKTAG( 'w', 'i', 'd', 'e' ): case MKTAG( 'f', 'r', 'e', 'e' ): case MKTAG( 'm', 'd', 'a', 't' ): case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */ case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */ return AVPROBE_SCORE_MAX; case MKTAG( 'f', 't', 'y', 'p' ): case MKTAG( 's', 'k', 'i', 'p' ): offset = to_be32(p->buf+offset) + offset; break; default: /* unrecognized tag */ return 0; } } return 0;}static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap){ MOVContext *mov = (MOVContext *) s->priv_data; ByteIOContext *pb = &s->pb; int i, j, nb, err; MOV_atom_t atom = { 0, 0, 0 }; mov->fc = s; mov->parse_table = mov_default_parse_table;#if 0 /* XXX: I think we should auto detect */ if(s->iformat->name[1] == 'p') mov->mp4 = 1;#endif if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */ atom.size = url_filesize(url_fileno(pb)); else atom.size = 0x7FFFFFFFFFFFFFFFLL;#ifdef DEBUG printf("filesz=%Ld\n", atom.size);#endif /* check MOV header */ err = mov_read_default(mov, pb, atom); if (err<0 || (!mov->found_moov && !mov->found_mdat)) { av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%lld\n", err, mov->found_moov, mov->found_mdat, url_ftell(pb)); return -1; }#ifdef DEBUG printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));#endif /* some cleanup : make sure we are on the mdat atom */ if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset)) url_fseek(pb, mov->mdat_offset, SEEK_SET); mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */#ifdef DEBUG printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));#endif#ifdef DEBUG printf("streams= %d\n", s->nb_streams);#endif mov->total_streams = nb = s->nb_streams;#if 1 for(i=0; i<s->nb_streams;) { if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */ av_free(s->streams[i]); for(j=i+1; j<s->nb_streams; j++) s->streams[j-1] = s->streams[j]; s->nb_streams--; } else i++; } for(i=0; i<s->nb_streams;i++) { MOVStreamContext *sc; sc = (MOVStreamContext *)s->streams[i]->priv_data; sc->ffindex = i; sc->is_ff_stream = 1; }#endif#ifdef DEBUG printf("real streams= %d\n", s->nb_streams);#endif return 0;}/* Yes, this is ugly... I didn't write the specs of QT :p *//* XXX:remove useless commented code sometime */static int mov_read_packet(AVFormatContext *s, AVPacket *pkt){ MOVContext *mov = (MOVContext *) s->priv_data; MOVStreamContext *sc; int64_t offset = 0x0FFFFFFFFFFFFFFFLL; int i, a, b, m; int size; size = 0x0FFFFFFF;#ifdef MOV_SPLIT_CHUNKS if (mov->partial) { int idx; sc = mov->partial; idx = sc->sample_to_chunk_index; if (idx < 0) return 0; size = sc->sample_sizes[sc->current_sample]; sc->current_sample++; sc->left_in_chunk--; if (sc->left_in_chunk <= 0) mov->partial = 0; offset = mov->next_chunk_offset; /* extract the sample */ goto readchunk; }#endifagain: sc = 0; for(i=0; i<mov->total_streams; i++) { MOVStreamContext *msc = mov->streams[i]; //printf("MOCHUNK %ld %d %p pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb)); if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0 && (msc->chunk_offsets[msc->next_chunk] < offset)) { sc = msc; offset = msc->chunk_offsets[msc->next_chunk]; //printf("SELETED %Ld i:%d\n", offset, i); } } if (!sc || offset==0x0FFFFFFFFFFFFFFFLL) return -1; sc->next_chunk++; if(mov->next_chunk_offset < offset) { /* some meta data */ url_fskip(&s->pb, (offset - mov->next_chunk_offset)); mov->next_chunk_offset = offset; }//printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset); if(!sc->is_ff_stream) { url_fskip(&s->pb, (offset - mov->next_chunk_offset)); mov->next_chunk_offset = offset; offset = 0x0FFFFFFFFFFFFFFFLL; goto again; } /* now get the chunk size... */ for(i=0; i<mov->total_streams; i++) { MOVStreamContext *msc = mov->streams[i]; if ((msc->next_chunk < msc->chunk_count) && ((msc->chunk_offsets[msc->next_chunk] - offset) < size)) size = msc->chunk_offsets[msc->next_chunk] - offset; }#ifdef MOV_MINOLTA_FIX //Make sure that size is according to sample_size (Needed by .mov files //created on a Minolta Dimage Xi where audio chunks contains waste data in the end) //Maybe we should really not only check sc->sample_size, but also sc->sample_sizes //but I have no such movies if (sc->sample_size > 0) { int foundsize=0; for(i=0; i<(sc->sample_to_chunk_sz); i++) { if( (sc->sample_to_chunk[i].first)<=(sc->next_chunk) && (sc->sample_size>0) ) { // I can't figure out why for PCM audio sample_size is always 1 // (it should actually be channels*bits_per_second/8) but it is. AVCodecContext* cod = &s->streams[sc->ffindex]->codec; if (sc->sample_size == 1 && (cod->codec_id == CODEC_ID_PCM_S16BE || cod->codec_id == CODEC_ID_PCM_S16LE)) foundsize=(sc->sample_to_chunk[i].count*cod->channels*cod->bits_per_sample)/8; else foundsize=sc->sample_to_chunk[i].count*sc->sample_size; }#ifdef DEBUG /*printf("sample_to_chunk first=%ld count=%ld, id=%ld\n", sc->sample_to_chunk[i].first, sc->sample_to_chunk[i].count, sc->sample_to_chunk[i].id);*/#endif } if( (foundsize>0) && (foundsize<size) ) {#ifdef DEBUG /*printf("this size should actually be %d\n",foundsize);*/#endif size=foundsize; } }#endif //MOV_MINOLTA_FIX#ifdef MOV_SPLIT_CHUNKS /* split chunks into samples */ if (sc->sample_size == 0) { int idx = sc->sample_to_chunk_index; if ((idx + 1 < sc->sample_to_chunk_sz) && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first)) idx++; sc->sample_to_chunk_index = idx; if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) { mov->partial = sc; /* we'll have to get those samples before next chunk */ sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1; size = sc->sample_sizes[sc->current_sample]; } sc->current_sample++; }#endifreadchunk://printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size); if(size == 0x0FFFFFFF) size = mov->mdat_size + mov->mdat_offset - offset; if(size < 0) return -1; if(size == 0) return -1; url_fseek(&s->pb, offset, SEEK_SET); //printf("READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb)); if (sc->header_len > 0) { av_new_packet(pkt, size + sc->header_len); memcpy(pkt->data, sc->header_data, sc->header_len); get_buffer(&s->pb, pkt->data + sc->header_len, size); /* free header */ av_freep(&sc->header_data); sc->header_len = 0; } else { av_new_packet(pkt, size); get_buffer(&s->pb, pkt->data, pkt->size); } pkt->stream_index = sc->ffindex; // If the keyframes table exists, mark any samples that are in the table as key frames. // If no table exists, treat very sample as a key frame. if (sc->keyframes) { a = 0; b = sc->keyframe_count - 1; while (a < b) { m = (a + b + 1) >> 1; if (sc->keyframes[m] > sc->current_sample) { b = m - 1; } else { a = m; } } if (sc->keyframes[a] == sc->current_sample) pkt->flags |= PKT_FLAG_KEY; } else pkt->flags |= PKT_FLAG_KEY;#ifdef DEBUG/* printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size); for(i=0; i<8; i++) printf("%02x ", pkt->data[i]); for(i=0; i<8; i++) printf("%c ", (pkt->data[i]) & 0x7F); puts("");*/#endif mov->next_chunk_offset = offset + size; return 0;}static int mov_read_close(AVFormatContext *s){ int i; MOVContext *mov = (MOVContext *) s->priv_data; for(i=0; i<mov->total_streams; i++) mov_free_stream_context(mov->streams[i]); /* free color tabs */ for(i=0; i<mov->ctab_size; i++) av_freep(&mov->ctab[i]); av_freep(&mov->ctab); return 0;}static AVInputFormat mov_iformat = { "mov,mp4,m4a,3gp", "QuickTime/MPEG4 format", sizeof(MOVContext), mov_probe, mov_read_header, mov_read_packet, mov_read_close,};int mov_init(void){ av_register_input_format(&mov_iformat); return 0;}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -