Code Monkey home page Code Monkey logo

Comments (4)

h4tr3d avatar h4tr3d commented on August 28, 2024

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

from avcpp.

h4tr3d avatar h4tr3d commented on August 28, 2024

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;
}

from avcpp.

jcyhcs avatar jcyhcs commented on August 28, 2024

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!

from avcpp.

h4tr3d avatar h4tr3d commented on August 28, 2024

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.

from avcpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.