static void parse_waveformatex(AVIOContext *pb, AVCodecContext *c) { ff_asf_guid subformat; int bps = avio_rl16(pb); if (bps) c->bits_per_coded_sample = bps; c->channel_layout = avio_rl32(pb); /* dwChannelMask */ ff_get_guid(pb, &subformat); { const uint8_t aguid[] = { FF_MEDIASUBTYPE_BASE_GUID }; if (!memcmp(subformat + 4, aguid, 12)) { c->codec_tag = AV_RL32(subformat); c->codec_id = ff_wav_codec_get_id(c->codec_tag, c->bits_per_coded_sample); } else { c->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat); if (!c->codec_id) av_log(c, AV_LOG_WARNING, "unknown subformat:"FF_PRI_GUID"\n", FF_ARG_GUID(subformat)); } } }
int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size) { int id; id = avio_rl16(pb); codec->codec_type = AVMEDIA_TYPE_AUDIO; codec->channels = avio_rl16(pb); codec->sample_rate = avio_rl32(pb); codec->bit_rate = avio_rl32(pb) * 8; codec->block_align = avio_rl16(pb); if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */ codec->bits_per_coded_sample = 8; } else codec->bits_per_coded_sample = avio_rl16(pb); if (id == 0xFFFE) { codec->codec_tag = 0; } else { codec->codec_tag = id; codec->codec_id = ff_wav_codec_get_id(id, codec->bits_per_coded_sample); } if (size >= 18) { /* We're obviously dealing with WAVEFORMATEX */ int cbSize = avio_rl16(pb); /* cbSize */ size -= 18; cbSize = FFMIN(size, cbSize); if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */ parse_waveformatex(pb, codec); cbSize -= 22; size -= 22; } codec->extradata_size = cbSize; if (cbSize > 0) { av_free(codec->extradata); codec->extradata = av_mallocz(codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); if (!codec->extradata) return AVERROR(ENOMEM); avio_read(pb, codec->extradata, codec->extradata_size); size -= cbSize; } /* It is possible for the chunk to contain garbage at the end */ if (size > 0) avio_skip(pb, size); } if (codec->codec_id == AV_CODEC_ID_AAC_LATM) { /* Channels and sample_rate values are those prior to applying SBR * and/or PS. */ codec->channels = 0; codec->sample_rate = 0; } /* override bits_per_coded_sample for G.726 */ if (codec->codec_id == AV_CODEC_ID_ADPCM_G726 && codec->sample_rate) codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate; return 0; }
static int avisynth_read_header(AVFormatContext *s, AVFormatParameters *ap) { AVISynthContext *avs = s->priv_data; HRESULT res; AVIFILEINFO info; DWORD id; AVStream *st; AVISynthStream *stream; AVIFileInit(); res = AVIFileOpen(&avs->file, s->filename, OF_READ|OF_SHARE_DENY_WRITE, NULL); if (res != S_OK) { av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res); AVIFileExit(); return -1; } res = AVIFileInfo(avs->file, &info, sizeof(info)); if (res != S_OK) { av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res); AVIFileExit(); return -1; } avs->streams = av_mallocz(info.dwStreams * sizeof(AVISynthStream)); for (id=0; id<info.dwStreams; id++) { stream = &avs->streams[id]; stream->read = 0; if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK) { if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK) { if (stream->info.fccType == streamtypeAUDIO) { WAVEFORMATEX wvfmt; LONG struct_size = sizeof(WAVEFORMATEX); if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK) continue; st = av_new_stream(s, id); st->codec->codec_type = AVMEDIA_TYPE_AUDIO; st->codec->block_align = wvfmt.nBlockAlign; st->codec->channels = wvfmt.nChannels; st->codec->sample_rate = wvfmt.nSamplesPerSec; st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8; st->codec->bits_per_coded_sample = wvfmt.wBitsPerSample; stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate; stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8; st->codec->codec_tag = wvfmt.wFormatTag; st->codec->codec_id = ff_wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_coded_sample); } else if (stream->info.fccType == streamtypeVIDEO) { BITMAPINFO imgfmt; LONG struct_size = sizeof(BITMAPINFO); stream->chunck_size = stream->info.dwSampleSize; stream->chunck_samples = 1; if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK) continue; st = av_new_stream(s, id); st->codec->codec_type = AVMEDIA_TYPE_VIDEO; st->r_frame_rate.num = stream->info.dwRate; st->r_frame_rate.den = stream->info.dwScale; st->codec->width = imgfmt.bmiHeader.biWidth; st->codec->height = imgfmt.bmiHeader.biHeight; st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount; st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale; st->codec->codec_tag = imgfmt.bmiHeader.biCompression; st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, imgfmt.bmiHeader.biCompression); st->duration = stream->info.dwLength; } else { AVIStreamRelease(stream->handle); continue; } avs->nb_streams++; st->codec->stream_codec_tag = stream->info.fccHandler; av_set_pts_info(st, 64, info.dwScale, info.dwRate); st->start_time = stream->info.dwStart; } } } return 0; }
/** * @brief read until we found all data needed for decoding * @param vst video stream of which to change parameters * @param ast video stream of which to change parameters * @param myth set if this is a MythTVVideo format file * @return 1 if all required codec data was found */ static int get_codec_data(AVIOContext *pb, AVStream *vst, AVStream *ast, int myth) { nuv_frametype frametype; if (!vst && !myth) return 1; // no codec data needed while (!url_feof(pb)) { int size, subtype; frametype = avio_r8(pb); switch (frametype) { case NUV_EXTRADATA: subtype = avio_r8(pb); avio_skip(pb, 6); size = PKTSIZE(avio_rl32(pb)); if (vst && subtype == 'R') { vst->codec->extradata_size = size; vst->codec->extradata = av_malloc(size); avio_read(pb, vst->codec->extradata, size); size = 0; if (!myth) return 1; } break; case NUV_MYTHEXT: avio_skip(pb, 7); size = PKTSIZE(avio_rl32(pb)); if (size != 128 * 4) break; avio_rl32(pb); // version if (vst) { vst->codec->codec_tag = avio_rl32(pb); vst->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, vst->codec->codec_tag); if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G')) vst->codec->codec_id = CODEC_ID_NUV; } else avio_skip(pb, 4); if (ast) { ast->codec->codec_tag = avio_rl32(pb); ast->codec->sample_rate = avio_rl32(pb); ast->codec->bits_per_coded_sample = avio_rl32(pb); ast->codec->channels = avio_rl32(pb); ast->codec->codec_id = ff_wav_codec_get_id(ast->codec->codec_tag, ast->codec->bits_per_coded_sample); ast->need_parsing = AVSTREAM_PARSE_FULL; } else avio_skip(pb, 4 * 4); size -= 6 * 4; avio_skip(pb, size); return 1; case NUV_SEEKP: size = 11; break; default: avio_skip(pb, 7); size = PKTSIZE(avio_rl32(pb)); break; } avio_skip(pb, size); } return 0; }
static int avisynth_read_header(AVFormatContext *s) { AVISynthContext *avs = s->priv_data; HRESULT res; AVIFILEINFO info; DWORD id; AVStream *st; AVISynthStream *stream; wchar_t filename_wchar[1024] = { 0 }; char filename_char[1024] = { 0 }; AVIFileInit(); /* avisynth can't accept UTF-8 filename */ MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wchar, 1024); WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wchar, -1, filename_char, 1024, NULL, NULL); res = AVIFileOpen(&avs->file, filename_char, OF_READ|OF_SHARE_DENY_WRITE, NULL); if (res != S_OK) { av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res); AVIFileExit(); return -1; } res = AVIFileInfo(avs->file, &info, sizeof(info)); if (res != S_OK) { av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res); AVIFileExit(); return -1; } avs->streams = av_mallocz(info.dwStreams * sizeof(AVISynthStream)); for (id=0; id<info.dwStreams; id++) { stream = &avs->streams[id]; stream->read = 0; if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK) { if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK) { if (stream->info.fccType == streamtypeAUDIO) { WAVEFORMATEX wvfmt; LONG struct_size = sizeof(WAVEFORMATEX); if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK) continue; st = avformat_new_stream(s, NULL); st->id = id; st->codec->codec_type = AVMEDIA_TYPE_AUDIO; st->codec->block_align = wvfmt.nBlockAlign; st->codec->channels = wvfmt.nChannels; st->codec->sample_rate = wvfmt.nSamplesPerSec; st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8; st->codec->bits_per_coded_sample = wvfmt.wBitsPerSample; stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate; stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8; st->codec->codec_tag = wvfmt.wFormatTag; st->codec->codec_id = ff_wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_coded_sample); } else if (stream->info.fccType == streamtypeVIDEO) { BITMAPINFO imgfmt; LONG struct_size = sizeof(BITMAPINFO); stream->chunck_size = stream->info.dwSampleSize; stream->chunck_samples = 1; if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK) continue; st = avformat_new_stream(s, NULL); st->id = id; st->codec->codec_type = AVMEDIA_TYPE_VIDEO; st->r_frame_rate.num = stream->info.dwRate; st->r_frame_rate.den = stream->info.dwScale; st->codec->width = imgfmt.bmiHeader.biWidth; st->codec->height = imgfmt.bmiHeader.biHeight; st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount; st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale; st->codec->codec_tag = imgfmt.bmiHeader.biCompression; st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, imgfmt.bmiHeader.biCompression); if (st->codec->codec_id == CODEC_ID_RAWVIDEO && imgfmt.bmiHeader.biCompression== BI_RGB) { st->codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE); if (st->codec->extradata) { st->codec->extradata_size = 9; memcpy(st->codec->extradata, "BottomUp", 9); } } st->duration = stream->info.dwLength; } else { AVIStreamRelease(stream->handle); continue; } avs->nb_streams++; st->codec->stream_codec_tag = stream->info.fccHandler; avpriv_set_pts_info(st, 64, info.dwScale, info.dwRate); st->start_time = stream->info.dwStart; } } } return 0; }
/** * @brief read until we found all data needed for decoding * @param vst video stream of which to change parameters * @param ast video stream of which to change parameters * @param myth set if this is a MythTVVideo format file * @return 0 or AVERROR code */ static int get_codec_data(AVIOContext *pb, AVStream *vst, AVStream *ast, int myth) { nuv_frametype frametype; if (!vst && !myth) return 1; // no codec data needed while (!avio_feof(pb)) { int size, subtype; frametype = avio_r8(pb); switch (frametype) { case NUV_EXTRADATA: subtype = avio_r8(pb); avio_skip(pb, 6); size = PKTSIZE(avio_rl32(pb)); if (vst && subtype == 'R') { if (vst->codec->extradata) { av_freep(&vst->codec->extradata); vst->codec->extradata_size = 0; } if (ff_get_extradata(vst->codec, pb, size) < 0) return AVERROR(ENOMEM); size = 0; if (!myth) return 0; } break; case NUV_MYTHEXT: avio_skip(pb, 7); size = PKTSIZE(avio_rl32(pb)); if (size != 128 * 4) break; avio_rl32(pb); // version if (vst) { vst->codec->codec_tag = avio_rl32(pb); vst->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, vst->codec->codec_tag); if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G')) vst->codec->codec_id = AV_CODEC_ID_NUV; } else avio_skip(pb, 4); if (ast) { int id; ast->codec->codec_tag = avio_rl32(pb); ast->codec->sample_rate = avio_rl32(pb); ast->codec->bits_per_coded_sample = avio_rl32(pb); ast->codec->channels = avio_rl32(pb); ast->codec->channel_layout = 0; id = ff_wav_codec_get_id(ast->codec->codec_tag, ast->codec->bits_per_coded_sample); if (id == AV_CODEC_ID_NONE) { id = ff_codec_get_id(nuv_audio_tags, ast->codec->codec_tag); if (id == AV_CODEC_ID_PCM_S16LE) id = ff_get_pcm_codec_id(ast->codec->bits_per_coded_sample, 0, 0, ~1); } ast->codec->codec_id = id; ast->need_parsing = AVSTREAM_PARSE_FULL; } else avio_skip(pb, 4 * 4); size -= 6 * 4; avio_skip(pb, size); return 0; case NUV_SEEKP: size = 11; break; default: avio_skip(pb, 7); size = PKTSIZE(avio_rl32(pb)); break; } avio_skip(pb, size); } return 0; }