void CodecFactory::removeCodec(const std::string& codec_id) { // convert PluginNotFound exceptions into CodecNotFound exceptions try { PluginConfig<Codec>::removePlugin(codec_id); } catch (PluginManager<Codec>::PluginNotFoundException&) { throw CodecNotFoundException(codec_id); } }
void CodecFactory::setCodecConfig(const std::string& codec_id, const xmlNodePtr config_ptr) { // convert PluginNotFound exceptions into CodecNotFound exceptions try { PluginConfig<Codec>::setPluginConfig(codec_id, config_ptr); } catch (PluginManager<Codec>::PluginNotFoundException&) { throw CodecNotFoundException(codec_id); } }
CodecPtr CodecFactory::getCodec(const std::string& codec_id) { boost::mutex::scoped_lock factory_lock(m_mutex); Codec *codec_ptr = m_plugins.get(codec_id); // throw an exception if the codec was not found if (codec_ptr == NULL) throw CodecNotFoundException(codec_id); // return a cloned instance of the Codec since its state may change // while encoding or decoding data streams return codec_ptr->clone(); }
FFData(const std::string &path, VSFileSystem::VSFileType type, Format &fmt, int streamIdx) throw(Exception) : pFormatCtx(0), pCodecCtx(0), pCodec(0), pStream(0), packetBuffer(0), packetBufferSize(0), sampleBufferBase(0), filepath(path), filetype(type), audioStreamIndex(streamIdx) { packet.data = 0; char buf[(sizeof(type)+1)/2+1]; sprintf(buf, "%d", type); // Initialize libavcodec/libavformat if necessary FFMpeg::initLibraries(); // Open file std::string npath = std::string("vsfile:") + path + "|" + buf; std::string errbase = std::string("Cannot open URL \"") + npath + "\""; if ( (0 != av_open_input_file(&pFormatCtx, npath.c_str(), NULL, BUFFER_SIZE, NULL)) ||(0 > av_find_stream_info(pFormatCtx)) ) throw FileOpenException(errbase + " (wrong format or file not found)"); // Dump format info in case we want to know... #ifdef VS_DEBUG dump_format(pFormatCtx, 0, npath.c_str(), false); #endif // Find audio stream pCodecCtx = 0; streamIndex = -1; for (unsigned int i=0; (pCodecCtx==0) && (i < pFormatCtx->nb_streams); ++i) if ((pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) && (streamIdx-- == 0)) pCodecCtx = (pStream = pFormatCtx->streams[streamIndex = i])->codec; if (pCodecCtx == 0) throw FileOpenException(errbase + " (wrong or no audio stream)"); // Find codec for the audio stream and open it pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if(pCodec == 0) throw CodecNotFoundException(errbase + " (unsupported codec)"); if(avcodec_open(pCodecCtx, pCodec) < 0) throw CodecNotFoundException(errbase + " (unsupported codec)"); // Get some info fmt.sampleFrequency = pCodecCtx->sample_rate; fmt.channels = pCodecCtx->channels; fmt.nativeOrder = 1; // always so for ffmpeg switch (pCodecCtx->sample_fmt) { case SAMPLE_FMT_U8: fmt.bitsPerSample = 8; fmt.signedSamples = 0; break; case SAMPLE_FMT_S16: fmt.bitsPerSample = 16; fmt.signedSamples = 1; break; #ifdef SAMPLE_FMT_S24 case SAMPLE_FMT_S24: fmt.bitsPerSample = 24; fmt.signedSamples = 1; break; #endif #ifdef SAMPLE_FMT_S32 case SAMPLE_FMT_S32: fmt.bitsPerSample = 32; fmt.signedSamples = 1; break; #endif default: throw CodecNotFoundException(errbase + " (unsupported audio format)"); } sampleSize = (fmt.bitsPerSample + 7) / 8 * fmt.channels; assert(sampleSize > 0); // Initialize timebase counter sampleBufferStart = 0; streamSize = 0; // Initialize sample buffer sampleBufferBase = malloc(sampleSize * BUFFER_SIZE + BUFFER_ALIGNMENT); ptrdiff_t offs = ((reinterpret_cast<ptrdiff_t>(sampleBufferBase)) & (BUFFER_ALIGNMENT-1)); sampleBufferAligned = ((char*)sampleBufferBase) + BUFFER_ALIGNMENT - offs; sampleBufferAlloc = sampleSize * BUFFER_SIZE; sampleBuffer = 0; sampleBufferSize = 0; }