AVcodec
Posted: 07 Jan 2008 09:57
hi friends
i am new to this forum
i got one problem in avcodec
i am using avcodec libraries for decompression of mpeg files and play back it
here my problem is i want to read the compressed file from buffer ,but i am now reading from the hard disk
if we give buffer address for decompression it is not coming
but if we give file name (which is in hard disk) which contains extension the it is executing
since the buffer does't have any extension so it is not taking
i want to take from buffer
this is my problem
here i am givin sample code for understanding
here guess_format function is the problem i think,since buffer does't have any extension
code:
Decompression("test.M4V");//here i want 2 give buffer name but not the file as it is showing
and the function is as follows
int Decompression(char *inputfile)
{
const char *filename;
AVOutputFormat *fmt;
AVFormatContext *oc;
AVStream *video_st;
double video_pts;
unsigned int i = 0;
AVFormatContext *pFormatCtx;
int videoStream = 0;
AVCodecContext *pCodecCtx;
AVFrame *pFrame;
AVCodec *pCodec;
AVPacket packet;
int frameFinished;
const char *filenamei;
AVOutputFormat *fmt1;
// initialize libavcodec, and register all codecs and formats */
av_register_all();
filename = "Girish1.YUV";
filenamei = inputfile;
// auto detect the output format from the name. default is mpeg. */
fmt = guess_format(NULL, filename, NULL);
if (!fmt)
{
printf("Could not deduce output format from file extension: using MPEG.\n");
fmt = guess_format("mpeg", NULL, NULL);
}
if (!fmt)
{
fprintf(stderr, "Could not find suitable output format\n");
exit(1);
}
fmt1 = guess_format(NULL, filenamei, NULL);
if (!fmt1)
{
printf("Could not deduce input format from file extension: using MPEG.\n");
fmt1 = guess_format("mpeg", NULL, NULL);
}
if (!fmt1)
{
fprintf(stderr, "Could not find suitable input format\n");
exit(1);
}
// Open video file input
if(av_open_input_file(&pFormatCtx, filenamei, NULL, 10000, NULL)!=0)
{
printf("Couldn't open input file\n");
return NULL; // Couldn't open file
}
// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
{
printf("Couldn't find stream information\n");
return NULL; // Couldn't open file
} // Couldn't find stream information
// Dump information about file onto standard error
dump_format(pFormatCtx, 0, filenamei, false);
// Find the first video stream
videoStream=-1;
for(i=0; i < (pFormatCtx->nb_streams); i++)
if(pFormatCtx->streams->codec->codec_type == CODEC_TYPE_VIDEO)
{
videoStream=i;
break;
}
if(videoStream==-1)
{
printf("Didn't find a video stream\n");
return NULL; // Didn't find a video stream
}
// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
pCodecCtx->width = RESW;
pCodecCtx->height =RESH;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{
printf("Codec not found\n");
return NULL; // Codec not found
}
// bitstreams where frame boundaries can fall in the middle of packets
/* if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
{
pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
}*/
// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
{
printf("Could not open codec\n");
return NULL; // Could not open codec
}
// Allocate video frame
/*pFrame = alloc_picture(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
if (!pFrame)
{
fprintf(stderr, "Could not allocate picture\n");
exit(1);
}*/
// allocate the output format context */
oc = av_alloc_format_context();
if (!oc)
{
fprintf(stderr, "Memory error\n");
exit(1);
}
oc->oformat = fmt;
_snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
// add the video streams using the default format codecs and initialize the codecs
video_st = NULL;
if (fmt->video_codec != CODEC_ID_NONE)
{
video_st = add_video_stream(oc, fmt->video_codec);
}
// set the output parameters
if (av_set_parameters(oc, NULL) < 0)
{
fprintf(stderr, "Invalid output format parameters\n");
exit(1);
}
dump_format(oc, 0, filename, 1);
// now that all the parameters are set, we can open the video codecs and allocate the necessary encode buffers
if (video_st)
{
if(!(open_video(oc, video_st)))
{
printf("\nfailed to open videostream for output");
exit(1);
}
}
// open the output file, if needed */
if (!(fmt->flags & AVFMT_NOFILE))
{
if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
{
fprintf(stderr, "Could not open '%s'\n", filename);
exit(1);
}
}
// write the stream header, if any
if((av_write_header(oc)) != 0)
{
printf("\nfailed to write the header");
}
for(;;k++)
{
// compute current video time
if (video_st)
{
video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
}
else
{
video_pts = 0.0;
}
pFrame = alloc_picture(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
if (!pFrame)
{
fprintf(stderr, "Could not allocate picture\n");
exit(1);
}
// write interleaved video frames */
if(av_read_frame(pFormatCtx, &packet)>=0)
{
if(packet.stream_index==videoStream)
{
// Decode video frame
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, packet.data, packet.size);
video_outbuf_size1 = 2000000; //by me
video_outbuf1 = (unsigned char *) av_malloc(video_outbuf_size1);
if(not_init == false)
{
not_init = true;
InitConvertTable();
}
// ConvertYUVtoYUV(320,240,(unsigned int*)pFrame,(unsigned int*)pFrame);
ConvertYUV2RGB((unsigned char*)pFrame->data[0],(unsigned char*)pFrame->data[1],(unsigned char*)pFrame->data[2],video_outbuf1,RESW,RESH);
PostMessage(g_hWnd,ON_RENDER,(unsigned long)video_outbuf1, NULL);
//Sleep(100);
// Did we get a video frame?
if(frameFinished)
{
if(!(write_video_frame(oc, video_st,pFrame,i)))
{
printf("\nfailed to write the video frame");
exit(1);
}
av_free_packet(&packet);
av_free(pFrame);
}
}
}
else
break;
}
//av_free_packet(&packet);
//av_free(pFrame);
// close each codec */
if (video_st)
{
close_video(oc, video_st);
}
// write the trailer, if any */
if((av_write_trailer(oc)) != 0)
{
printf("\nfailed to write the trailer");
}
// free the streams */
for(i = 0; i < oc->nb_streams; i++)
{
av_freep(&oc->streams->codec);
av_freep(&oc->streams);
}
if (!(fmt->flags & AVFMT_NOFILE))
{
// close the output file */
url_fclose(&oc->pb);
}
// free the stream */
av_free(oc);
av_free(video_outbuf1);
// Close the codec
avcodec_close(pCodecCtx);
// Close the video file
av_close_input_file(pFormatCtx);
//free(p);
return 0;
}
i am new to this forum
i got one problem in avcodec
i am using avcodec libraries for decompression of mpeg files and play back it
here my problem is i want to read the compressed file from buffer ,but i am now reading from the hard disk
if we give buffer address for decompression it is not coming
but if we give file name (which is in hard disk) which contains extension the it is executing
since the buffer does't have any extension so it is not taking
i want to take from buffer
this is my problem
here i am givin sample code for understanding
here guess_format function is the problem i think,since buffer does't have any extension
code:
Decompression("test.M4V");//here i want 2 give buffer name but not the file as it is showing
and the function is as follows
int Decompression(char *inputfile)
{
const char *filename;
AVOutputFormat *fmt;
AVFormatContext *oc;
AVStream *video_st;
double video_pts;
unsigned int i = 0;
AVFormatContext *pFormatCtx;
int videoStream = 0;
AVCodecContext *pCodecCtx;
AVFrame *pFrame;
AVCodec *pCodec;
AVPacket packet;
int frameFinished;
const char *filenamei;
AVOutputFormat *fmt1;
// initialize libavcodec, and register all codecs and formats */
av_register_all();
filename = "Girish1.YUV";
filenamei = inputfile;
// auto detect the output format from the name. default is mpeg. */
fmt = guess_format(NULL, filename, NULL);
if (!fmt)
{
printf("Could not deduce output format from file extension: using MPEG.\n");
fmt = guess_format("mpeg", NULL, NULL);
}
if (!fmt)
{
fprintf(stderr, "Could not find suitable output format\n");
exit(1);
}
fmt1 = guess_format(NULL, filenamei, NULL);
if (!fmt1)
{
printf("Could not deduce input format from file extension: using MPEG.\n");
fmt1 = guess_format("mpeg", NULL, NULL);
}
if (!fmt1)
{
fprintf(stderr, "Could not find suitable input format\n");
exit(1);
}
// Open video file input
if(av_open_input_file(&pFormatCtx, filenamei, NULL, 10000, NULL)!=0)
{
printf("Couldn't open input file\n");
return NULL; // Couldn't open file
}
// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
{
printf("Couldn't find stream information\n");
return NULL; // Couldn't open file
} // Couldn't find stream information
// Dump information about file onto standard error
dump_format(pFormatCtx, 0, filenamei, false);
// Find the first video stream
videoStream=-1;
for(i=0; i < (pFormatCtx->nb_streams); i++)
if(pFormatCtx->streams->codec->codec_type == CODEC_TYPE_VIDEO)
{
videoStream=i;
break;
}
if(videoStream==-1)
{
printf("Didn't find a video stream\n");
return NULL; // Didn't find a video stream
}
// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
pCodecCtx->width = RESW;
pCodecCtx->height =RESH;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{
printf("Codec not found\n");
return NULL; // Codec not found
}
// bitstreams where frame boundaries can fall in the middle of packets
/* if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
{
pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
}*/
// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
{
printf("Could not open codec\n");
return NULL; // Could not open codec
}
// Allocate video frame
/*pFrame = alloc_picture(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
if (!pFrame)
{
fprintf(stderr, "Could not allocate picture\n");
exit(1);
}*/
// allocate the output format context */
oc = av_alloc_format_context();
if (!oc)
{
fprintf(stderr, "Memory error\n");
exit(1);
}
oc->oformat = fmt;
_snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
// add the video streams using the default format codecs and initialize the codecs
video_st = NULL;
if (fmt->video_codec != CODEC_ID_NONE)
{
video_st = add_video_stream(oc, fmt->video_codec);
}
// set the output parameters
if (av_set_parameters(oc, NULL) < 0)
{
fprintf(stderr, "Invalid output format parameters\n");
exit(1);
}
dump_format(oc, 0, filename, 1);
// now that all the parameters are set, we can open the video codecs and allocate the necessary encode buffers
if (video_st)
{
if(!(open_video(oc, video_st)))
{
printf("\nfailed to open videostream for output");
exit(1);
}
}
// open the output file, if needed */
if (!(fmt->flags & AVFMT_NOFILE))
{
if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0)
{
fprintf(stderr, "Could not open '%s'\n", filename);
exit(1);
}
}
// write the stream header, if any
if((av_write_header(oc)) != 0)
{
printf("\nfailed to write the header");
}
for(;;k++)
{
// compute current video time
if (video_st)
{
video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
}
else
{
video_pts = 0.0;
}
pFrame = alloc_picture(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
if (!pFrame)
{
fprintf(stderr, "Could not allocate picture\n");
exit(1);
}
// write interleaved video frames */
if(av_read_frame(pFormatCtx, &packet)>=0)
{
if(packet.stream_index==videoStream)
{
// Decode video frame
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, packet.data, packet.size);
video_outbuf_size1 = 2000000; //by me
video_outbuf1 = (unsigned char *) av_malloc(video_outbuf_size1);
if(not_init == false)
{
not_init = true;
InitConvertTable();
}
// ConvertYUVtoYUV(320,240,(unsigned int*)pFrame,(unsigned int*)pFrame);
ConvertYUV2RGB((unsigned char*)pFrame->data[0],(unsigned char*)pFrame->data[1],(unsigned char*)pFrame->data[2],video_outbuf1,RESW,RESH);
PostMessage(g_hWnd,ON_RENDER,(unsigned long)video_outbuf1, NULL);
//Sleep(100);
// Did we get a video frame?
if(frameFinished)
{
if(!(write_video_frame(oc, video_st,pFrame,i)))
{
printf("\nfailed to write the video frame");
exit(1);
}
av_free_packet(&packet);
av_free(pFrame);
}
}
}
else
break;
}
//av_free_packet(&packet);
//av_free(pFrame);
// close each codec */
if (video_st)
{
close_video(oc, video_st);
}
// write the trailer, if any */
if((av_write_trailer(oc)) != 0)
{
printf("\nfailed to write the trailer");
}
// free the streams */
for(i = 0; i < oc->nb_streams; i++)
{
av_freep(&oc->streams->codec);
av_freep(&oc->streams);
}
if (!(fmt->flags & AVFMT_NOFILE))
{
// close the output file */
url_fclose(&oc->pb);
}
// free the stream */
av_free(oc);
av_free(video_outbuf1);
// Close the codec
avcodec_close(pCodecCtx);
// Close the video file
av_close_input_file(pFormatCtx);
//free(p);
return 0;
}