status_t AVFormatWriter::Init(const media_file_format* fileFormat) { TRACE("AVFormatWriter::Init()\n"); uint8* buffer = static_cast<uint8*>(av_malloc(kIOBufferSize)); if (buffer == NULL) return B_NO_MEMORY; // Allocate I/O context and initialize it with buffer // and hook functions, pass ourself as cookie. fIOContext = av_alloc_put_byte(buffer, kIOBufferSize, 1, this, 0, _Write, _Seek); if (fIOContext == NULL) { TRACE("av_alloc_put_byte() failed!\n"); return B_ERROR; } // Setup I/O hooks. This seems to be enough. fContext->pb = fIOContext; // Set the AVOutputFormat according to fileFormat... fContext->oformat = av_guess_format(fileFormat->short_name, fileFormat->file_extension, fileFormat->mime_type); if (fContext->oformat == NULL) { TRACE(" failed to find AVOuputFormat for %s\n", fileFormat->short_name); return B_NOT_SUPPORTED; } TRACE(" found AVOuputFormat for %s: %s\n", fileFormat->short_name, fContext->oformat->name); return B_OK; }
ByteIOContext* FFmpegTagger::initIo(std::istream& istr, bool isSeekable, unsigned char* pIoBuffer) { static char streamName[] = "std::istream"; // FIXME: deallocate URLContext after stream has finished. URLContext* pUrlContext = new URLContext; pUrlContext->is_streamed = 1; // pUrlContext->priv_data = 0; pUrlContext->priv_data = &istr; pUrlContext->filename = streamName; pUrlContext->prot = new URLProtocol; pUrlContext->prot->name = "OMM avio wrapper for std::iostream"; pUrlContext->prot->next = 0; pUrlContext->prot->url_open = 0; // pUrlContext->prot->url_open = (int (*)(URLContext *, const char *, int))IOOpen; pUrlContext->prot->url_read = (int (*) (URLContext *, unsigned char *, int))FFmpegTagger::IORead; pUrlContext->prot->url_write = 0; if (isSeekable) { pUrlContext->prot->url_seek = (int64_t (*) (URLContext *, int64_t, int))FFmpegTagger::IOSeek; } else { pUrlContext->prot->url_seek = 0; } pUrlContext->prot->url_close = 0; // pUrlContext->prot->url_close = (int (*)(URLContext *))IOClose; LOG(ffmpeg, trace, "ffmpeg::av_alloc_put_byte() ..."); ByteIOContext* pIoContext; if (isSeekable) { pIoContext = av_alloc_put_byte(pIoBuffer, _IoBufferSize, 0, pUrlContext, FFmpegTagger::IORead, 0, FFmpegTagger::IOSeek); } else { pIoContext = av_alloc_put_byte(pIoBuffer, _IoBufferSize, 0, pUrlContext, FFmpegTagger::IORead, 0, 0); pIoContext->is_streamed = 1; } pIoContext->max_packet_size = _IoBufferSize; _timestamp.update(); _startTimestamp.update(); return pIoContext; }
int decoder_load_info( Decoder *pDecoder ) { unsigned int i = 0; AVProbeData prbData; uint8_t *pBuffer, *pIoBuffer; AVInputFormat *pInputFmt; ByteIOContext *pIoCtx; prbData.filename = "DUMMY"; prbData.buf_size = LARGE_BUFFER_SIZE; pBuffer = (uint8_t*) av_mallocz( LARGE_BUFFER_SIZE ); memset( pBuffer + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE ); pDecoder->fpRead( pDecoder->pOpaqueSource, pBuffer, LARGE_BUFFER_SIZE ); prbData.buf = pBuffer; pInputFmt = av_probe_input_format( &prbData, 1 ); pDecoder->fpSeek( pDecoder->pOpaqueSource, 0, SEEK_SET ); pIoBuffer = (uint8_t*) av_mallocz( INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE ); pIoCtx = av_alloc_put_byte( pIoBuffer, INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE, 0, pDecoder->pOpaqueSource, pDecoder->fpRead, pDecoder->fpWrite, pDecoder->fpSeek ); if( pIoCtx == NULL ) { printf("Failed getting the byte io context \n"); goto error; } if( av_open_input_stream( &pDecoder->pFmtCtx, pIoCtx, "DUMMY", pInputFmt, NULL ) < 0 ) { error("Failed opening input stream"); goto error; } if( __load_codec_ctx( pDecoder ) < 0 ) { error("Failed loading the codec details"); goto error; } av_free( pBuffer ); pBuffer = NULL; return SUCCESS; error: av_free( pBuffer ); pBuffer = NULL; return ERROR; }
/** * Creates a new ByteIOContext. */ static ByteIOContext* aacd_ff_create_byteioctx( AACDFFmpegInfo *ff ) { int buffer_size = ff->cinfo->bbsize; unsigned char *buffer = av_mallocz( buffer_size ); ByteIOContext *pb = av_alloc_put_byte( buffer, buffer_size, 0, ff, aacd_ff_io_read_packet, NULL, NULL); if (!pb) { av_free( buffer ); AACD_WARN( "create_byteioctx(): ByteIOContext could not be created" ); } return pb; }
/**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */ static int rdt_parse_packet (AVFormatContext *ctx, PayloadContext *rdt, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, int flags) { int seq = 1, res; ByteIOContext pb; if (rdt->audio_pkt_cnt == 0) { int pos; init_put_byte(&pb, buf, len, 0, NULL, NULL, NULL, NULL); flags = (flags & RTP_FLAG_KEY) ? 2 : 0; res = ff_rm_parse_packet (rdt->rmctx, &pb, st, rdt->rmst[st->index], len, pkt, &seq, flags, *timestamp); pos = url_ftell(&pb); if (res < 0) return res; if (res > 0) { if (st->codec->codec_id == CODEC_ID_AAC) { memcpy (rdt->buffer, buf + pos, len - pos); rdt->rmctx->pb = av_alloc_put_byte (rdt->buffer, len - pos, 0, NULL, NULL, NULL, NULL); } goto get_cache; } } else { get_cache: rdt->audio_pkt_cnt = ff_rm_retrieve_cache (rdt->rmctx, rdt->rmctx->pb, st, rdt->rmst[st->index], pkt); if (rdt->audio_pkt_cnt == 0 && st->codec->codec_id == CODEC_ID_AAC) av_freep(&rdt->rmctx->pb); } pkt->stream_index = st->index; pkt->pts = *timestamp; return rdt->audio_pkt_cnt > 0; }
SPDIFEncoder::SPDIFEncoder(QString muxer, AVCodecContext *ctx) : m_complete(false), m_oc(NULL), m_stream(NULL), m_size(0) { QByteArray dev_ba = muxer.toAscii(); AVOutputFormat *fmt = av_guess_format(dev_ba.constData(), NULL, NULL); if (!(av_guess_format && avformat_alloc_context && av_new_stream && av_write_header && av_write_frame && av_write_trailer && av_set_parameters)) { VERBOSE(VB_IMPORTANT, LOC_ERR + "Couldn't find libavformat"); return; } if (!avcodec_close) { VERBOSE(VB_IMPORTANT, LOC_ERR + "Couldn't find libavcodec"); return; } if (!(av_freep && av_alloc_put_byte)) { VERBOSE(VB_IMPORTANT, LOC_ERR + "Couldn't find libavutil"); return; } if (!fmt) { VERBOSE(VB_AUDIO, LOC_ERR + "av_guess_format"); return; } m_oc = avformat_alloc_context(); if (!m_oc) { VERBOSE(VB_AUDIO, LOC_ERR + "avformat_alloc_context"); return; } m_oc->oformat = fmt; if (av_set_parameters(m_oc, NULL) < 0) { VERBOSE(VB_AUDIO, LOC_ERR + "av_set_parameters"); Destroy(); return; } m_oc->pb = av_alloc_put_byte(m_buffer, sizeof(m_buffer), URL_RDONLY, this, NULL, funcIO, NULL); if (!m_oc->pb) { VERBOSE(VB_AUDIO, LOC_ERR + "av_alloc_put_byte"); Destroy(); return; } m_oc->pb->is_streamed = true; m_oc->flags |= AVFMT_NOFILE | AVFMT_FLAG_IGNIDX; if (av_set_parameters(m_oc, NULL) != 0) { VERBOSE(VB_AUDIO, LOC_ERR + "av_set_parameters"); Destroy(); return; } m_stream = av_new_stream(m_oc, 1); if (!m_stream) { VERBOSE(VB_AUDIO, LOC_ERR + "av_new_stream"); Destroy(); return; } // copy without decoding or reencoding m_stream->stream_copy = true; AVCodecContext *codec = m_stream->codec; codec->codec_type = ctx->codec_type; codec->codec_id = ctx->codec_id; codec->sample_rate = ctx->sample_rate; codec->sample_fmt = ctx->sample_fmt; codec->channels = ctx->channels; codec->bit_rate = ctx->bit_rate; codec->extradata = new uint8_t[ctx->extradata_size]; codec->extradata_size = ctx->extradata_size; memcpy(codec->extradata, ctx->extradata, ctx->extradata_size); av_write_header(m_oc); VERBOSE(VB_AUDIO, LOC + QString("Creating %1 encoder (%2, %3Hz)") .arg(muxer).arg(ff_codec_id_string((CodecID)codec->codec_type)) .arg(codec->sample_rate)); m_complete = true; }
static demuxer_t* demux_open_lavf(demuxer_t *demuxer){ AVFormatContext *avfc; AVFormatParameters ap; const AVOption *opt; AVMetadataTag *t = NULL; lavf_priv_t *priv= demuxer->priv; int i; //start_time = 0.0; char mp_filename[256]="mp:"; memset(&ap, 0, sizeof(AVFormatParameters)); //demux_lavf_find_geodata(demuxer); stream_seek(demuxer->stream, 0); int filepos=stream_tell(demuxer->stream); avfc = avformat_alloc_context(); if (opt_cryptokey) parse_cryptokey(avfc, opt_cryptokey); if (user_correct_pts != 0) avfc->flags |= AVFMT_FLAG_GENPTS; /* if (index_mode == 0) */ /* avfc->flags |= AVFMT_FLAG_IGNIDX; */ ap.prealloced_context = 1; #if 0 if(opt_probesize) { opt = av_set_int(avfc, "probesize", opt_probesize); if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize); } if(opt_analyzeduration) { opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE); if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration); } if(opt_avopt){ if(parse_avopts(avfc, opt_avopt) < 0){ mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt); return NULL; } } #endif if(demuxer->stream->url) { if (!strncmp(demuxer->stream->url, "ffmpeg://rtsp:", 14)) av_strlcpy(mp_filename, demuxer->stream->url + 9, sizeof(mp_filename)); else av_strlcat(mp_filename, demuxer->stream->url, sizeof(mp_filename)); } else av_strlcat(mp_filename, "foobar.dummy", sizeof(mp_filename)); priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0, demuxer, mp_read, NULL, mp_seek); priv->pb->read_seek = mp_read_seek; priv->pb->is_streamed = !demuxer->stream->end_pos || (demuxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK; filepos=stream_tell(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; } filepos=stream_tell(demuxer->stream); 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; } filepos=stream_tell(demuxer->stream); if(!strncmp(avfc->iformat->name,"aac",4)) get_aac_duration(demuxer,avfc); if(!strncmp(avfc->iformat->name,"mp3",4)) priv->avfc->duration = get_mp3_duration(demuxer) * AV_TIME_BASE; /* Add metadata. */ av_metadata_conv(avfc, NULL, avfc->iformat->metadata_conv); while((t = av_metadata_get(avfc->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))){ demux_info_add(demuxer, t->key, t->value); } for(i=0; i < avfc->nb_chapters; i++) { AVChapter *c = avfc->chapters[i]; uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000}); uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000}); t = av_metadata_get(c->metadata, "title", NULL, 0); demuxer_add_chapter(demuxer, t ? t->value : NULL, start, end); } demuxer->ttaflag = 0; for(i=0; i<avfc->nb_streams; i++) handle_stream(demuxer, avfc, i); if(demuxer->matroflag && !demuxer->ttaflag) return NULL; if(avfc->nb_programs) { int p; for (p = 0; p < avfc->nb_programs; p++) { AVProgram *program = avfc->programs[p]; t = av_metadata_get(program->metadata, "title", NULL, 0); mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, t ? t->value : ""); } } 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; filepos=stream_tell(demuxer->stream); int j=0; AVCodecContext *codec; AVCodec *avc; demuxer->audio_info = malloc(100*avfc->nb_streams); for(i=0; i<avfc->nb_streams; i++){ if(demuxer->a_streams[i]){ char *info = malloc(100); codec= ((AVStream *)avfc->streams[i])->codec; avc = avcodec_find_decoder(codec->codec_id); char *cn = avc ? avc->name : "unknown"; char *codec_name = malloc(100); strncpy(codec_name,cn,100); if((codec_name[0]=='d')&&(codec_name[1]=='c')&&(codec_name[2]=='a')) strncpy(codec_name,"dts",100); int a; for(a=0;codec_name[a];a++) if(codec_name[a]>='a'&&codec_name[a]<='z') codec_name[a]-=32; sprintf(info, "%s(%dHz %dCh)--%d", codec_name,codec->sample_rate, codec->channels, i); free(codec_name); memcpy(demuxer->audio_info+j*100,info, 100); j++; free(info); info = NULL; } } j = 0; demuxer->sub_info = malloc(100*avfc->nb_streams); for(i=0; i<avfc->nb_streams; i++){ if(demuxer->s_streams[i]){ char *info = malloc(100); AVStream *st= avfc->streams[i]; AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL, 0); codec= st->codec; if (lang && lang->value) strcpy(info, lang->value); else strcpy(info, "unknown"); sprintf(info, "%s--%d", info,i); memcpy(demuxer->sub_info+j*100,info, 100); j++; free(info); info = NULL; } } if(AV_NOPTS_VALUE == priv->avfc->start_time){ LOGW("priv->avfc->start_time = AV_NOPTS_VALUE"); demuxer->start_time = 0; }else{ demuxer->start_time = priv->avfc->start_time; } return demuxer; }
static demuxer_t* demux_open_lavf(demuxer_t *demuxer){ AVFormatContext *avfc; AVFormatParameters ap; const AVOption *opt; AVMetadataTag *t = NULL; lavf_priv_t *priv= demuxer->priv; int i; char mp_filename[256]="mp:"; memset(&ap, 0, sizeof(AVFormatParameters)); stream_seek(demuxer->stream, 0); avfc = avformat_alloc_context(); if (opt_cryptokey) parse_cryptokey(avfc, opt_cryptokey); if (user_correct_pts != 0) avfc->flags |= AVFMT_FLAG_GENPTS; if (index_mode == 0) avfc->flags |= AVFMT_FLAG_IGNIDX; ap.prealloced_context = 1; if(opt_probesize) { opt = av_set_int(avfc, "probesize", opt_probesize); if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize); } if(opt_analyzeduration) { opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE); if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration); } if(opt_avopt){ if(parse_avopts(avfc, opt_avopt) < 0){ mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt); return NULL; } } if(demuxer->stream->url) { if (!strncmp(demuxer->stream->url, "ffmpeg://rtsp:", 14)) av_strlcpy(mp_filename, demuxer->stream->url + 9, sizeof(mp_filename)); else av_strlcat(mp_filename, demuxer->stream->url, sizeof(mp_filename)); } else av_strlcat(mp_filename, "foobar.dummy", sizeof(mp_filename)); if (!(priv->avif->flags & AVFMT_NOFILE)) { priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0, demuxer, mp_read, NULL, mp_seek); priv->pb->read_seek = mp_read_seek; priv->pb->is_streamed = !demuxer->stream->end_pos || (demuxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK; } 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; } /* Add metadata. */ while((t = av_metadata_get(avfc->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) demux_info_add(demuxer, t->key, t->value); for(i=0; i < avfc->nb_chapters; i++) { AVChapter *c = avfc->chapters[i]; uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000}); uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000}); t = av_metadata_get(c->metadata, "title", NULL, 0); demuxer_add_chapter(demuxer, t ? t->value : NULL, start, end); } for(i=0; i<avfc->nb_streams; i++) handle_stream(demuxer, avfc, i); priv->nb_streams_last = avfc->nb_streams; if(avfc->nb_programs) { int p; for (p = 0; p < avfc->nb_programs; p++) { AVProgram *program = avfc->programs[p]; t = av_metadata_get(program->metadata, "title", NULL, 0); mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, t ? t->value : ""); mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d\n", program->id); } } 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; }
int setupDemuxerFormat(DemuxerSettings *ds, char *file, struct hqv_data *hqv) { AVProbeData probe_data, *pd = &probe_data; ds->pFormatCtx = avformat_alloc_context(); ds->pFormatCtx->flags |= AVFMT_FLAG_NONBLOCK; ds->pFormatCtx->flags |= AVFMT_FLAG_GENPTS; #ifdef AVFMT_FLAG_IGNDTS ds->pFormatCtx->flags |= AVFMT_FLAG_IGNDTS; #endif if (ds->streaming) ds->pFormatCtx->flags |= AVFMT_NOFILE|AVFMT_FLAG_IGNIDX; // Setup input format for raw A/V if (ds->input_format != NULL) { ds->fmt = av_iformat_next(NULL); if (ds->fmt != NULL && ds->fmt->name != NULL && strcmp(ds->fmt->name, ds->input_format) != 0) { do { ds->fmt = av_iformat_next(ds->fmt); if (ds->fmt == NULL || ds->fmt->name == NULL) break; } while (strcmp(ds->fmt->name, ds->input_format) != 0); } if (ds->fmt) av_log(NULL, AV_LOG_INFO, "Input format: %s\n", ds->fmt->long_name); else av_log(NULL, AV_LOG_WARNING, "Failed finding input format: %s\n", ds->input_format); } if (ds->streaming) ds->stream_buffer = (uint8_t*)av_malloc(STREAM_BUFFER_SIZE); // Try to figure out input format if (ds->streaming && ds->fmt == NULL) { pd->filename = ""; if (file) pd->filename = file; pd->buf = ds->stream_buffer; pd->buf_size = STREAM_BUFFER_SIZE; // Wait till have enough input data while(av_fifo_size(hqv->fifo) < STREAM_BUFFER_SIZE && continueDecoding) usleep(33000); if (!continueDecoding) exit(0); // Copy some fifo data memcpy(ds->stream_buffer, hqv->fifo->buffer, STREAM_BUFFER_SIZE); // Probe input format ds->fmt = av_probe_input_format(pd, 1); if (!ds->fmt) { av_log(NULL, AV_LOG_FATAL, "Failed probing input file: %s\n", file); exit(1); } else av_log(NULL, AV_LOG_INFO, "Input format: %s\n", ds->fmt->long_name); } // Open Input File if (ds->streaming) { // Streaming input ds->pb = av_alloc_put_byte(ds->stream_buffer, STREAM_BUFFER_SIZE, 0, hqv, fifo_read, NULL, NULL); ds->pb->is_streamed = 1; // Open video device stream if (av_open_input_stream(&ds->pFormatCtx, ds->pb, file, ds->fmt, ds->ap) != 0) { av_log(NULL, AV_LOG_FATAL, "%s: could not open device stream\n", file); return -1; // Couldn't open file } } else { // Open file if (av_open_input_file(&ds->pFormatCtx, file, ds->fmt, 0, ds->ap) != 0) { av_log(NULL, AV_LOG_FATAL, "%s: could not openfile\n", file); return -1; // Couldn't open file } } // Retrieve stream information if (av_find_stream_info(ds->pFormatCtx) < 0) { av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", file); return -1; } return 0; }