1

When I am passing the scaled frame to the encoder, I keep getting the error from the container mp4 code. The sws context is defined as:

encoder->sws_ctx = sws_getContext(1920, 1080,
                            AV_PIX_FMT_YUV420P, 
                           scaled_frame->width, scaled_frame->height, scaled_frame->format, SWS_BICUBIC, NULL, NULL, NULL );

The code for scaling the frame is as follows:

 sws_scale_frame(encoder->sws_ctx, scaled_frame, input_frame);
 if (encode_video(decoder, encoder, scaled_frame))
     return -1;

When I am passing the input_frame directly to the function encode_video I get no errors. So, I think the error is possibly from sws_scale_frame, maybe from the context or frame declarations. However, I am unable to figure out what?

The declaration of scaled_frame is as follows:

AVFrame *scaled_frame = av_frame_alloc();
scaled_frame->width = 854;
scaled_frame->height = 480;
scaled_frame->format = AV_PIX_FMT_YUV420P;

I have looked through the examples in the ffmpeg repo, but solution didn't work.

genpfault
  • 51,148
  • 11
  • 85
  • 139
lokit khemka
  • 349
  • 3
  • 11
  • Please show the definition of AV_PIX_FMT_YUV420P – stark May 03 '23 at 14:25
  • Please look at this page for the definition: https://ffmpeg.org/doxygen/3.4/pixfmt_8h.html – lokit khemka May 03 '23 at 14:41
  • `PTS` is "presentation time stamp". This is how a _decoder_ knows when to display the frame. So, you may have to set the PTS for the [each] frame. Or, you have to set the video frame rate (e.g. 30 fps) so the PTS can be calculated. So, I'm not sure that a scaled frame (i.e. the pixel format, frame geometry, aspect ratio) matters as far as this goes. Audio frames also have a PTS. This is how A/V sync is maintained. Also, if a stream segment is corrupted, the PTS values can be used to resync. (e.g.) inject still frames, add/elide audio samples. – Craig Estey May 03 '23 at 17:17
  • Also, there is `DTS` (decode time stamp). This tells [usually H/W decoders with limited frame buffer memory] when to decode the frame. This is so the decode process doesn't fall behind or run ahead too much. It doesn't wait too long to decode the frame and fall behind or run ahead and use too much memory. – Craig Estey May 03 '23 at 17:27
  • 1
    Also, for a GOP (group-of-pictures), the incoming sequence to the encoder might be: `I-frame,B-frame,B-frame,P-frame`. This would be transmitted as: `I-frame,P-frame,B-frame,B-frame` so the timing would be (e.g.) `dts=0/pts=0 dts=1/pts=3 dts=2/pts=1 dts=3/pts=2` where each of these numbers is scaled by `n` – Craig Estey May 03 '23 at 17:34
  • So, what is `scaled_frame->pts` for your frames? You may need to examine the PTS/DTS for the unscaled input and copy/adjust those values. – Craig Estey May 03 '23 at 17:39
  • `scaled_frame->pts` is some garbage value. – lokit khemka May 04 '23 at 03:36
  • 1
    ```scaled_frame->pts = input_frame->pts; scaled_frame->pkt_dts = input_frame->pkt_dts;``` seems to have solve the error. – lokit khemka May 04 '23 at 03:41

0 Answers0