Kienan Knight-Boehm
Kienan Knight-Boehm

Reputation: 165

Forcing global headers in LibAV

I'm trying to write a C program using LibAV that takes input video from a webcam and saves it as an H264 MP4 file. I'm modifying a working program that saves .ppm frames from the webcam. I'm unable to convert the AVPackets so that they may be written, though--specifically, avformat_write_header() is failing, with the messages

[mp4 @ 0050c000] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 @ 0050c000] Could not find tag for codec none in stream #0, codec not currently supported in container

The call is apparently returning error -22, but I can find no place where that error code is actually explained. How can I force avformat_write_header() to add in global headers when it's trying to write the MP4? Code below; some of it is adapted from this question, but I'm trying to adapt it from an input video file to a webcam.

int _tmain(int argc, _TCHAR* argv[])
{
    AVInputFormat *inputFormat = NULL;
    AVDictionary *inputDictionary= NULL;
    AVFormatContext *inputFormatCtx = NULL;
    AVFormatContext *outputFormatCtx = NULL;
    AVCodecContext *inputCodecCtxOrig = NULL;
    AVCodecContext *inputCodecCtx = NULL;
    AVCodecContext *outputCodecCtx;
    AVCodec *inputCodec = NULL;
    AVCodec *outputCodec = NULL;
    AVStream *stream = NULL;
    AVIOContext *avioContext = NULL;
    avcodec_register_all();
    av_register_all();
    avdevice_register_all();
    av_dict_set(&inputDictionary, "Logitech HD Pro Webcam C920", "video", 0);
    avformat_alloc_output_context2(&outputFormatCtx, NULL, NULL, "output.mp4");
    avio_open(&avioContext, "output.mp4", AVIO_FLAG_WRITE);
    outputFormatCtx->pb = avioContext;
    stream = avformat_new_stream(outputFormatCtx, outputCodec);
    inputFormat = av_find_input_format("dshow");
    int r = avformat_open_input(&inputFormatCtx, "video=Logitech HD Pro Webcam C920", inputFormat, &inputDictionary);
    if (r != 0) {
        fprintf(stderr, "avformat_open_input() failed with error %d!\n", r);
        return -1; }
    r = avformat_find_stream_info(inputFormatCtx, NULL);
    if (r != 0) {
        fprintf(stderr, "avformat_find_stream_info() failed!\n");
        return -1; }
    av_dump_format(inputFormatCtx, 0, "video=Logitech HD Pro Webcam C920", 0);
    unsigned int i;
    int videoStream;
    videoStream = -1;
    for (i = 0; i < inputFormatCtx->nb_streams; i++) {
        if (inputFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO {
            videoStream = i;
            break; }
    }
    if (videoStream == -1)
        { return -1; }
    inputCodecCtxOrig = inputFormatCtx->streams[videoStream]->codec;
    inputCodec = avcodec_find_decoder(inputCodecCtxOrig->codec_id);
    if (inputCodec == NULL) {
        fprintf(stderr, "avcodec_find_decoder() failed!\n");
        return -1; }
    else { printf("Supported codec!\n"); }
    inputCodecCtx = avcodec_alloc_context3(inputCodec);
    if (inputCodecCtx == NULL) {
        fprintf(stderr, "avcodec_alloc_context3() failed!\n");
        return -1; }
    if (avcodec_copy_context(inputCodecCtx, inputCodecCtxOrig) != 0) {
        fprintf(stderr, "avcodec_copy_context() failed!\n");
        return -1; }
    if (avcodec_open2(inputCodecCtx,inputCodec,&inputDictionary) < 0) {
        fprintf(stderr, "avcodec_open2() failed!\n");
        return -1; }
    outputFormatCtx->oformat = av_guess_format(NULL, "output.mp4", NULL);
    outputFormatCtx->oformat->flags |= AVFMT_GLOBALHEADER;
    outputCodecCtx = avcodec_alloc_context3(outputCodec);
    avcodec_copy_context(outputCodecCtx, inputCodecCtx);
    outputCodec = inputCodec;
    avcodec_open2(outputCodecCtx, outputCodec, NULL);
    AVPacket packet;
    printf("foo\n");
    int errnum = avformat_write_header(outputFormatCtx, &inputDictionary);
    printf("bar %d\n", errnum);
    while(av_read_frame(inputFormatCtx, &packet)>=0) {
        av_interleaved_write_frame(outputFormatCtx, &packet);
        av_free_packet(&packet);
    }
    avcodec_close(inputCodecCtx);
    avcodec_close(inputCodecCtxOrig);
    avformat_close_input(&inputFormatCtx);
    return 0;
}

Upvotes: 2

Views: 3521

Answers (1)

Anton Angelov
Anton Angelov

Reputation: 1244

How can I force avformat_write_header() to add in global headers

Global headers are written by the encoder, later the muxer reads them from the codec's extra_data field. So you should set this flag in the codec's context, before you call avcodec_open2().

outputCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

[mp4 @ 0050c000] Could not find tag for codec none in stream #0, codec not currently supported in container

You can try to setup the encoder explicitly (i.e. manually), or copy the codeccontext from original input codeccontext.

outputCodec = av_codec_find_encoder(AV_CODEC_ID_H264);
if(!outputCodec) return -1; //no encoder found

outputCodecCtx = avcodec_alloc_context3(outputCodec);
avcodec_copy_context(outputCodecCtx, inputCodecCtxOrig); //copy from orig

//You have to make sure each field is populated correctly
//with debugger or assertations
assert(outputCodecCtx->codec_id == AV_CODEC_ID_H264); //etc

outputCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
if(avcodec_open2(outputCodecCtx, outputCodec, NULL) <0) return -1;

Upvotes: 2

Related Questions