Click here to Skip to main content
15,891,513 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: how to stop run of a program after a message display Pin
Maximilien19-May-10 8:43
Maximilien19-May-10 8:43 
GeneralRe: how to stop run of a program after a message display Pin
mrby12319-May-10 9:15
mrby12319-May-10 9:15 
AnswerRe: how to stop run of a program after a message display Pin
Aescleal19-May-10 12:53
Aescleal19-May-10 12:53 
AnswerRe: how to stop run of a program after a message display Pin
KarstenK19-May-10 21:10
mveKarstenK19-May-10 21:10 
QuestionNeed to change the main icon of an SDI app with the new ribbon interface (with a transparent icon) Pin
sashoalm19-May-10 3:45
sashoalm19-May-10 3:45 
AnswerRe: Need to change the main icon of an SDI app with the new ribbon interface (with a transparent icon) Pin
Code-o-mat19-May-10 3:51
Code-o-mat19-May-10 3:51 
GeneralRe: Need to change the main icon of an SDI app with the new ribbon interface (with a transparent icon) Pin
sashoalm19-May-10 4:03
sashoalm19-May-10 4:03 
GeneralRe: Need to change the main icon of an SDI app with the new ribbon interface (with a transparent icon) Pin
Code-o-mat19-May-10 4:05
Code-o-mat19-May-10 4:05 
GeneralRe: Need to change the main icon of an SDI app with the new ribbon interface (with a transparent icon) Pin
sashoalm19-May-10 4:28
sashoalm19-May-10 4:28 
Questionhow do I create a process dump file in WinXP and Win7? Pin
Dave Calkins19-May-10 3:28
Dave Calkins19-May-10 3:28 
AnswerRe: how do I create a process dump file in WinXP and Win7? Pin
Code-o-mat19-May-10 3:47
Code-o-mat19-May-10 3:47 
GeneralRe: how do I create a process dump file in WinXP and Win7? Pin
Dave Calkins19-May-10 4:38
Dave Calkins19-May-10 4:38 
QuestionWinInet Pin
Rajmohan SK19-May-10 1:30
Rajmohan SK19-May-10 1:30 
AnswerRe: WinInet Pin
«_Superman_»19-May-10 11:03
professional«_Superman_»19-May-10 11:03 
QuestionRe: WinInet Pin
Rajmohan SK19-May-10 18:29
Rajmohan SK19-May-10 18:29 
QuestionPNG Image Pin
AbhiHcl18-May-10 23:59
AbhiHcl18-May-10 23:59 
AnswerRe: PNG Image Pin
sashoalm19-May-10 0:31
sashoalm19-May-10 0:31 
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..

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.