static int init_vqf_audio_codec(sh_audio_t *sh_audio){ WAVEFORMATEX *in_fmt=sh_audio->wf; vqf_priv_t*priv=sh_audio->context; int ver; mp_msg(MSGT_DECAUDIO, MSGL_INFO, "======= Win32 (TWinVQ) AUDIO Codec init =======\n"); sh_audio->channels=in_fmt->nChannels; sh_audio->samplerate=in_fmt->nSamplesPerSec; sh_audio->sample_format=AF_FORMAT_S16_NE; // sh_audio->sample_format=AF_FORMAT_FLOAT_NE; sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/8; priv->o_wf.nChannels=in_fmt->nChannels; priv->o_wf.nSamplesPerSec=in_fmt->nSamplesPerSec; priv->o_wf.nBlockAlign=sh_audio->samplesize*in_fmt->nChannels; priv->o_wf.nAvgBytesPerSec=in_fmt->nBlockAlign*in_fmt->nChannels; priv->o_wf.wFormatTag=0x01; priv->o_wf.wBitsPerSample=in_fmt->wBitsPerSample; priv->o_wf.cbSize=0; if( mp_msg_test(MSGT_DECAUDIO,MSGL_V) ) { mp_msg(MSGT_DECAUDIO, MSGL_V, "Input format:\n"); print_wave_header(in_fmt, MSGL_V); mp_msg(MSGT_DECAUDIO, MSGL_V, "Output fmt:\n"); print_wave_header(&priv->o_wf, MSGL_V); } memcpy(&priv->hi,&in_fmt[1],sizeof(headerInfo)); if((ver=TvqInitialize(&priv->hi,&priv->index,0))){ const char *tvqe[]={ "No errors", "General error", "Wrong version", "Channel setting error", "Wrong coding mode", "Inner parameter setting error", "Wrong number of VQ pre-selection candidates, used only in encoder" }; mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Tvq initialization error: %s\n",ver>=0&&ver<7?tvqe[ver]:"Unknown"); return 0; } ver=TvqCheckVersion(priv->hi.ID); if(ver==TVQ_UNKNOWN_VERSION){ mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Tvq unknown version of stream\n" ); return 0; } TvqGetConfInfo(&priv->cf); TvqGetVectorInfo(priv->bits_0,priv->bits_1); priv->framesize=TvqGetFrameSize(); sh_audio->audio_in_minsize=priv->framesize*in_fmt->nChannels; sh_audio->a_in_buffer_size=4*sh_audio->audio_in_minsize; sh_audio->a_in_buffer=av_malloc(sh_audio->a_in_buffer_size); sh_audio->a_in_buffer_len=0; return 1; }
static int asf_init_audio_stream(demuxer_t *demuxer,struct asf_priv* asf, sh_audio_t* sh_audio, ASF_stream_header_t *streamh, int *ppos, uint8_t** buf, char *hdr, unsigned int hdr_len) { uint8_t *buffer = *buf; int pos = *ppos; sh_audio->wf=calloc((streamh->type_size<sizeof(*sh_audio->wf))?sizeof(*sh_audio->wf):streamh->type_size,1); memcpy(sh_audio->wf,buffer,streamh->type_size); le2me_WAVEFORMATEX(sh_audio->wf); if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf,MSGL_V); if(ASF_LOAD_GUID_PREFIX(streamh->concealment)==ASF_GUID_PREFIX_audio_conceal_interleave){ buffer = &hdr[pos]; pos += streamh->stream_size; if (pos > hdr_len) return 0; asf->scrambling_h=buffer[0]; asf->scrambling_w=(buffer[2]<<8)|buffer[1]; asf->scrambling_b=(buffer[4]<<8)|buffer[3]; if(asf->scrambling_b>0){ asf->scrambling_w/=asf->scrambling_b; } } else { asf->scrambling_b=asf->scrambling_h=asf->scrambling_w=1; } mp_msg(MSGT_HEADER,MSGL_V,"ASF: audio scrambling: %d x %d x %d\n",asf->scrambling_h,asf->scrambling_w,asf->scrambling_b); return 1; }
/*! * @brief プログラムのエントリポイント * * 使い方<br> * $ ./main [wavefile-name] N * @param [in] argc コマンドライン引数の数 * @param [in] argv コマンドライン引数 * @return 終了コード */ int main(int argc, char *argv[]) { unsigned int N; unsigned int k; double frq_rate; double *restrict f_real; double *restrict f_imag; WAVE_DATA *restrict wd; POINT *restrict points; RANGE range = {{0.0, 0.0}, {0.0, 0.0}}; if (argc != 3) { fprintf(stderr, "bad argument!\n"); return EXIT_FAILURE; } wd = read_wave_file(argv[1]); N = str2int(argv[2]); print_wave_header(wd); f_real = CALLOC(double, N); f_imag = CALLOC(double, N); points = CALLOC(POINT, N); if (f_real == NULL || f_imag == NULL || points == NULL) { fputs("メモリ確保エラーです\n", stderr); return EXIT_FAILURE; } auto_dft(f_real, f_imag, wd->s_data1, NULL, N); frq_rate = (double)wd->samples_per_sec / N; for (k = 0; k < N; k++) { points[k].x = frq_rate * k; points[k].y = sqrt(SQ(f_real[k]) + SQ(f_imag[k])); if (range.max.y < points[k].y) range.max.y = points[k].y; } range.max.x = (double)wd->samples_per_sec / 2; range.max.y = (range.max.y == 0.0 ? 1.0 : range.max.y * 1.2); // print_result(points, N); if (show_gplot(points, N, range) == -1) { fputs("gnuplotを起動できません\n", stderr); fputs("gnuplotにPATHが通っているか、確認してください\n", stderr); } FREE(&f_real); FREE(&f_imag); FREE(&points); free_wave_data(wd); return EXIT_SUCCESS; }
static int preinit(sh_audio_t *sh_audio) { HRESULT ret; WAVEFORMATEX *in_fmt = sh_audio->wf; DWORD srcsize = 0; acm_context_t *priv; priv = malloc(sizeof(acm_context_t)); if (!priv) return 0; sh_audio->context = priv; mp_msg(MSGT_WIN32, MSGL_V, "======= Win32 (ACM) AUDIO Codec init =======\n"); // priv->handle = NULL; priv->o_wf = malloc(sizeof(*priv->o_wf)); if (!priv->o_wf) { mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_ACMiniterror); return 0; } priv->o_wf->nChannels = in_fmt->nChannels; priv->o_wf->nSamplesPerSec = in_fmt->nSamplesPerSec; priv->o_wf->nAvgBytesPerSec = 2*in_fmt->nSamplesPerSec*in_fmt->nChannels; priv->o_wf->wFormatTag = WAVE_FORMAT_PCM; priv->o_wf->nBlockAlign = 2*in_fmt->nChannels; priv->o_wf->wBitsPerSample = 16; // priv->o_wf->wBitsPerSample = inf_fmt->wBitsPerSample; priv->o_wf->cbSize = 0; if ( mp_msg_test(MSGT_DECAUDIO,MSGL_V) ) { mp_msg(MSGT_DECAUDIO, MSGL_V, "Input format:\n"); print_wave_header(in_fmt, MSGL_V); mp_msg(MSGT_DECAUDIO, MSGL_V, "Output format:\n"); print_wave_header(priv->o_wf, MSGL_V); } MSACM_RegisterDriver((const char *)sh_audio->codec->dll, in_fmt->wFormatTag, 0); ret = acmStreamOpen(&priv->handle, (HACMDRIVER)NULL, in_fmt, priv->o_wf, NULL, 0, 0, 0); if (ret) { if (ret == ACMERR_NOTPOSSIBLE) mp_msg(MSGT_WIN32, MSGL_ERR, "ACM_Decoder: Unappropriate audio format\n"); else mp_msg(MSGT_WIN32, MSGL_ERR, "ACM_Decoder: acmStreamOpen error: %d\n", (int)ret); mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_ACMiniterror); return 0; } mp_msg(MSGT_WIN32, MSGL_V, "Audio codec opened OK! ;-)\n"); acmStreamSize(priv->handle, in_fmt->nBlockAlign, &srcsize, ACM_STREAMSIZEF_SOURCE); //if ( mp_msg_test(MSGT_DECAUDIO,MSGL_V) ) printf("Audio ACM output buffer min. size: %ld (reported by codec)\n", srcsize); srcsize *= 2; //if (srcsize < MAX_OUTBURST) srcsize = MAX_OUTBURST; if (!srcsize) { mp_msg(MSGT_WIN32, MSGL_WARN, "Warning! ACM codec reports srcsize=0\n"); srcsize = 16384; } // limit srcsize to 4-16kb //while(srcsize && srcsize<4096) srcsize*=2; //while(srcsize>16384) srcsize/=2; sh_audio->audio_out_minsize=srcsize; // audio output min. size mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM output buffer min. size: %ld\n",srcsize); acmStreamSize(priv->handle, srcsize, &srcsize, ACM_STREAMSIZEF_DESTINATION); // if(srcsize<in_fmt->nBlockAlign) srcsize=in_fmt->nBlockAlign; if (!srcsize) { mp_msg(MSGT_WIN32, MSGL_WARN, "Warning! ACM codec reports srcsize=0\n"); srcsize = 2*in_fmt->nBlockAlign; } mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM input buffer min. size: %ld\n",srcsize); sh_audio->audio_in_minsize=2*srcsize; // audio input min. size sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec; sh_audio->channels=priv->o_wf->nChannels; sh_audio->samplerate=priv->o_wf->nSamplesPerSec; sh_audio->samplesize=2; mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: Win32/ACM audio codec init OK!\n"); return 1; }
static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) { lavf_priv_t *priv= demuxer->priv; AVStream *st= avfc->streams[i]; AVCodecContext *codec= st->codec; char *stream_type = NULL; int stream_id; AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0); AVDictionaryEntry *title= av_dict_get(st->metadata, "title", NULL, 0); int g; switch(codec->codec_type){ case AVMEDIA_TYPE_AUDIO:{ WAVEFORMATEX *wf; sh_audio_t* sh_audio; sh_audio = new_sh_audio_aid(demuxer, i, priv->audio_streams, lang ? lang->value : NULL); if(!sh_audio) break; stream_type = "audio"; priv->astreams[priv->audio_streams] = i; wf= calloc(sizeof(*wf) + codec->extradata_size, 1); codec->codec_tag = mp_codec_id2tag(codec->codec_id, codec->codec_tag, 1); wf->wFormatTag= codec->codec_tag; wf->nChannels= codec->channels; wf->nSamplesPerSec= codec->sample_rate; wf->nAvgBytesPerSec= codec->bit_rate/8; wf->nBlockAlign= codec->block_align ? codec->block_align : 1; wf->wBitsPerSample= codec->bits_per_coded_sample; wf->cbSize= codec->extradata_size; if(codec->extradata_size) memcpy(wf + 1, codec->extradata, codec->extradata_size); sh_audio->wf= wf; sh_audio->audio.dwSampleSize= codec->block_align; if(codec->frame_size && codec->sample_rate){ sh_audio->audio.dwScale=codec->frame_size; sh_audio->audio.dwRate= codec->sample_rate; }else{ sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8; sh_audio->audio.dwRate = codec->bit_rate; } g= av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate); sh_audio->audio.dwScale /= g; sh_audio->audio.dwRate /= g; // printf("sca:%d rat:%d fs:%d sr:%d ba:%d\n", sh_audio->audio.dwScale, sh_audio->audio.dwRate, codec->frame_size, codec->sample_rate, codec->block_align); sh_audio->ds= demuxer->audio; sh_audio->format= codec->codec_tag; sh_audio->channels= codec->channels; sh_audio->samplerate= codec->sample_rate; sh_audio->i_bps= codec->bit_rate/8; switch (codec->codec_id) { case CODEC_ID_PCM_S8: case CODEC_ID_PCM_U8: sh_audio->samplesize = 1; break; case CODEC_ID_PCM_S16LE: case CODEC_ID_PCM_S16BE: case CODEC_ID_PCM_U16LE: case CODEC_ID_PCM_U16BE: sh_audio->samplesize = 2; break; case CODEC_ID_PCM_ALAW: sh_audio->format = 0x6; break; case CODEC_ID_PCM_MULAW: sh_audio->format = 0x7; break; } if (title && title->value) mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", priv->audio_streams, title->value); if (st->disposition & AV_DISPOSITION_DEFAULT) sh_audio->default_track = 1; if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V); // select the first audio stream if auto-selection is requested if (demuxer->audio->id == -1) { demuxer->audio->id = i; demuxer->audio->sh= demuxer->a_streams[i]; } if (demuxer->audio->id != i) st->discard= AVDISCARD_ALL; stream_id = priv->audio_streams++; break; } case AVMEDIA_TYPE_VIDEO:{ sh_video_t* sh_video; BITMAPINFOHEADER *bih; sh_video=new_sh_video_vid(demuxer, i, priv->video_streams); if(!sh_video) break; stream_type = "video"; priv->vstreams[priv->video_streams] = i; bih=calloc(sizeof(*bih) + codec->extradata_size,1); if(codec->codec_id == CODEC_ID_RAWVIDEO) { switch (codec->pix_fmt) { case PIX_FMT_RGB24: codec->codec_tag= MKTAG(24, 'B', 'G', 'R'); case PIX_FMT_BGR24: codec->codec_tag= MKTAG(24, 'R', 'G', 'B'); } } codec->codec_tag = mp_codec_id2tag(codec->codec_id, codec->codec_tag, 0); bih->biSize= sizeof(*bih) + codec->extradata_size; bih->biWidth= codec->width; bih->biHeight= codec->height; bih->biBitCount= codec->bits_per_coded_sample; bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8; bih->biCompression= codec->codec_tag; sh_video->bih= bih; sh_video->disp_w= codec->width; sh_video->disp_h= codec->height; if (st->time_base.den) { /* if container has time_base, use that */ sh_video->video.dwRate= st->time_base.den; sh_video->video.dwScale= st->time_base.num; } else { sh_video->video.dwRate= codec->time_base.den; sh_video->video.dwScale= codec->time_base.num; } sh_video->fps=av_q2d(st->r_frame_rate); sh_video->frametime=1/av_q2d(st->r_frame_rate); sh_video->format=bih->biCompression; if(st->sample_aspect_ratio.num) sh_video->aspect = codec->width * st->sample_aspect_ratio.num / (float)(codec->height * st->sample_aspect_ratio.den); else sh_video->aspect=codec->width * codec->sample_aspect_ratio.num / (float)(codec->height * codec->sample_aspect_ratio.den); sh_video->i_bps=codec->bit_rate/8; if (title && title->value) mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", priv->video_streams, title->value); mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n", codec->width, codec->sample_aspect_ratio.num, codec->height, codec->sample_aspect_ratio.den); sh_video->ds= demuxer->video; if(codec->extradata_size) memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size); if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V); /* short biPlanes; int biXPelsPerMeter; int biYPelsPerMeter; int biClrUsed; int biClrImportant; */ // select the first video stream if auto-selection is requested if(demuxer->video->id == -1) { demuxer->video->id = i; demuxer->video->sh= demuxer->v_streams[i]; } if(demuxer->video->id != i) st->discard= AVDISCARD_ALL; stream_id = priv->video_streams++; break; } case AVMEDIA_TYPE_SUBTITLE:{ sh_sub_t* sh_sub; char type; if(codec->codec_id == CODEC_ID_TEXT || codec->codec_id == AV_CODEC_ID_SUBRIP) type = 't'; else if(codec->codec_id == CODEC_ID_MOV_TEXT) type = 'm'; else if(codec->codec_id == CODEC_ID_SSA) type = 'a'; else if(codec->codec_id == CODEC_ID_DVD_SUBTITLE) type = 'v'; else if(codec->codec_id == CODEC_ID_XSUB) type = 'x'; else if(codec->codec_id == CODEC_ID_DVB_SUBTITLE) type = 'b'; else if(codec->codec_id == CODEC_ID_DVB_TELETEXT) type = 'd'; else if(codec->codec_id == CODEC_ID_HDMV_PGS_SUBTITLE) type = 'p'; #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 14, 100) else if(codec->codec_id == CODEC_ID_EIA_608) type = 'c'; #endif else if(codec->codec_tag == MKTAG('c', '6', '0', '8')) type = 'c'; else break; sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams, lang ? lang->value : NULL); if(!sh_sub) break; stream_type = "subtitle"; priv->sstreams[priv->sub_streams] = i; sh_sub->type = type; if (codec->extradata_size) { sh_sub->extradata = malloc(codec->extradata_size); memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size); sh_sub->extradata_len = codec->extradata_size; } if (title && title->value) mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", priv->sub_streams, title->value); if (st->disposition & AV_DISPOSITION_DEFAULT) sh_sub->default_track = 1; stream_id = priv->sub_streams++; break; } case AVMEDIA_TYPE_ATTACHMENT:{ if (st->codec->codec_id == CODEC_ID_TTF) { AVDictionaryEntry *fnametag = av_dict_get(st->metadata, "filename", NULL, 0); demuxer_add_attachment(demuxer, fnametag ? fnametag->value : NULL, "application/x-truetype-font", codec->extradata, codec->extradata_size); } break; } default: st->discard= AVDISCARD_ALL; } if (stream_type) { AVCodec *avc = avcodec_find_decoder(codec->codec_id); const char *codec_name = avc ? avc->name : "unknown"; if (!avc && *stream_type == 's' && demuxer->s_streams[i]) codec_name = sh_sub_type2str(((sh_sub_t *)demuxer->s_streams[i])->type); mp_msg(MSGT_DEMUX, MSGL_INFO, "[lavf] stream %d: %s (%s), -%cid %d", i, stream_type, codec_name, *stream_type, stream_id); if (lang && lang->value && *stream_type != 'v') mp_msg(MSGT_DEMUX, MSGL_INFO, ", -%clang %s", *stream_type, lang->value); if (title && title->value) mp_msg(MSGT_DEMUX, MSGL_INFO, ", %s", title->value); mp_msg(MSGT_DEMUX, MSGL_INFO, "\n"); } }
int main(int argc, char* argv[]){ FILE *f = fopen(argc > 1 ? argv[1] : "Alice Deejay - Back In My Life.asf", "rb"); if(!f){ printf("file not found\n"); exit(1); } //printf("sizeof=%d\n", sizeof(objh)); //printf("sizeof=%d\n", sizeof(asfh)); fread(&asfh, sizeof(asfh), 1, f); // header obj //for(i = 0; i < 16; i++) // printf("%02X ", asfh.objh.guid[i]); printf("[%s] %d (subchunks: %d)\n", chunk_type(asfh.objh.guid), (int) asfh.objh.size, asfh.cno); while(fread(&objh, sizeof(objh), 1, f) > 0){ int pos = ftell(f); //for(i = 0; i < 16; i++) // printf("%02X ", objh.guid[i]); printf("0x%08X [%s] %d\n", pos-sizeof(objh), chunk_type(objh.guid), (int) objh.size); switch(*((unsigned int*)&objh.guid)){ case 0xB7DC0791: // guid_stream_header fread(&streamh, sizeof(streamh), 1, f); printf("stream type: %s\n", chunk_type(streamh.type)); printf("stream concealment: %s\n", chunk_type(streamh.concealment)); printf("type: %d bytes, stream: %d bytes ID: %d\n", (int)streamh.type_size, (int)streamh.stream_size, (int)streamh.stream_no); printf("FILEPOS=0x%lX\n", ftell(f)); // type-specific data: fread(buffer,streamh.type_size,1,f); switch(*((unsigned int*)&streamh.type)){ case 0xF8699E40: // guid_audio_stream print_wave_header((WAVEFORMATEX*)buffer); break; case 0xBC19EFC0: // guid_video_stream print_video_header((BITMAPINFOHEADER*)&buffer[4 + 4 + 1 + 2]); break; } // stream-specific data: fread(buffer, streamh.stream_size, 1, f); break; //case 0xD6E229D1: // return "guid_header_2_0"; case 0x8CABDCA1: // guid_file_header fread(&fileh, sizeof(fileh), 1, f); printf("packets: %d flags: %d pack_size: %d frame_size: %d\n", (int)fileh.packets, (int)fileh.flags, (int)fileh.packetsize, (int)fileh.frame_size); break; case 0x75b22636: // guid_data_chunk { int endp = pos + objh.size - sizeof(objh); unsigned char* packet = malloc((int)fileh.packetsize); int fpos; fseek(f, 26, SEEK_CUR); while((fpos = ftell(f)) < endp){ fread(packet, (int)fileh.packetsize, 1, f); if(packet[0] == 0x82){ unsigned char flags = packet[3]; unsigned char* p = &packet[5]; unsigned long time; unsigned short duration; int segs = 1; int seg; int padding=0; if(flags & 8){ padding = p[0]; ++p; } else if(flags & 16){ padding = p[0] | (p[1] << 8); p += 2; } time = *((unsigned long*)p); p += 4; duration = *((unsigned short*)p); p += 2; if(flags & 1){ segs = p[0] - 0x80; ++p; } printf("%08X: flag=%02X segs=%d pad=%d time=%ld dur=%d\n", fpos, flags, segs, padding, time, duration); for(seg = 0; seg < segs; seg++){ ASF_segmhdr_t* sh = (ASF_segmhdr_t*)p; int len = 0; p += sizeof(ASF_segmhdr_t); if(sh->flag & 8) p+=8;// else if(sh->flag & 1) ++p; if(flags & 1){ len = *((unsigned short*)p); p += 2; } printf(" seg #%d: streamno=%d seq=%d flag=%02X len=%d\n", seg, sh->streamno&0x7F, sh->seq, sh->flag, len); #ifdef SAVE_STREAMS if(!streams[sh->streamno & 0x7F]){ char name[256]; snprintf(name, 256, "stream%02X.dat", sh->streamno & 0x7F); streams[sh->streamno & 0x7F] = fopen(name, "wb"); } fwrite(p, len, 1, streams[sh->streamno & 0x7F]); #endif p += len; } } else printf("%08X: UNKNOWN %02X %02X %02X %02X %02X...\n", fpos, packet[0], packet[1], packet[2], packet[3], packet[4]); } } break; //case 0x33000890: // return "guid_index_chunk"; } fseek(f, pos + objh.size - sizeof(objh), SEEK_SET); } fclose(f); return 0; }
static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) { lavf_priv_t *priv= demuxer->priv; AVStream *st= avfc->streams[i]; AVCodecContext *codec= st->codec; char *stream_type = NULL; int stream_id; AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL, 0); AVMetadataTag *title= av_metadata_get(st->metadata, "title", NULL, 0); int g, override_tag = av_codec_get_tag(mp_codecid_override_taglists, codec->codec_id); // For some formats (like PCM) always trust CODEC_ID_* more than codec_tag if (override_tag) codec->codec_tag = override_tag; switch(codec->codec_type){ case CODEC_TYPE_AUDIO:{ WAVEFORMATEX *wf; sh_audio_t* sh_audio; sh_audio = new_sh_audio_aid(demuxer, i, priv->audio_streams, lang ? lang->value : NULL); if(!sh_audio) break; stream_type = "audio"; priv->astreams[priv->audio_streams] = i; wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1); // mp4a tag is used for all mp4 files no matter what they actually contain if(codec->codec_tag == MKTAG('m', 'p', '4', 'a')) codec->codec_tag= 0; if(!codec->codec_tag) codec->codec_tag= av_codec_get_tag(mp_wav_taglists, codec->codec_id); wf->wFormatTag= codec->codec_tag; wf->nChannels= codec->channels; wf->nSamplesPerSec= codec->sample_rate; wf->nAvgBytesPerSec= codec->bit_rate/8; wf->nBlockAlign= codec->block_align ? codec->block_align : 1; wf->wBitsPerSample= codec->bits_per_coded_sample; wf->cbSize= codec->extradata_size; if(codec->extradata_size) memcpy(wf + 1, codec->extradata, codec->extradata_size); sh_audio->wf= wf; sh_audio->audio.dwSampleSize= codec->block_align; if(codec->frame_size && codec->sample_rate){ sh_audio->audio.dwScale=codec->frame_size; sh_audio->audio.dwRate= codec->sample_rate; }else{ sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8; sh_audio->audio.dwRate = codec->bit_rate; } g= av_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate); sh_audio->audio.dwScale /= g; sh_audio->audio.dwRate /= g; // printf("sca:%d rat:%d fs:%d sr:%d ba:%d\n", sh_audio->audio.dwScale, sh_audio->audio.dwRate, codec->frame_size, codec->sample_rate, codec->block_align); sh_audio->ds= demuxer->audio; sh_audio->format= codec->codec_tag; sh_audio->channels= codec->channels; sh_audio->samplerate= codec->sample_rate; /* in opencore : try_decode_frame() delete in libavformat/utils.c ,so some parame maybe not be get so if samplerate no get in header, then give a fix value 44100 */ if(!sh_audio->channels) sh_audio->channels = 2; if(!sh_audio->samplerate) sh_audio->samplerate = 44100; sh_audio->i_bps= codec->bit_rate/8; switch (codec->codec_id) { case CODEC_ID_PCM_S8: case CODEC_ID_PCM_U8: sh_audio->samplesize = 1; break; case CODEC_ID_PCM_S16LE: case CODEC_ID_PCM_S16BE: case CODEC_ID_PCM_U16LE: case CODEC_ID_PCM_U16BE: sh_audio->samplesize = 2; break; case CODEC_ID_PCM_ALAW: sh_audio->format = 0x6; break; case CODEC_ID_PCM_MULAW: sh_audio->format = 0x7; break; } if (title && title->value) mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", priv->audio_streams, title->value); if (st->disposition & AV_DISPOSITION_DEFAULT) sh_audio->default_track = 1; if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V); // select the first audio stream if (!demuxer->audio->sh) { demuxer->audio->id = i; demuxer->audio->sh= demuxer->a_streams[i]; } else st->discard= AVDISCARD_ALL; stream_id = priv->audio_streams++; break; } case CODEC_TYPE_VIDEO:{ sh_video_t* sh_video; BITMAPINFOHEADER *bih; sh_video=new_sh_video_vid(demuxer, i, priv->video_streams); if(!sh_video) break; stream_type = "video"; priv->vstreams[priv->video_streams] = i; bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1); if(codec->codec_id == CODEC_ID_RAWVIDEO) { switch (codec->pix_fmt) { case PIX_FMT_RGB24: codec->codec_tag= MKTAG(24, 'B', 'G', 'R'); } } if(!codec->codec_tag) codec->codec_tag= av_codec_get_tag(mp_bmp_taglists, codec->codec_id); bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size; if(!codec->width || !codec->height){ codec->width = 16*4; codec->height = 9*4; } bih->biWidth= codec->width; bih->biHeight= codec->height; bih->biBitCount= codec->bits_per_coded_sample; bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8; bih->biCompression= codec->codec_tag; sh_video->bih= bih; sh_video->disp_w= codec->width; sh_video->disp_h= codec->height; if (st->time_base.den) { /* if container has time_base, use that */ sh_video->video.dwRate= st->time_base.den; sh_video->video.dwScale= st->time_base.num; } else { sh_video->video.dwRate= codec->time_base.den; sh_video->video.dwScale= codec->time_base.num; } sh_video->fps=av_q2d(st->r_frame_rate); sh_video->frametime=1/av_q2d(st->r_frame_rate); sh_video->format=bih->biCompression; if(st->sample_aspect_ratio.num) sh_video->aspect = codec->width * st->sample_aspect_ratio.num / (float)(codec->height * st->sample_aspect_ratio.den); else sh_video->aspect=codec->width * codec->sample_aspect_ratio.num / (float)(codec->height * codec->sample_aspect_ratio.den); sh_video->rotation_degrees = st->rotation_degrees; sh_video->i_bps=codec->bit_rate/8; if (title && title->value) mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", priv->video_streams, title->value); mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n", codec->width, codec->sample_aspect_ratio.num, codec->height, codec->sample_aspect_ratio.den); sh_video->ds= demuxer->video; if(codec->extradata_size) memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size); if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V); /* short biPlanes; int biXPelsPerMeter; int biYPelsPerMeter; int biClrUsed; int biClrImportant; */ if(demuxer->video->id != i && demuxer->video->id != -1) st->discard= AVDISCARD_ALL; else{ demuxer->video->id = i; demuxer->video->sh= demuxer->v_streams[i]; } stream_id = priv->video_streams++; break; } case CODEC_TYPE_SUBTITLE:{ sh_sub_t* sh_sub; char type; /* only support text subtitles for now */ if(codec->codec_id == CODEC_ID_TEXT) type = 't'; else if(codec->codec_id == CODEC_ID_MOV_TEXT) type = 'm'; else if(codec->codec_id == CODEC_ID_SSA) type = 'a'; else if(codec->codec_id == CODEC_ID_DVD_SUBTITLE) type = 'v'; else if(codec->codec_id == CODEC_ID_XSUB) type = 'x'; else if(codec->codec_id == CODEC_ID_DVB_SUBTITLE) type = 'b'; else if(codec->codec_id == CODEC_ID_DVB_TELETEXT) type = 'd'; else if(codec->codec_id == CODEC_ID_HDMV_PGS_SUBTITLE) type = 'p'; else break; sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams, lang ? lang->value : NULL); if(!sh_sub) break; stream_type = "subtitle"; priv->sstreams[priv->sub_streams] = i; sh_sub->type = type; if (codec->extradata_size) { sh_sub->extradata = malloc(codec->extradata_size); memcpy(sh_sub->extradata, codec->extradata, codec->extradata_size); sh_sub->extradata_len = codec->extradata_size; } if (title && title->value) mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", priv->sub_streams, title->value); if (st->disposition & AV_DISPOSITION_DEFAULT) sh_sub->default_track = 1; stream_id = priv->sub_streams++; break; } case CODEC_TYPE_ATTACHMENT:{ if (st->codec->codec_id == CODEC_ID_TTF) demuxer_add_attachment(demuxer, st->filename, "application/x-truetype-font", codec->extradata, codec->extradata_size); break; } default: st->discard= AVDISCARD_ALL; } if (stream_type) { AVCodec *avc = avcodec_find_decoder(codec->codec_id); const char *codec_name = avc ? avc->name : "unknown"; if (strcmp("tta", codec_name) == 0) demuxer->ttaflag = 1; if (!avc && *stream_type == 's' && demuxer->s_streams[stream_id]) codec_name = sh_sub_type2str(((sh_sub_t *)demuxer->s_streams[stream_id])->type); mp_msg(MSGT_DEMUX, MSGL_INFO, "[lavf] stream %d: %s (%s), -%cid %d", i, stream_type, codec_name, *stream_type, stream_id); if (lang && lang->value && *stream_type != 'v') mp_msg(MSGT_DEMUX, MSGL_INFO, ", -%clang %s", *stream_type, lang->value); if (title && title->value) mp_msg(MSGT_DEMUX, MSGL_INFO, ", %s", title->value); mp_msg(MSGT_DEMUX, MSGL_INFO, "\n"); } }
DMO_AudioDecoder * DMO_AudioDecoder_Open(const char* dllname, GUID* guid, WAVEFORMATEX* wf,int out_channels) //DMO_AudioDecoder * DMO_AudioDecoder_Create(const CodecInfo * info, const WAVEFORMATEX* wf) { DMO_AudioDecoder *this; int sz; WAVEFORMATEX* pWF; #ifdef LDT_paranoia Setup_LDT_Keeper(); Setup_FS_Segment(); #endif this = malloc(sizeof(DMO_AudioDecoder)); this->m_iFlushed=1; sz = 18 + wf->cbSize; this->m_sVhdr = malloc(sz); memcpy(this->m_sVhdr, wf, sz); this->m_sVhdr2 = malloc(18); memcpy(this->m_sVhdr2, this->m_sVhdr, 18); pWF = (WAVEFORMATEX*)this->m_sVhdr2; pWF->wFormatTag = 1; pWF->wBitsPerSample = 16; pWF->nChannels = out_channels; pWF->nBlockAlign = 2*pWF->nChannels; //pWF->nChannels * (pWF->wBitsPerSample + 7) / 8; pWF->nAvgBytesPerSec = pWF->nBlockAlign * pWF->nSamplesPerSec; pWF->cbSize = 0; memset(&this->m_sOurType, 0, sizeof(this->m_sOurType)); this->m_sOurType.majortype=MEDIATYPE_Audio; this->m_sOurType.subtype=MEDIASUBTYPE_PCM; this->m_sOurType.subtype.f1=wf->wFormatTag; this->m_sOurType.formattype=FORMAT_WaveFormatEx; this->m_sOurType.lSampleSize=wf->nBlockAlign; this->m_sOurType.bFixedSizeSamples=1; this->m_sOurType.bTemporalCompression=0; this->m_sOurType.cbFormat=sz; this->m_sOurType.pbFormat=this->m_sVhdr; memset(&this->m_sDestType, 0, sizeof(this->m_sDestType)); this->m_sDestType.majortype=MEDIATYPE_Audio; this->m_sDestType.subtype=MEDIASUBTYPE_PCM; this->m_sDestType.formattype=FORMAT_WaveFormatEx; this->m_sDestType.bFixedSizeSamples=1; this->m_sDestType.bTemporalCompression=0; this->m_sDestType.lSampleSize=pWF->nBlockAlign; this->m_sDestType.cbFormat=18; //pWF->cbSize; this->m_sDestType.pbFormat=this->m_sVhdr2; #if 0 print_wave_header((WAVEFORMATEX *)this->m_sVhdr); print_wave_header((WAVEFORMATEX *)this->m_sVhdr2); #endif this->m_pDMO_Filter = DMO_FilterCreate(dllname, guid, &this->m_sOurType, &this->m_sDestType); if( !this->m_pDMO_Filter ) { free(this->m_sVhdr); free(this->m_sVhdr2); free(this); return NULL; } return this; }
static demuxer_t* demux_open_lavf(demuxer_t *demuxer){ AVFormatContext *avfc; AVFormatParameters ap; AVOption *opt; lavf_priv_t *priv= demuxer->priv; int i,g; char mp_filename[256]="mp:"; memset(&ap, 0, sizeof(AVFormatParameters)); stream_seek(demuxer->stream, 0); register_protocol(&mp_protocol); avfc = av_alloc_format_context(); if (correct_pts) avfc->flags |= AVFMT_FLAG_GENPTS; if (index_mode == 0) avfc->flags |= AVFMT_FLAG_IGNIDX; ap.prealloced_context = 1; if(opt_probesize) { double d = (double) opt_probesize; opt = av_set_double(avfc, "probesize", opt_probesize); if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %.3f\r\n", d); } if(demuxer->stream->url) strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3); else strncpy(mp_filename + 3, "foobar.dummy", sizeof(mp_filename)-3); url_fopen(&priv->pb, mp_filename, URL_RDONLY); ((URLContext*)(priv->pb.opaque))->priv_data= demuxer->stream; if(av_open_input_stream(&avfc, &priv->pb, mp_filename, priv->avif, &ap)<0){ mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n"); return NULL; } priv->avfc= avfc; if(av_find_stream_info(avfc) < 0){ mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n"); return NULL; } if(avfc->title [0]) demux_info_add(demuxer, "name" , avfc->title ); if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author ); if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright); if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment ); if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album ); // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year ); // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track ); if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre ); for(i=0; i<avfc->nb_streams; i++){ AVStream *st= avfc->streams[i]; AVCodecContext *codec= st->codec; switch(codec->codec_type){ case CODEC_TYPE_AUDIO:{ WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1); sh_audio_t* sh_audio; if(priv->audio_streams >= MAX_A_STREAMS) break; sh_audio=new_sh_audio(demuxer, i); if(!sh_audio) break; priv->astreams[priv->audio_streams] = i; priv->audio_streams++; if(!codec->codec_tag) codec->codec_tag= av_codec_get_tag(mp_wav_taglists, codec->codec_id); wf->wFormatTag= codec->codec_tag; wf->nChannels= codec->channels; wf->nSamplesPerSec= codec->sample_rate; wf->nAvgBytesPerSec= codec->bit_rate/8; wf->nBlockAlign= codec->block_align ? codec->block_align : 1; wf->wBitsPerSample= codec->bits_per_sample; wf->cbSize= codec->extradata_size; if(codec->extradata_size){ memcpy( wf + 1, codec->extradata, codec->extradata_size); } sh_audio->wf= wf; sh_audio->audio.dwSampleSize= codec->block_align; if(codec->frame_size && codec->sample_rate){ sh_audio->audio.dwScale=codec->frame_size; sh_audio->audio.dwRate= codec->sample_rate; }else{ sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8; sh_audio->audio.dwRate = codec->bit_rate; } g= ff_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate); sh_audio->audio.dwScale /= g; sh_audio->audio.dwRate /= g; // printf("sca:%d rat:%d fs:%d sr:%d ba:%d\n", sh_audio->audio.dwScale, sh_audio->audio.dwRate, codec->frame_size, codec->sample_rate, codec->block_align); sh_audio->ds= demuxer->audio; sh_audio->format= codec->codec_tag; sh_audio->channels= codec->channels; sh_audio->samplerate= codec->sample_rate; sh_audio->i_bps= codec->bit_rate/8; switch (codec->codec_id) { case CODEC_ID_PCM_S8: case CODEC_ID_PCM_U8: sh_audio->samplesize = 1; break; case CODEC_ID_PCM_S16LE: case CODEC_ID_PCM_S16BE: case CODEC_ID_PCM_U16LE: case CODEC_ID_PCM_U16BE: sh_audio->samplesize = 2; break; case CODEC_ID_PCM_ALAW: sh_audio->format = 0x6; break; case CODEC_ID_PCM_MULAW: sh_audio->format = 0x7; break; } if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V); if((audio_lang && st->language[0] && !strncmp(audio_lang, st->language, 3)) || (demuxer->audio->id == i || demuxer->audio->id == -1) ) { demuxer->audio->id = i; demuxer->audio->sh= demuxer->a_streams[i]; } else st->discard= AVDISCARD_ALL; break;} case CODEC_TYPE_VIDEO:{ sh_video_t* sh_video; BITMAPINFOHEADER *bih; if(priv->video_streams >= MAX_V_STREAMS) break; sh_video=new_sh_video(demuxer, i); if(!sh_video) break; priv->vstreams[priv->video_streams] = i; priv->video_streams++; bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1); if(!codec->codec_tag) codec->codec_tag= av_codec_get_tag(mp_bmp_taglists, codec->codec_id); bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size; bih->biWidth= codec->width; bih->biHeight= codec->height; bih->biBitCount= codec->bits_per_sample; bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8; bih->biCompression= codec->codec_tag; sh_video->bih= bih; sh_video->disp_w= codec->width; sh_video->disp_h= codec->height; if (st->time_base.den) { /* if container has time_base, use that */ sh_video->video.dwRate= st->time_base.den; sh_video->video.dwScale= st->time_base.num; } else { sh_video->video.dwRate= codec->time_base.den; sh_video->video.dwScale= codec->time_base.num; } sh_video->fps=av_q2d(st->r_frame_rate); sh_video->frametime=1/av_q2d(st->r_frame_rate); sh_video->format = bih->biCompression; sh_video->aspect= codec->width * codec->sample_aspect_ratio.num / (float)(codec->height * codec->sample_aspect_ratio.den); sh_video->i_bps= codec->bit_rate/8; mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n", codec->width, codec->sample_aspect_ratio.num, codec->height, codec->sample_aspect_ratio.den); sh_video->ds= demuxer->video; if(codec->extradata_size) memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size); if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V); /* short biPlanes; int biXPelsPerMeter; int biYPelsPerMeter; int biClrUsed; int biClrImportant;*/ if(demuxer->video->id != i && demuxer->video->id != -1) st->discard= AVDISCARD_ALL; else{ demuxer->video->id = i; demuxer->video->sh= demuxer->v_streams[i]; } break;} default: st->discard= AVDISCARD_ALL; } } mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams); mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD); if(!priv->audio_streams) demuxer->audio->id=-2; // nosound // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio; if(!priv->video_streams){ if(!priv->audio_streams){ mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n"); return NULL; } demuxer->video->id=-2; // audio-only } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video; return demuxer; }
static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) { lavf_priv_t *priv= demuxer->priv; AVStream *st= avfc->streams[i]; AVCodecContext *codec= st->codec; int g; switch(codec->codec_type){ case CODEC_TYPE_AUDIO:{ WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1); sh_audio_t* sh_audio; if(priv->audio_streams >= MAX_A_STREAMS) break; sh_audio=new_sh_audio(demuxer, i); mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "lavf", i); if(!sh_audio) break; priv->astreams[priv->audio_streams] = i; priv->audio_streams++; // mp4a tag is used for all mp4 files no matter what they actually contain if(codec->codec_tag == MKTAG('m', 'p', '4', 'a')) codec->codec_tag= 0; if(codec->codec_id == CODEC_ID_ADPCM_IMA_AMV) codec->codec_tag= MKTAG('A','M','V','A'); if(!codec->codec_tag) codec->codec_tag= av_codec_get_tag(mp_wav_taglists, codec->codec_id); wf->wFormatTag= codec->codec_tag; wf->nChannels= codec->channels; wf->nSamplesPerSec= codec->sample_rate; wf->nAvgBytesPerSec= codec->bit_rate/8; wf->nBlockAlign= codec->block_align ? codec->block_align : 1; wf->wBitsPerSample= codec->bits_per_sample; wf->cbSize= codec->extradata_size; if(codec->extradata_size) memcpy(wf + 1, codec->extradata, codec->extradata_size); sh_audio->wf= wf; sh_audio->audio.dwSampleSize= codec->block_align; if(codec->frame_size && codec->sample_rate){ sh_audio->audio.dwScale=codec->frame_size; sh_audio->audio.dwRate= codec->sample_rate; }else{ sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8; sh_audio->audio.dwRate = codec->bit_rate; } g= ff_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate); sh_audio->audio.dwScale /= g; sh_audio->audio.dwRate /= g; // printf("sca:%d rat:%d fs:%d sr:%d ba:%d\n", sh_audio->audio.dwScale, sh_audio->audio.dwRate, codec->frame_size, codec->sample_rate, codec->block_align); sh_audio->ds= demuxer->audio; sh_audio->format= codec->codec_tag; sh_audio->channels= codec->channels; sh_audio->samplerate= codec->sample_rate; sh_audio->i_bps= codec->bit_rate/8; switch (codec->codec_id) { case CODEC_ID_PCM_S8: case CODEC_ID_PCM_U8: sh_audio->samplesize = 1; break; case CODEC_ID_PCM_S16LE: case CODEC_ID_PCM_S16BE: case CODEC_ID_PCM_U16LE: case CODEC_ID_PCM_U16BE: sh_audio->samplesize = 2; break; case CODEC_ID_PCM_ALAW: sh_audio->format = 0x6; break; case CODEC_ID_PCM_MULAW: sh_audio->format = 0x7; break; } if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V); if((audio_lang && st->language[0] && !strncmp(audio_lang, st->language, 3)) || (demuxer->audio->id == i || demuxer->audio->id == -1)) { demuxer->audio->id = i; demuxer->audio->sh= demuxer->a_streams[i]; } else st->discard= AVDISCARD_ALL; break; } case CODEC_TYPE_VIDEO:{ sh_video_t* sh_video; BITMAPINFOHEADER *bih; if(priv->video_streams >= MAX_V_STREAMS) break; sh_video=new_sh_video(demuxer, i); mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "lavf", i); if(!sh_video) break; priv->vstreams[priv->video_streams] = i; priv->video_streams++; bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1); if(!codec->codec_tag) codec->codec_tag= av_codec_get_tag(mp_bmp_taglists, codec->codec_id); bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size; bih->biWidth= codec->width; bih->biHeight= codec->height; bih->biBitCount= codec->bits_per_sample; bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8; bih->biCompression= codec->codec_tag; sh_video->bih= bih; sh_video->disp_w= codec->width; sh_video->disp_h= codec->height; if (st->time_base.den) { /* if container has time_base, use that */ sh_video->video.dwRate= st->time_base.den; sh_video->video.dwScale= st->time_base.num; } else { sh_video->video.dwRate= codec->time_base.den; sh_video->video.dwScale= codec->time_base.num; } sh_video->fps=av_q2d(st->r_frame_rate); sh_video->frametime=1/av_q2d(st->r_frame_rate); sh_video->format=bih->biCompression; sh_video->aspect=codec->width * codec->sample_aspect_ratio.num / (float)(codec->height * codec->sample_aspect_ratio.den); sh_video->i_bps=codec->bit_rate/8; mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n", codec->width, codec->sample_aspect_ratio.num, codec->height, codec->sample_aspect_ratio.den); sh_video->ds= demuxer->video; if(codec->extradata_size) memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size); if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V); /* short biPlanes; int biXPelsPerMeter; int biYPelsPerMeter; int biClrUsed; int biClrImportant; */ if(demuxer->video->id != i && demuxer->video->id != -1) st->discard= AVDISCARD_ALL; else{ demuxer->video->id = i; demuxer->video->sh= demuxer->v_streams[i]; } break; } case CODEC_TYPE_SUBTITLE:{ sh_sub_t* sh_sub; if(priv->sub_streams >= MAX_S_STREAMS) break; /* only support text subtitles for now */ if(codec->codec_id != CODEC_ID_TEXT) break; sh_sub = new_sh_sub_sid(demuxer, i, priv->sub_streams); mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_SubtitleID, "lavf", priv->sub_streams); if(!sh_sub) break; priv->sstreams[priv->sub_streams] = i; sh_sub->type = 't'; demuxer->sub->sh = demuxer->s_streams[priv->sub_streams++]; break; } default: st->discard= AVDISCARD_ALL; } }
void read_avi_header(demuxer_t *demuxer,int index_mode){ sh_audio_t *sh_audio=NULL; sh_video_t *sh_video=NULL; int stream_id=-1; int idxfix_videostream=0; int idxfix_divx=0; avi_priv_t* priv=demuxer->priv; off_t list_end=0; //---- AVI header: priv->idx_size=0; priv->audio_streams=0; while(1){ int id=stream_read_dword_le(demuxer->stream); unsigned chunksize,size2; static int last_fccType=0; static int last_fccHandler=0; char* hdr=NULL; // if(stream_eof(demuxer->stream)) break; // Imply -forceidx if -saveidx is specified if (index_file_save) index_mode = 2; // if(id==mmioFOURCC('L','I','S','T')){ unsigned len=stream_read_dword_le(demuxer->stream); // list size id=stream_read_dword_le(demuxer->stream); // list type mp_msg(MSGT_HEADER,MSGL_DBG2,"LIST %.4s len=%u\n",(char *) &id,len); if(len >= 4) { len -= 4; list_end=stream_tell(demuxer->stream)+((len+1)&(~1)); } else { mp_msg(MSGT_HEADER,MSGL_WARN,MSGTR_MPDEMUX_AVIHDR_EmptyList); list_end = 0; } mp_msg(MSGT_HEADER,MSGL_V,"list_end=0x%X\n",(int)list_end); if(id==listtypeAVIMOVIE){ // found MOVI header if(!demuxer->movi_start) demuxer->movi_start=stream_tell(demuxer->stream); demuxer->movi_end=stream_tell(demuxer->stream)+len; mp_msg(MSGT_HEADER,MSGL_V,MSGTR_MPDEMUX_AVIHDR_FoundMovieAt,(int)demuxer->movi_start,(int)demuxer->movi_end); if(demuxer->stream->end_pos>demuxer->movi_end) demuxer->movi_end=demuxer->stream->end_pos; if(index_mode==-2 || index_mode==2 || index_mode==0) break; // reading from non-seekable source (stdin) or forced index or no index forced if(list_end>0) stream_seek(demuxer->stream,list_end); // skip movi list_end=0; } continue; } size2=stream_read_dword_le(demuxer->stream); mp_msg(MSGT_HEADER,MSGL_DBG2,"CHUNK %.4s len=%u\n",(char *) &id,size2); chunksize=(size2+1)&(~1); switch(id){ // Indicates where the subject of the file is archived case mmioFOURCC('I','A','R','L'): hdr="Archival Location";break; // Lists the artist of the original subject of the file; // for example, "Michaelangelo." case mmioFOURCC('I','A','R','T'): hdr="Artist";break; // Lists the name of the person or organization that commissioned // the subject of the file; for example "Pope Julian II." case mmioFOURCC('I','C','M','S'): hdr="Commissioned";break; // Provides general comments about the file or the subject // of the file. If the comment is several sentences long, end each // sentence with a period. Do not include new-line characters. case mmioFOURCC('I','C','M','T'): hdr="Comments";break; // Records the copyright information for the file; for example, // "Copyright Encyclopedia International 1991." If there are multiple // copyrights, separate them by semicolon followed by a space. case mmioFOURCC('I','C','O','P'): hdr="Copyright";break; // Describes whether an image has been cropped and, if so, how it // was cropped; for example, "lower-right corner." case mmioFOURCC('I','C','R','D'): hdr="Creation Date";break; // Describes whether an image has been cropped and, if so, how it // was cropped; for example, "lower-right corner." case mmioFOURCC('I','C','R','P'): hdr="Cropped";break; // Specifies the size of the original subject of the file; for // example, "8.5 in h, 11 in w." case mmioFOURCC('I','D','I','M'): hdr="Dimensions";break; // Stores dots per inch setting of the digitizer used to // produce the file, such as "300." case mmioFOURCC('I','D','P','I'): hdr="Dots Per Inch";break; // Stores the of the engineer who worked on the file. If there are // multiple engineers, separate the names by a semicolon and a blank; // for example, "Smith, John; Adams, Joe." case mmioFOURCC('I','E','N','G'): hdr="Engineer";break; // Describes the original work, such as "landscape,", "portrait," // "still liefe," etc. case mmioFOURCC('I','G','N','R'): hdr="Genre";break; // Provides a list of keywords that refer to the file or subject of the // file. Separate multiple keywords with a semicolon and a blank; // for example, "Seattle, aerial view; scenery." case mmioFOURCC('I','K','E','Y'): hdr="Keywords";break; // ILGT - Describes the changes in the lightness settings on the digitizer // required to produce the file. Note that the format of this information // depends on the hardware used. case mmioFOURCC('I','L','G','T'): hdr="Lightness";break; // IMED - Decribes the original subject of the file, such as // "computer image," "drawing," "lithograph," and so on. case mmioFOURCC('I','M','E','D'): hdr="Medium";break; // INAM - Stores the title of the subject of the file, such as // "Seattle from Above." case mmioFOURCC('I','N','A','M'): hdr="Title";break; // IPLT - Specifies the number of colors requested when digitizing // an image, such as "256." case mmioFOURCC('I','P','L','T'): hdr="Palette Setting";break; // IPRD - Specifies the name of title the file was originally intended // for, such as "Encyclopedia of Pacific Northwest Geography." case mmioFOURCC('I','P','R','D'): hdr="Product";break; // ISBJ - Decsribes the contents of the file, such as // "Aerial view of Seattle." case mmioFOURCC('I','S','B','J'): hdr="Subject";break; // ISFT - Identifies the name of the software packages used to create the // file, such as "Microsoft WaveEdit" case mmioFOURCC('I','S','F','T'): hdr="Software";break; // ISHP - Identifies the change in sharpness for the digitizer // required to produce the file (the format depends on the hardware used). case mmioFOURCC('I','S','H','P'): hdr="Sharpness";break; // ISRC - Identifies the name of the person or organization who // suplied the original subject of the file; for example, "Try Research." case mmioFOURCC('I','S','R','C'): hdr="Source";break; // ISRF - Identifies the original form of the material that was digitized, // such as "slide," "paper," "map," and so on. This is not necessarily // the same as IMED case mmioFOURCC('I','S','R','F'): hdr="Source Form";break; // ITCH - Identifies the technician who digitized the subject file; // for example, "Smith, John." case mmioFOURCC('I','T','C','H'): hdr="Technician";break; case mmioFOURCC('I','S','M','P'): hdr="Time Code";break; case mmioFOURCC('I','D','I','T'): hdr="Digitization Time";break; case ckidAVIMAINHDR: // read 'avih' stream_read(demuxer->stream,(char*) &avih,FFMIN(size2,sizeof(avih))); le2me_MainAVIHeader(&avih); // swap to machine endian chunksize-=FFMIN(size2,sizeof(avih)); if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_avih(&avih,MSGL_V); // else print_avih_flags(&avih,MSGL_V); break; case ckidSTREAMHEADER: { // read 'strh' AVIStreamHeader h; stream_read(demuxer->stream,(char*) &h,FFMIN(size2,sizeof(h))); le2me_AVIStreamHeader(&h); // swap to machine endian chunksize-=FFMIN(size2,sizeof(h)); ++stream_id; if(h.fccType==streamtypeVIDEO){ sh_video=new_sh_video(demuxer,stream_id); mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "aviheader", stream_id); memcpy(&sh_video->video,&h,sizeof(h)); sh_video->stream_delay = (float)sh_video->video.dwStart * sh_video->video.dwScale/sh_video->video.dwRate; } else if(h.fccType==streamtypeAUDIO){ sh_audio=new_sh_audio(demuxer,stream_id); mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "aviheader", stream_id); memcpy(&sh_audio->audio,&h,sizeof(h)); sh_audio->stream_delay = (float)sh_audio->audio.dwStart * sh_audio->audio.dwScale/sh_audio->audio.dwRate; } last_fccType=h.fccType; last_fccHandler=h.fccHandler; if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_strh(&h,MSGL_V); break; } case mmioFOURCC('i', 'n', 'd', 'x'): { uint32_t i; avisuperindex_chunk *s; if(!index_mode) break; if(chunksize<=24){ break; } priv->suidx_size++; priv->suidx = realloc_struct(priv->suidx, priv->suidx_size, sizeof (avisuperindex_chunk)); s = &priv->suidx[priv->suidx_size-1]; chunksize-=24; memcpy(s->fcc, "indx", 4); s->dwSize = size2; s->wLongsPerEntry = stream_read_word_le(demuxer->stream); s->bIndexSubType = stream_read_char(demuxer->stream); s->bIndexType = stream_read_char(demuxer->stream); s->nEntriesInUse = stream_read_dword_le(demuxer->stream); *(uint32_t *)s->dwChunkId = stream_read_dword_le(demuxer->stream); stream_read(demuxer->stream, (char *)s->dwReserved, 3*4); memset(s->dwReserved, 0, 3*4); print_avisuperindex_chunk(s,MSGL_V); // Check and fix this useless crap if(s->wLongsPerEntry != sizeof (avisuperindex_entry)/4) { mp_msg (MSGT_HEADER, MSGL_WARN, "Broken super index chunk size: %u\n",s->wLongsPerEntry); s->wLongsPerEntry = sizeof(avisuperindex_entry)/4; } if( ((chunksize/4)/s->wLongsPerEntry) < s->nEntriesInUse){ mp_msg (MSGT_HEADER, MSGL_WARN, "Broken super index chunk\n"); s->nEntriesInUse = (chunksize/4)/s->wLongsPerEntry; } s->aIndex = calloc(s->nEntriesInUse, sizeof (avisuperindex_entry)); s->stdidx = calloc(s->nEntriesInUse, sizeof (avistdindex_chunk)); // now the real index of indices for (i=0; i<s->nEntriesInUse; i++) { chunksize-=16; s->aIndex[i].qwOffset = stream_read_qword_le(demuxer->stream); s->aIndex[i].dwSize = stream_read_dword_le(demuxer->stream); s->aIndex[i].dwDuration = stream_read_dword_le(demuxer->stream); mp_msg (MSGT_HEADER, MSGL_V, "ODML (%.4s): [%d] 0x%016"PRIx64" 0x%04x %u\n", (s->dwChunkId), i, (uint64_t)s->aIndex[i].qwOffset, s->aIndex[i].dwSize, s->aIndex[i].dwDuration); } break; } case ckidSTREAMFORMAT: { // read 'strf' if(last_fccType==streamtypeVIDEO){ sh_video->bih=calloc(FFMAX(chunksize, sizeof(BITMAPINFOHEADER)), 1); // sh_video->bih=malloc(chunksize); memset(sh_video->bih,0,chunksize); mp_msg(MSGT_HEADER,MSGL_V,MSGTR_MPDEMUX_AVIHDR_FoundBitmapInfoHeader,chunksize,sizeof(BITMAPINFOHEADER)); stream_read(demuxer->stream,(char*) sh_video->bih,chunksize); le2me_BITMAPINFOHEADER(sh_video->bih); // swap to machine endian if (sh_video->bih->biSize > chunksize && sh_video->bih->biSize > sizeof(BITMAPINFOHEADER)) sh_video->bih->biSize = chunksize; // fixup MS-RLE header (seems to be broken for <256 color files) if(sh_video->bih->biCompression<=1 && sh_video->bih->biSize==40) sh_video->bih->biSize=chunksize; if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih,MSGL_V); chunksize=0; sh_video->fps=(float)sh_video->video.dwRate/(float)sh_video->video.dwScale; sh_video->frametime=(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; sh_video->format = sh_video->bih->biCompression; // if(demuxer->video->id==-1) demuxer->video->id=stream_id; // IdxFix: idxfix_videostream=stream_id; switch(sh_video->bih->biCompression){ case mmioFOURCC('M', 'P', 'G', '4'): case mmioFOURCC('m', 'p', 'g', '4'): case mmioFOURCC('D', 'I', 'V', '1'): idxfix_divx=3; // set index recovery mpeg4 flavour: msmpeg4v1 mp_msg(MSGT_HEADER,MSGL_V,MSGTR_MPDEMUX_AVIHDR_RegeneratingKeyfTableForMPG4V1); break; case mmioFOURCC('D', 'I', 'V', '3'): case mmioFOURCC('d', 'i', 'v', '3'): case mmioFOURCC('D', 'I', 'V', '4'): case mmioFOURCC('d', 'i', 'v', '4'): case mmioFOURCC('D', 'I', 'V', '5'): case mmioFOURCC('d', 'i', 'v', '5'): case mmioFOURCC('D', 'I', 'V', '6'): case mmioFOURCC('d', 'i', 'v', '6'): case mmioFOURCC('M', 'P', '4', '3'): case mmioFOURCC('m', 'p', '4', '3'): case mmioFOURCC('M', 'P', '4', '2'): case mmioFOURCC('m', 'p', '4', '2'): case mmioFOURCC('D', 'I', 'V', '2'): case mmioFOURCC('A', 'P', '4', '1'): idxfix_divx=1; // set index recovery mpeg4 flavour: msmpeg4v3 mp_msg(MSGT_HEADER,MSGL_V,MSGTR_MPDEMUX_AVIHDR_RegeneratingKeyfTableForDIVX3); break; case mmioFOURCC('D', 'I', 'V', 'X'): case mmioFOURCC('d', 'i', 'v', 'x'): case mmioFOURCC('D', 'X', '5', '0'): case mmioFOURCC('X', 'V', 'I', 'D'): case mmioFOURCC('x', 'v', 'i', 'd'): case mmioFOURCC('F', 'M', 'P', '4'): case mmioFOURCC('f', 'm', 'p', '4'): idxfix_divx=2; // set index recovery mpeg4 flavour: generic mpeg4 mp_msg(MSGT_HEADER,MSGL_V,MSGTR_MPDEMUX_AVIHDR_RegeneratingKeyfTableForMPEG4); break; } } else if(last_fccType==streamtypeAUDIO){ unsigned wf_size = chunksize<sizeof(WAVEFORMATEX)?sizeof(WAVEFORMATEX):chunksize; sh_audio->wf=calloc(wf_size,1); // sh_audio->wf=malloc(chunksize); memset(sh_audio->wf,0,chunksize); mp_msg(MSGT_HEADER,MSGL_V,MSGTR_MPDEMUX_AVIHDR_FoundWaveFmt,chunksize,sizeof(WAVEFORMATEX)); stream_read(demuxer->stream,(char*) sh_audio->wf,chunksize); le2me_WAVEFORMATEX(sh_audio->wf); if (sh_audio->wf->cbSize != 0 && wf_size < sizeof(WAVEFORMATEX)+sh_audio->wf->cbSize) { sh_audio->wf=realloc(sh_audio->wf, sizeof(WAVEFORMATEX)+sh_audio->wf->cbSize); } sh_audio->format=sh_audio->wf->wFormatTag; if (sh_audio->format == 1 && last_fccHandler == mmioFOURCC('A', 'x', 'a', 'n')) sh_audio->format = last_fccHandler; sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec; chunksize=0; if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf,MSGL_V); ++priv->audio_streams; // if(demuxer->audio->id==-1) demuxer->audio->id=stream_id; } break; } case mmioFOURCC('v', 'p', 'r', 'p'): { VideoPropHeader *vprp = malloc(chunksize); unsigned int i; stream_read(demuxer->stream, (void*)vprp, chunksize); le2me_VideoPropHeader(vprp); chunksize -= sizeof(*vprp)-sizeof(vprp->FieldInfo); chunksize /= sizeof(VIDEO_FIELD_DESC); if (vprp->nbFieldPerFrame > chunksize) { vprp->nbFieldPerFrame = chunksize; } chunksize = 0; for (i=0; i<vprp->nbFieldPerFrame; i++) { le2me_VIDEO_FIELD_DESC(&vprp->FieldInfo[i]); } if (sh_video) { sh_video->aspect = GET_AVI_ASPECT(vprp->dwFrameAspectRatio); } if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_vprp(vprp,MSGL_V); free(vprp); break; } case mmioFOURCC('d', 'm', 'l', 'h'): { // dmlh 00 00 00 04 frms unsigned int total_frames = stream_read_dword_le(demuxer->stream); mp_msg(MSGT_HEADER,MSGL_V,MSGTR_MPDEMUX_AVIHDR_FoundAVIV2Header, chunksize, total_frames); stream_skip(demuxer->stream, chunksize-4); chunksize = 0; } break; case ckidAVINEWINDEX: if(demuxer->movi_end>stream_tell(demuxer->stream)) demuxer->movi_end=stream_tell(demuxer->stream); // fixup movi-end if(index_mode && !priv->isodml){ int i; priv->idx_size=size2>>4; mp_msg(MSGT_HEADER,MSGL_V,MSGTR_MPDEMUX_AVIHDR_ReadingIndexBlockChunksForFrames, priv->idx_size,avih.dwTotalFrames, (int64_t)stream_tell(demuxer->stream)); priv->idx=malloc(priv->idx_size<<4); // printf("\nindex to %p !!!!! (priv=%p)\n",priv->idx,priv); stream_read(demuxer->stream,(char*)priv->idx,priv->idx_size<<4); for (i = 0; i < priv->idx_size; i++) { // swap index to machine endian AVIINDEXENTRY *entry=(AVIINDEXENTRY*)priv->idx + i; le2me_AVIINDEXENTRY(entry); /* * We (ab)use the upper word for bits 32-47 of the offset, so * we'll clear them here. * FIXME: AFAIK no codec uses them, but if one does it will break */ entry->dwFlags&=0xffff; } chunksize-=priv->idx_size<<4; if( mp_msg_test(MSGT_HEADER,MSGL_DBG2) ) print_index(priv->idx,priv->idx_size,MSGL_DBG2); } break; /* added May 2002 */ case mmioFOURCC('R','I','F','F'): { char riff_type[4]; mp_msg(MSGT_HEADER, MSGL_V, MSGTR_MPDEMUX_AVIHDR_AdditionalRIFFHdr); stream_read(demuxer->stream, riff_type, sizeof riff_type); if (strncmp(riff_type, "AVIX", sizeof riff_type)) mp_msg(MSGT_HEADER, MSGL_WARN, MSGTR_MPDEMUX_AVIHDR_WarnNotExtendedAVIHdr); else { /* * We got an extended AVI header, so we need to switch to * ODML to get seeking to work, provided we got indx chunks * in the header (suidx_size > 0). */ if (priv->suidx_size > 0) priv->isodml = 1; } chunksize = 0; list_end = 0; /* a new list will follow */ break; } case ckidAVIPADDING: stream_skip(demuxer->stream, chunksize); chunksize = 0; break; } if(hdr){ mp_msg(MSGT_HEADER,MSGL_V,"hdr=%s size=%u\n",hdr,size2); if(size2==3) chunksize=1; // empty else { char buf[256]; int len=(size2<250)?size2:250; stream_read(demuxer->stream,buf,len); chunksize-=len; buf[len]=0; mp_msg(MSGT_HEADER,MSGL_V,"%-10s: %s\n",hdr,buf); demux_info_add(demuxer, hdr, buf); } } mp_msg(MSGT_HEADER,MSGL_DBG2,"list_end=0x%"PRIX64" pos=0x%"PRIX64" chunksize=0x%"PRIX64" next=0x%"PRIX64"\n", (int64_t)list_end, (int64_t)stream_tell(demuxer->stream), (int64_t)chunksize, (int64_t)chunksize+(int64_t)stream_tell(demuxer->stream)); if(list_end>0 && chunksize+stream_tell(demuxer->stream) == list_end) list_end=0; if(list_end>0 && chunksize+stream_tell(demuxer->stream)>list_end){ mp_msg(MSGT_HEADER,MSGL_V,MSGTR_MPDEMUX_AVIHDR_BrokenChunk,chunksize,(char *) &id); stream_seek(demuxer->stream,list_end); list_end=0; } else if(chunksize>0) stream_skip(demuxer->stream,chunksize); else if((int)chunksize<0) mp_msg(MSGT_HEADER,MSGL_WARN,"chunksize=%u (id=%.4s)\n",chunksize,(char *) &id); }