3

Requirements :

I have a bunch of images (to be more specific they are 1024x768, 24bpp RGB PNG files) that I want to encode into a video files.

And I need to use 'libavcodec' library, not 'ffmpeg' tool. (well I know they are basically same in the origin, I am emphasizing because someone may answer to use 'ffmpeg' tool, but that's not a solution what I am looking for)

I am using h264 encoder.

Target : A high quality video with equal resolution (1024 x 768), YUV420P each image has a duration of 1 second. 24 fps

Problems : i've tried with many different (but same resolution and bits) png images, and all have failed to output a good video.

For some series of images, only the frames of first second was shown in a good shape, but the remaining frames was distorted and color changed (lighter).

For some series of images, it seemed the images were zoomed-in and distorted again.

and etc.

Question : I am a total AV newbie and I need someone to verify my steps for encoding. I am total AV newbie.

1) av_register_all()

2) avcodec_register_all()

3) avcodec_find_encoder()

4) avcodec_alloc_context3()

5) sets codec configuraton to context.

6) avcodec_open2()

7) opens a output file using fopen_s()

8)

for(int second=1; second<=10; ++seconds)
{

  Read a image from local using Gdiplus

  Create a gdiplus bitmap and draw the image onto this bitmap

  Get the raw byte data using LockBits

  Transfer this RGB raw byte into YUV420 frame using 'swscontext', 'sws_scale'

  for(int f=0; f<24; ++f)
  {
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;

    pFrame->pts = f;
    ret = avcodec_encode_video2(pContext, &pkt, pFrame, &got_output);

    if(got_output)
    {
        fwrite(pkt.data, 1, pkt.size, outputFile);
        av_free_packet(&pkt);
    }
  }
}

/* get the delayed frames */
for (got_output = 1; got_output; i++) {
    fflush(stdout);

    ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
    if (ret < 0) {
        fprintf(stderr, "Error encoding frame\n");
        exit(1);
    }

    if (got_output) {
        printf("Write frame %3d (size=%5d)\n", i, pkt.size);
        fwrite(pkt.data, 1, pkt.size, f);
        av_free_packet(&pkt);
    }
}

close everything required

I am sure that I misunderstood some steps using ffmpeg api. The above pseudo codes are based on the 'encoding' example of ffmpeg. which part am I doing wrong? please can someone help me?

p.s sorry about broken english. english is not my natvie language. I tried my best =P

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
Echomania
  • 31
  • 2

0 Answers0