[Libav-user] Error in reading streaming context - using libavcodec 57.64.101
יוסף אלון
yos104104 at gmail.com
Tue Apr 4 13:54:56 EEST 2017
you right (I tried to make it simpler) this is the code:
#include "libavformat/avformat.h"
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/rational.h>
#include <libavutil/samplefmt.h>
#include "libswresample/swresample.h"
#include <stdio.h>
#include <assert.h>
#define AVCODEC_MAX_AUDIO_FRAME_SIZE 19200
void die(const char *msg)
{
fprintf(stderr,"%s\n",msg);
exit(1);
}
int main(int argc, char** argv)
{
av_register_all();
avformat_network_init();
const char* input_filename=argv[1];
AVFormatContext* container=avformat_alloc_context();
if(avformat_open_input(&container,input_filename,NULL,NULL)<0)
die("Could not open file");
if(avformat_find_stream_info(container,NULL)<0)
die("Could not find file info");
av_dump_format(container,0,input_filename,0);
int stream_id=-1;
int i;
for(i=0;i<container->nb_streams;i++)
{
if(container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
{
stream_id=i;//audio stream index
break;
}
}
if(stream_id==-1){
die("Could not find Audio Stream");
}
AVDictionary *metadata=container->metadata;
AVCodecContext *ctx=container->streams[stream_id]->codec;
AVCodec *codec=avcodec_find_decoder(ctx->codec_id);
ctx=avcodec_alloc_context3(codec);
if(codec==NULL){
die("cannot find codec!");
}
if(avcodec_open2(ctx,codec, NULL)<0){
die("Codec cannot be found");
}
int bits;
enum AVSampleFormat sfmt=ctx->sample_fmt;
if(sfmt==AV_SAMPLE_FMT_U8){
printf("U8\n");
bits=8;
}else if(sfmt==AV_SAMPLE_FMT_S16){
printf("S16\n");
bits=16;
}else if(sfmt==AV_SAMPLE_FMT_S32){
printf("S32\n");
bits=32;
}
else if(sfmt==AV_SAMPLE_FMT_FLTP){
bits=32;
}
printf("\nBits: %d, Channels:%d\nRate: %d\n",bits, ctx->channels,
ctx->sample_rate );
// prepare to read data
AVPacket packet;
av_init_packet(&packet);
AVFrame *frame=av_frame_alloc();
if (!frame)
{
fprintf(stderr, "Error allocating the frame\n");
return -1;
}
int buffer_size=AVCODEC_MAX_AUDIO_FRAME_SIZE+
FF_INPUT_BUFFER_PADDING_SIZE;
uint8_t buffer[buffer_size];
packet.data=buffer;
packet.size =buffer_size;
packet.pos = 0; //add from
https://lists.ffmpeg.org/pipermail/ffmpeg-user/2013-August/017040.html
int len;
int frameFinished=0;
i = 0;
while(av_read_frame(container,&packet)>=0)
{
if(packet.stream_index==stream_id){
//printf("Audio Frame read \n");
int len=avcodec_decode_audio4(ctx,frame,&frameFinished,&packet);
//frame->
if(frameFinished){
printf("Finished reading Frame len : %d , nb_samples:%d
buffer_size:%d line size: %d
\n",len,frame->nb_samples,buffer_size,frame->linesize[0]);
if (i == 0)
{
printf("play\n");
i++;
}
}else
{
printf("Not Finished\n");
}
}else {
printf("Some other packet possibly Video\n");
}
}
avformat_close_input(&container);
return 0;
}
2017-04-04 13:35 GMT+03:00 Carl Eugen Hoyos <ceffmpeg at gmail.com>:
> 2017-04-04 12:15 GMT+02:00 יוסף אלון <yos104104 at gmail.com>:
> >
> > I am reading a udp data and recieve:
> >
> > Input #0, mpegts, from 'udp://224.10.0.15:1234':
> > Duration: N/A, start: 66502.042356, bitrate: N/A
> > Program 29
> > Metadata:
> > service_name : Galatz
> > service_provider: Idan +
> > Stream #0:0[0xb81]: Audio: aac_latm (HE-AAC) ([17][0][0][0] /
> 0x0011),
> > 48000 Hz, stereo, fltp
> >
> > But when I print the codec context I receive the following data:
> > Bits: 8, Channels:2048
> > sample_rate: 8.
>
> The code you posted looks different from this output.
>
> Carl Eugen
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
>
--
בברכה, יוסף אלון
050-4916740
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20170404/ac32d11f/attachment.html>
More information about the Libav-user
mailing list