Click here to Skip to main content
15,881,744 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: PNG Image Pin
EmoBemo19-May-10 3:26
EmoBemo19-May-10 3:26 
Questionimage color depth Pin
john563218-May-10 23:21
john563218-May-10 23:21 
AnswerRe: image color depth Pin
Code-o-mat19-May-10 4:03
Code-o-mat19-May-10 4:03 
GeneralRe: image color depth Pin
john563219-May-10 4:18
john563219-May-10 4:18 
GeneralRe: image color depth Pin
Code-o-mat19-May-10 4:24
Code-o-mat19-May-10 4:24 
GeneralRe: image color depth Pin
john563219-May-10 19:59
john563219-May-10 19:59 
GeneralRe: image color depth Pin
Code-o-mat19-May-10 22:05
Code-o-mat19-May-10 22:05 
QuestionAVCODEC_OPEN and AVCODEC_ENCODE_VIDEO fails... Pin
gmallax18-May-10 22:28
gmallax18-May-10 22:28 
Hi all,

I am working on transcoding using ffmeg.Im having the mpeg4 video and alaw audio which should be converted to the desired video and audio format.Im reading the frame by frame conversion from my media stream using av_read_frame(...)
int main(int argc, char **argv)
{
    const char *filename;
    AVOutputFormat *fmt;
    AVFormatContext *oc;
	AVFormatContext *ic;
	AVCodecContext *pCodecCtx;
	AVCodec *pCodec;
    AVStream *audio_st, *video_st;
	int i,frame,video_stream;

    // initialize libavcodec, and register all codecs and formats 
    av_register_all();    
     
    filename = "test.mp4";

	if(av_open_input_file(&ic, filename, NULL, 0, NULL)!=0) 
		return -1;	// Couldn't open file 

	// Retrieve stream information
	if(av_find_stream_info(ic)<0) 
		return -1; // Couldn't find stream information 

	// Find the first video stream 
	video_stream=-1; 
	for(i=0; i<10; i++) 
	if(ic->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) 
	{ 
		video_stream=i; 
		break; 
	}
	if(video_stream==-1) 
		return -1; // Didn't find a video stream 

	// Get a pointer to the codec context for the video stream 
	pCodecCtx=ic->streams[video_stream]->codec; 

	// Find the decoder for the video stream 
	pCodec=avcodec_find_decoder(pCodecCtx->codec_id); 
	if(pCodec==NULL) return -1; // Codec not found 

	// Open codec 
	if(avcodec_open(pCodecCtx, pCodec)<0) return -1; // Could not open codec 

	// Allocate frames
    AVPacket    pkt;
    AVFrame videoFrame;

	// 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);
    }

	/* allocate the output media 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 video stream
    AVStream* stream = av_new_stream(oc, 0);
    AVCodecContext* videoEncoderContext = stream->codec;
    setupVideoEncode(videoEncoderContext);
    stream->sample_aspect_ratio = av_d2q(1, 255);
    stream->pts_wrap_bits = 33;
    stream->codec->thread_count = 0;

	// Set output parameters
    if (av_set_parameters(oc, 0) < 0) {
        return 0;
    }   
    // Open video encoder
    
    AVCodec* codec = avcodec_find_encoder(videoEncoderContext->codec_id);
    if (!codec)
	{        
        return 0;
	}

    if (avcodec_open(videoEncoderContext, codec) < 0)
    {
        return 0;
    }    /* write the stream header, if any */
    av_write_header(oc);

	 /* open the output file, if needed */
	const char *ofilename = "testresult.mp4";
    if (!(fmt->flags & AVFMT_NOFILE)) {
        if (url_fopen(&oc->pb, ofilename, URL_WRONLY) < 0) {
            fprintf(stderr, "Could not open '%s'\n", filename);
            exit(1);
        }
    }
	// Do the transcoding
    int audioFrameFinished = 0;
    int videoFrameFinished = 0; 
	const int outbuf_size = 640000;
    uint8_t outbuf[outbuf_size];
    int64_t lastpts = 0;

	while(av_read_frame( ic, &pkt ) == 0)
    {
		if (video_stream == pkt.stream_index)
		{
            avcodec_get_frame_defaults(&videoFrame);
            avcodec_decode_video(pCodecCtx, &videoFrame,&videoFrameFinished,pkt.data, pkt.size);
            videoFrame.pts = pkt.dts;
            if (videoFrameFinished)
            {
                int out_size = avcodec_encode_video(videoEncoderContext,outbuf, outbuf_size, &videoFrame);                if (out_size > 0)
                {
                    AVPacket outpkt;
                    av_init_packet(&outpkt);
                    outpkt.data = outbuf;
                    outpkt.size = out_size;
                    outpkt.stream_index = 0;
                    outpkt.dts = outpkt.pts = videoEncoderContext->coded_frame->pts;
                    outpkt.flags |=(videoEncoderContext->coded_frame->key_frame) ? PKT_FLAG_KEY : 0;               
                   
                    if (av_write_frame (oc, &outpkt) < 0)
                    {
                       return 0;
                    }
                }
            }
        }
        av_free_packet( &pkt );
    }
    
    /* write the trailer, if any */
    av_write_trailer(oc);

	avcodec_close(videoEncoderContext);
    av_free(videoEncoderContext);
 
    /* free the streams */
    for(i = 0; i < oc->nb_streams; i++) {
        av_freep(&oc->streams[i]);
    }

    if (!(fmt->flags & AVFMT_NOFILE)) {
        /* close the output file */
        url_fclose(oc->pb);
    }

    /* free the stream */
    av_free(oc);

    return 0;
}
)

In the above code,the bold statements failed always..(avcodec_open() after avcodec_find_encoder() and avcodec_encode_video(...))
Do any one help me?

Thanx..
AnswerRe: AVCODEC_OPEN and AVCODEC_ENCODE_VIDEO fails... Pin
EmoBemo19-May-10 0:46
EmoBemo19-May-10 0:46 
GeneralRe: AVCODEC_OPEN and AVCODEC_ENCODE_VIDEO fails... Pin
gmallax19-May-10 1:31
gmallax19-May-10 1:31 
QuestionUnexpected behaviour of CFileDialog with Rapi and Ceutil dll. Pin
Le@rner18-May-10 21:30
Le@rner18-May-10 21:30 
AnswerRe: Unexpected behaviour of CFileDialog with Rapi and Ceutil dll. Pin
David Crow19-May-10 5:39
David Crow19-May-10 5:39 
QuestionRemove menu toolbar from MDI Pin
mesajflaviu18-May-10 19:22
mesajflaviu18-May-10 19:22 
AnswerRe: Remove menu toolbar from MDI Pin
Fishyu18-May-10 23:39
Fishyu18-May-10 23:39 
GeneralRe: Remove menu toolbar from MDI Pin
mesajflaviu20-May-10 7:09
mesajflaviu20-May-10 7:09 
GeneralRe: Remove menu toolbar from MDI Pin
mesajflaviu20-May-10 19:43
mesajflaviu20-May-10 19:43 
QuestionProcess run as administrator Pin
ShilpiP18-May-10 18:47
ShilpiP18-May-10 18:47 
AnswerRe: Process run as administrator Pin
Xeqtr18-May-10 22:00
Xeqtr18-May-10 22:00 
QuestionRe: Process run as administrator Pin
David Crow19-May-10 5:41
David Crow19-May-10 5:41 
QuestionObject Oriented c++ Visual Studio Problem. Pin
RobNO18-May-10 12:16
professionalRobNO18-May-10 12:16 
AnswerRe: Object Oriented c++ Visual Studio Problem. Pin
Graham Breach18-May-10 12:27
Graham Breach18-May-10 12:27 
GeneralRe: Object Oriented c++ Visual Studio Problem. Pin
RobNO18-May-10 12:48
professionalRobNO18-May-10 12:48 
AnswerRe: Object Oriented c++ Visual Studio Problem. Pin
Luc Pattyn18-May-10 13:23
sitebuilderLuc Pattyn18-May-10 13:23 
AnswerRe: Object Oriented c++ Visual Studio Problem. Pin
xtofl18-May-10 22:26
xtofl18-May-10 22:26 
QuestionLarge Menu Items Pin
cryptoknight1718-May-10 12:14
cryptoknight1718-May-10 12:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.