Exemple #1
0
AUD_FFMPEGReader::AUD_FFMPEGReader(AUD_Reference<AUD_Buffer> buffer) :
		m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
		m_membuffer(buffer),
		m_membufferpos(0)
{
	m_membuf = reinterpret_cast<data_t*>(av_malloc(FF_MIN_BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE));

	m_aviocontext = avio_alloc_context(m_membuf, FF_MIN_BUFFER_SIZE, 0, this,
									   read_packet, NULL, seek_packet);

	if(!m_aviocontext)
	{
		av_free(m_aviocontext);
		AUD_THROW(AUD_ERROR_FILE, fileopen_error);
	}

	m_formatCtx = avformat_alloc_context();
	m_formatCtx->pb = m_aviocontext;
	if(avformat_open_input(&m_formatCtx, "", NULL, NULL)!=0)
	{
		av_free(m_aviocontext);
		AUD_THROW(AUD_ERROR_FILE, streamopen_error);
	}

	try
	{
		init();
	}
	catch(AUD_Exception&)
	{
		av_close_input_stream(m_formatCtx);
		av_free(m_aviocontext);
		throw;
	}
}
FFmpegMeta::~FFmpegMeta()
{
    if (_pFormatContext) {
        if (_useAvOpenInputStream) {
            LOG(ffmpeg, trace, "ffmpeg::av_close_input_stream() ...");
            // free AVFormatContext
            av_close_input_stream(_pFormatContext);
        }
        else {
            LOG(ffmpeg, trace, "ffmpeg::av_close_input_file() ...");
            av_close_input_file(_pFormatContext);
        }
    }
    if (_pInputFormat) {
//         LOG(ffmpeg, trace, "ffmpeg::av_freep(AVInputFormat*) ...");
//         av_freep(_pInputFormat);
    }
    if (_pIoContext) {
//         LOG(ffmpeg, trace, "ffmpeg::av_freep(ByteIOContext*) ...");
//         av_freep(_pIoContext);
    }
    if (_pIoBuffer) {
        delete _pIoBuffer;
        _pIoBuffer = 0;
    }
}
/**
 * Destroys our context.
 */
static void aacd_ff_destroy( AACDFFmpegInfo *ff )
{
    if ( !ff ) return;

    AACD_TRACE( "destroy() start" );

    AVFormatContext *ic = ff->avfctx;

    if ( ic )
    {
        if ( ff->audio_stream_index > -1) avcodec_close( ic->streams[ff->audio_stream_index]->codec );

        ByteIOContext *pb = ic->pb;

        av_close_input_stream( ff->avfctx );

        if ( pb ) aacd_ff_destroy_byteioctx( pb );
    }

    if (ff->avpkt) av_free( ff->avpkt );
    if (ff->pkt) av_free( ff->pkt );

    av_free( ff );

    AACD_TRACE( "destroy() stop" );
}
Exemple #4
0
void
fa_libav_close_format(AVFormatContext *fctx)
{
  AVIOContext *avio = fctx->pb;
  av_close_input_stream(fctx);
  fa_libav_close(avio);
}
Exemple #5
0
AVFormatContext *
fa_libav_open_format(AVIOContext *avio, const char *url, 
		     char *errbuf, size_t errlen)
{
  AVInputFormat *fmt = NULL;
  AVFormatContext *fctx;
  int err;

  avio_seek(avio, 0, SEEK_SET);

  if((err = av_probe_input_buffer(avio, &fmt, url, NULL, 0, 0)) != 0)
    return fa_libav_open_error(errbuf, errlen,
			       "Unable to probe file", err);

  if(fmt == NULL) {
    snprintf(errbuf, errlen, "Unknown file format");
    return NULL;
  }

  if((err = av_open_input_stream(&fctx, avio, url, fmt, NULL)) != 0)
    return fa_libav_open_error(errbuf, errlen,
			       "Unable to open file as input format", err);

  if(av_find_stream_info(fctx) < 0) {
    av_close_input_stream(fctx);
    return fa_libav_open_error(errbuf, errlen,
			       "Unable to handle file contents", err);
  }

  return fctx;
}
Exemple #6
0
int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
{
    int ret = 0;
    if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
        ByteIOContext pb;
        RTSPState *rt = s->priv_data;
        int len = strlen(p) * 6 / 8;
        char *buf = av_mallocz(len);
        av_base64_decode(buf, p, len);

        if (rtp_asf_fix_header(buf, len) < 0)
            av_log(s, AV_LOG_ERROR,
                   "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
        init_packetizer(&pb, buf, len);
        if (rt->asf_ctx) {
            av_close_input_stream(rt->asf_ctx);
            rt->asf_ctx = NULL;
        }
        ret = av_open_input_stream(&rt->asf_ctx, &pb, "", &asf_demuxer, NULL);
        if (ret < 0)
            return ret;
        av_metadata_copy(&s->metadata, rt->asf_ctx->metadata, 0);
        rt->asf_pb_pos = url_ftell(&pb);
        av_free(buf);
        rt->asf_ctx->pb = NULL;
    }
    return ret;
}
Exemple #7
0
AVFormatContext *
fa_libav_open_format(AVIOContext *avio, const char *url, 
		     char *errbuf, size_t errlen, const char *mimetype)
{
  AVInputFormat *fmt = NULL;
  AVFormatContext *fctx;
  int err;

  avio_seek(avio, 0, SEEK_SET);
  if(mimetype != NULL) {
    int i;

    for(i = 0; i < sizeof(mimetype2fmt) / sizeof(mimetype2fmt[0]); i++) {
      if(!strcmp(mimetype, mimetype2fmt[i].mimetype)) {
	fmt = av_find_input_format(mimetype2fmt[i].fmt);
	break;
      }
    }
    if(fmt == NULL)
      TRACE(TRACE_DEBUG, "probe", "Don't know mimetype %s, probing instead",
	    mimetype);
  }

  if(fmt == NULL) {

    if((err = av_probe_input_buffer(avio, &fmt, url, NULL, 0, 0)) != 0)
      return fa_libav_open_error(errbuf, errlen,
				 "Unable to probe file", err);

    if(fmt == NULL) {
      snprintf(errbuf, errlen, "Unknown file format");
      return NULL;
    }
  }

  fctx = avformat_alloc_context();
  fctx->pb = avio;

  if((err = avformat_open_input(&fctx, url, fmt, NULL)) != 0) {
    if(mimetype != NULL)
      return fa_libav_open_format(avio, url, errbuf, errlen, NULL);
    return fa_libav_open_error(errbuf, errlen,
			       "Unable to open file as input format", err);
  }

  if(av_find_stream_info(fctx) < 0) {
    av_close_input_stream(fctx);
    if(mimetype != NULL)
      return fa_libav_open_format(avio, url, errbuf, errlen, NULL);
    return fa_libav_open_error(errbuf, errlen,
			       "Unable to handle file contents", err);
  }

  return fctx;
}
Exemple #8
0
static int sap_read_close(AVFormatContext *s)
{
    struct SAPState *sap = s->priv_data;
    if (sap->sdp_ctx)
        av_close_input_stream(sap->sdp_ctx);
    if (sap->ann_fd)
        url_close(sap->ann_fd);
    av_freep(&sap->sdp);
    ff_network_close();
    return 0;
}
Exemple #9
0
AUD_FFMPEGReader::~AUD_FFMPEGReader()
{
	avcodec_close(m_codecCtx);

	if(m_aviocontext)
	{
		av_close_input_stream(m_formatCtx);
		av_free(m_aviocontext);
	}
	else
		av_close_input_file(m_formatCtx);
}
Exemple #10
0
static void demux_close_lavf(demuxer_t *demuxer)
{
    lavf_priv_t* priv = demuxer->priv;
    if (priv){
        if(priv->avfc)
        {
         av_freep(&priv->avfc->key);
         av_close_input_stream(priv->avfc);
        }
        av_freep(&priv->pb);
        free(priv); demuxer->priv= NULL;
    }
}
Exemple #11
0
void freeDemuxer(DemuxerSettings *ds) {
	if (ds->do_video && ds->pCodecCtxVideo)
		avcodec_close(ds->pCodecCtxVideo);
	if (ds->do_audio && ds->pCodecCtxAudio)
		avcodec_close(ds->pCodecCtxAudio);
	if (ds->do_sub && ds->pCodecCtxSub)
		avcodec_close(ds->pCodecCtxSub);
	if (ds->streaming) {
		av_free(ds->stream_buffer);
		av_close_input_stream(ds->pFormatCtx);
		av_freep(&ds->pb);
	} else
		av_close_input_file(ds->pFormatCtx);
}
Exemple #12
0
static void
rdt_free_context (PayloadContext *rdt)
{
    int i;

    for (i = 0; i < MAX_STREAMS; i++)
        if (rdt->rmst[i]) {
            ff_rm_free_rmstream(rdt->rmst[i]);
            av_freep(&rdt->rmst[i]);
        }
    if (rdt->rmctx)
        av_close_input_stream(rdt->rmctx);
    av_freep(&rdt->mlti_data);
    av_free(rdt);
}
Exemple #13
0
static void FFMPG_infile_close(struct mpxplay_filehand_buffered_func_s *fbfs,void *fbds,struct mpxplay_infile_info_s *miis)
{
 struct ffmpg_demuxer_data_s *ffmpi=miis->private_data;
#ifdef INFFMPG_DEBUG_HEADER
 mpxplay_debugf(MPXPLAY_DEBUG_OUTPUT,"ffmpg infile close %8.8X",(long)ffmpi);
#endif
 if(ffmpi){
  av_close_input_stream(&ffmpi->fctx);
  if(ffmpi->pkt.data)
   free(ffmpi->pkt.data);
  free(ffmpi);
 }
 fbfs->fclose(fbds);
#ifdef INFFMPG_DEBUG_HEADER
 mpxplay_debugf(MPXPLAY_DEBUG_OUTPUT,"ffmpg infile close end");
#endif
}
Exemple #14
0
static int read_header(AVFormatContext *s,
                       AVFormatParameters *ap)
{
    AnmDemuxContext *anm = s->priv_data;
    ByteIOContext *pb = s->pb;
    AVStream *st;
    int i, ret;

    url_fskip(pb, 4); /* magic number */
    if (get_le16(pb) != MAX_PAGES) {
        av_log_ask_for_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES) "\n");
        return AVERROR_INVALIDDATA;
    }

    anm->nb_pages   = get_le16(pb);
    anm->nb_records = get_le32(pb);
    url_fskip(pb, 2); /* max records per page */
    anm->page_table_offset = get_le16(pb);
    if (get_le32(pb) != ANIM_TAG)
        return AVERROR_INVALIDDATA;

    /* video stream */
    st = av_new_stream(s, 0);
    if (!st)
        return AVERROR(ENOMEM);
    st->codec->codec_type = CODEC_TYPE_VIDEO;
    st->codec->codec_id   = CODEC_ID_ANM;
    st->codec->codec_tag  = 0; /* no fourcc */
    st->codec->width      = get_le16(pb);
    st->codec->height     = get_le16(pb);
    if (get_byte(pb) != 0)
        goto invalid;
    url_fskip(pb, 1); /* frame rate multiplier info */

    /* ignore last delta record (used for looping) */
    if (get_byte(pb))  /* has_last_delta */
        anm->nb_records = FFMAX(anm->nb_records - 1, 0);

    url_fskip(pb, 1); /* last_delta_valid */

    if (get_byte(pb) != 0)
        goto invalid;

    if (get_byte(pb) != 1)
        goto invalid;

    url_fskip(pb, 1); /* other recs per frame */

    if (get_byte(pb) != 1)
        goto invalid;

    url_fskip(pb, 32); /* record_types */
    st->nb_frames = get_le32(pb);
    av_set_pts_info(st, 64, 1, get_le16(pb));
    url_fskip(pb, 58);

    /* color cycling and palette data */
    st->codec->extradata_size = 16*8 + 4*256;
    st->codec->extradata      = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
    if (!st->codec->extradata) {
        ret = AVERROR(ENOMEM);
        goto close_and_return;
    }
    ret = get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
    if (ret < 0)
        goto close_and_return;

    /* read page table */
    ret = url_fseek(pb, anm->page_table_offset, SEEK_SET);
    if (ret < 0)
        goto close_and_return;

    for (i = 0; i < MAX_PAGES; i++) {
        Page *p = &anm->pt[i];
        p->base_record = get_le16(pb);
        p->nb_records  = get_le16(pb);
        p->size        = get_le16(pb);
    }

    /* find page of first frame */
    anm->page = find_record(anm, 0);
    if (anm->page < 0) {
        ret = anm->page;
        goto close_and_return;
    }

    anm->record = -1;
    return 0;

invalid:
    av_log_ask_for_sample(s, NULL);
    ret = AVERROR_INVALIDDATA;

close_and_return:
    av_close_input_stream(s);
    return ret;
}
Exemple #15
0
static void ffmpeg_capture_close(AVFormatContext *ctx)
{
    if (ctx)
        av_close_input_stream(ctx);
}
int slimaudio_decoder_aac_process(slimaudio_t *audio) {
	char streamformat[16];
	int out_size;
	int len = 0;
	int iRC;
	u8_t *outbuf;
	u8_t *inbuf;

	/* It is not really correct to assume that all MP4 files (which were not
	 * otherwise recognized as ALAC or MOV by the scanner) are AAC, but that
	 * is the current server side status.
	 *
	 * Container type and bitstream format:
	 *
	 * '1' (adif),
	 * '2' (adts),
	 * '3' (latm within loas),
	 * '4' (rawpkts),
	 * '5' (mp4ff),
	 * '6' (latm within rawpkts)
	 * 
	 * This is a hack that assumes:
	 * (1) If the original content-type of the track is MP4 or SLS then we
	 *     are streaming an MP4 file (without any transcoding);
	 * (2) All other AAC streams will be adts.
	 *
	 * So the server will only set aac_format to '2' or '5'.
	 */

	DEBUGF ("aac: decoder_format:%d '%c'\n", audio->aac_format, audio->aac_format);

	int audioStream = 0; /* Always zero for aac decoder */

	switch ( audio->aac_format )
	{
		case '2':
			strncpy ( streamformat, "aac", sizeof (streamformat) );
			break;
		case '5':
			strncpy ( streamformat, "m4a", sizeof (streamformat) );
			break;
		default:
			fprintf (stderr, "aac: unknown container type: %c\n" ,audio->aac_format );
			return -1;
	}

	DEBUGF ("aac: play audioStream: %d\n", audioStream);

	inbuf = av_malloc(AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
	if ( !inbuf )
	{
		DEBUGF("aac: inbuf alloc failed.\n");
		return -1;
	}

	AVIOContext *AVIOCtx;

	AVIOCtx = avio_alloc_context(inbuf, AUDIO_CHUNK_SIZE, 0, audio, av_read_data, NULL, NULL);
	if ( AVIOCtx == NULL )
	{
		DEBUGF("aac: avio_alloc_context failed.\n");
		return -1;
	}
	else
	{
		AVIOCtx->is_streamed = 1;
	}

	AVInputFormat* pAVInputFormat = av_find_input_format(streamformat);
	if( !pAVInputFormat )
	{
		DEBUGF("aac: probe failed\n");
		return -1;
	}
	else
	{
		DEBUGF("aac: probe ok name:%s lname:%s\n", pAVInputFormat->name, pAVInputFormat->long_name);
	}

	AVFormatContext *pFormatCtx;

	pFormatCtx = avformat_alloc_context();
	if( pFormatCtx == NULL ) {
		DEBUGF("aac: avformat_alloc_context failed.\n");
	}
	else {
		pFormatCtx->pb = AVIOCtx;
	}

	AVCodecContext *pCodecCtx;
	
	iRC = avformat_open_input(&pFormatCtx, "", pAVInputFormat, NULL);

	if (iRC < 0)
	{
		DEBUGF("aac: input stream open failed:%d\n", iRC);
		return -1;
	}
	else
	{
		iRC = av_find_stream_info(pFormatCtx);
		if ( iRC < 0 )
		{
			DEBUGF("aac: find stream info failed:%d\n", iRC);
			return -1;
		}
		else
		{
			if ( pFormatCtx->nb_streams < audioStream )
			{
				DEBUGF("aac: invalid stream.\n");
				return -1;
			}

			if ( pFormatCtx->streams[audioStream]->codec->codec_type != CODEC_TYPE_AUDIO )
			{
				DEBUGF("aac: stream: %d is not audio.\n", audioStream );
				return -1;
			}
			else
			{
				pCodecCtx = pFormatCtx->streams[audioStream]->codec;
			}
		}
	}

	AVCodec *pCodec;

	/* Find the WMA audio decoder */
	pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
	if ( !pCodec )
	{
		DEBUGF("aac: codec not found.\n");
		return -1;
	} 
	
	/* Open codec */
	iRC = avcodec_open(pCodecCtx, pCodec);
	if ( iRC < 0)
	{
		DEBUGF("aac: could not open codec:%d\n", iRC);
		return -1;
	}

	outbuf = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
	if ( !outbuf )
	{
		DEBUGF("aac: outbuf alloc failed.\n");
		return -1;
	}

	bool eos = false;
	AVPacket avpkt;

	while ( ! eos )
	{
		iRC = av_read_frame (pFormatCtx, &avpkt);

		/* Some decoders fail to read the last packet so additional handling is required */
	        if (iRC < 0)
		{
			DEBUGF("aac: av_read_frame error: %d\n", iRC);

			if ( (iRC == AVERROR_EOF) )
			{
				DEBUGF("aac: AVERROR_EOF\n");
				eos=true;
			}

			if ( pFormatCtx->pb->eof_reached )
			{
				DEBUGF("aac: url_feof\n");
				eos=true;
			}

			if ( url_ferror(pFormatCtx->pb) )
			{
				DEBUGF("aac: url_ferror\n");
#if 0
		                break;
#endif
			}
		}

		out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
		len = avcodec_decode_audio3(pCodecCtx, (int16_t *)outbuf, &out_size, &avpkt);
		if (len < 0)
		{
			DEBUGF("aac: no audio to decode\n");
			//av_free_packet (&avpkt);
			//break;
		}

		if (out_size > 0)
		{
			/* if a frame has been decoded, output it */
			slimaudio_buffer_write(audio->output_buffer, (char*)outbuf, out_size);
		}

		av_free_packet (&avpkt);
	}

	if ( inbuf != NULL )
		av_free(inbuf);

	if ( outbuf != NULL )
		av_free(outbuf);

	DEBUGF ("aac: avcodec_close\n");
	avcodec_close(pCodecCtx);

	/* Close the stream */
	DEBUGF ("aac: av_close_input_stream\n");
	av_close_input_stream(pFormatCtx);

	return 0;
}