1

I decode h264 file with DecodeFrameNoDelay function and get yuvData buffer

I want write the yuvData buffer to file .yuv and play with ffmpeg command. But when I play yuv file with ffmpeg command the result is not correct.

This is ffmpeg command I use to play yuv file:

ffplay -f rawvideo -pix_fmt yuv420p -video_size 1280x720 input_video.yuv 

This is my code use to decode h264 file and write yuvData buffer to file .yuv

void init(int width, int height) {
    WelsCreateDecoder(&decoder);
    decodingParam = {0};
    decodingParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
    decoder->Initialize(&decodingParam);
    bufferInfo = {0};
    bufferInfo.iBufferStatus = 1;

    yuvData = new uint8_t*[3];
    yuvData[0] = new uint8_t[width * height];
    yuvData[1] = new uint8_t[width * height / 4];
    yuvData[2] = new uint8_t[width * height / 4];

}

bool decode(const unsigned char* rawEncodedData, int rawEncodedDataLength, uint8_t** yuvData, char *name) {
    int err = decoder->DecodeFrameNoDelay(rawEncodedData, rawEncodedDataLength, yuvData, &bufferInfo);

    int width = bufferInfo.UsrData.sSystemBuffer.iWidth;
    int height = bufferInfo.UsrData.sSystemBuffer.iHeight;

    FILE *pFile = fopen(std::string("screen.yuv").c_str(), "ab");
    fwrite(yuvData[0], 1, width * height, pFile);
    fwrite(yuvData[1], 1, width * height / 4, pFile);
    fwrite(yuvData[2], 1, width * height / 4, pFile);
    fclose(pFile);

    std::cout << width << height << std::endl;

    if (err != 0) {
        std::cout << "H264 decoding failed. Error code: " << err << "." << std::endl;
        parseErrorCode(err);
        return false;
    }

    printf("H264 decoding success, err: %d, name: %s, status: %d\n", err, name, bufferInfo.iBufferStatus);

    return true;
}

I have some question need make clear:

1/yuvData buffer I get from DecodeFrameNoDelay function is really yuv420 format ?

2/Something wrong with my code ?

Any help is appreciated.

Thanks and best regards.

Max
  • 81
  • 3
  • Did you solve your problem? If not, can you share some frames of the yuv-file so I can have a look? – Fredrik Pihl Aug 19 '19 at 09:17
  • @FredrikPihl I just know the `yuvData` from `DecodeFrameNoDelay` is yuv420 format and still not solve why ffmpeg play wrong with yuv file that I write. You can use this command `ffmpeg -i input.mp4 -vcodec copy -bsf h264_mp4toannexb -an -f h264 output.h264` to convert a video to h264 format and extract frame with command `ffmpeg -i input.h264 -c:v copy -frames:v 1 -f h264 frame.h264` – Max Aug 22 '19 at 08:16

0 Answers0