Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save avpack raw data into h264 file, but it can't play,help! #86

Closed
jcyhcs opened this issue Nov 24, 2020 · 4 comments
Closed

save avpack raw data into h264 file, but it can't play,help! #86

jcyhcs opened this issue Nov 24, 2020 · 4 comments

Comments

@jcyhcs
Copy link

jcyhcs commented Nov 24, 2020

image
container is FLV, how can i save raw h264 file?

@h4tr3d
Copy link
Owner

h4tr3d commented Nov 24, 2020

Don't write raw h264 data to the file. Try to use h264 muxer.

@h4tr3d
Copy link
Owner

h4tr3d commented Nov 24, 2020

Sample, that works for me:

#include <iostream>

#include "av.h"
#include "format.h"
#include "codeccontext.h"
#include "formatcontext.h"

using namespace std;

int main(int argc, char** argv)
{
    if (argc < 3)
        return 2;

    string input_uri{argv[1]};
    string output_uri{argv[2]};

    av::init();

    //
    // INPUT
    //
    av::FormatContext ictx;

    ictx.openInput(input_uri);
    ictx.findStreamInfo();

    ssize_t video_stream = -1;

    //
    // OUTPUT
    //
    av::OutputFormat oformat("h264");
    av::FormatContext octx;
    octx.setFormat(oformat);

    for (size_t i = 0; i < ictx.streamsCount(); ++i) {
        auto st = ictx.stream(i);
        auto coder = av::GenericCodecContext(st);

        if (!st.isVideo())
            continue;

        if (!octx.outputFormat().codecSupported(coder.codec()))
            continue;

        auto ost = octx.addStream(coder.codec());
        ost.setTimeBase(st.timeBase());
        auto ocoder = av::GenericCodecContext(ost);

        ocoder.copyContextFrom(coder);

        video_stream = i;
        break;
    }

    clog << "VideoStream: " << video_stream << '\n';

    octx.openOutput(output_uri);
    octx.writeHeader();
    octx.dump();

    while (true) {
        auto pkt = ictx.readPacket();
        if (!pkt)
            break;

        if (pkt.streamIndex() != video_stream)
            continue;

        pkt.setStreamIndex(0);
        octx.writePacket(pkt);
    }

    octx.writePacket();
    octx.writeTrailer();

    return 0;
}

@jcyhcs
Copy link
Author

jcyhcs commented Nov 25, 2020

one more question, i want to get raw h264 packet , then send to HARDWARE DECODER, but if use your example, how can realize this function? i try some code like this:
image
i just test my code is ok,it can create h264 file, and it can play, and the raw packet can send to hardware decoder. BUT, it this code RIGHT? it this code have memory leak?? PLEASE HELP ME!

@h4tr3d
Copy link
Owner

h4tr3d commented Nov 25, 2020

Just read about format of h264 packets and difference between Annex-B format and AVCC format. Start from here.

Basically, AVPacket contains NALU without [start code]. I deeply work only with Xilinx VCU via OpenMAX interface. This decoder expects packets in AnnexB format with start codes.

FFmpeg "h264" format saves frames into container in AnnexB format. You can do it manually by prepend 0x01000000 to the pkt.data().

Similar things done by BSF (bitstream filter) that used by your last sample.

By my opinion, it is not a good idea to use same AVPacket pointer to receive processed packets from bistream filter chain.

@h4tr3d h4tr3d closed this as completed Dec 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants