Newest 'libx264' Questions - Stack Overflow

http://stackoverflow.com/questions/tagged/libx264

Les articles publiés sur le site

  • How to get FFMPEG to encode H264 using libx264 ?

    12 décembre 2021, par Basit Anwer

    FFMPEG encode example fails to create a H264 video. MPEG1 works fine though.

    Pasting the code here as well

    
         * @file
         * video encoding with libavcodec API example
         *
         * @example encode_video.c
         */
        #include 
        #include 
        #include 
        #include avcodec.h>
        #include opt.h>
        #include imgutils.h>
        static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
                           FILE *outfile)
        {
            int ret;
            /* send the frame to the encoder */
            if (frame)
                printf("Send frame %3"PRId64"\n", frame->pts);
            ret = avcodec_send_frame(enc_ctx, frame);
            if (ret < 0) {
                fprintf(stderr, "Error sending a frame for encoding\n");
                exit(1);
            }
            while (ret >= 0) {
                ret = avcodec_receive_packet(enc_ctx, pkt);
                if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                    return;
                else if (ret < 0) {
                    fprintf(stderr, "Error during encoding\n");
                    exit(1);
                }
                printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
                fwrite(pkt->data, 1, pkt->size, outfile);
                av_packet_unref(pkt);
            }
        }
    
        int main(int argc, char **argv)
        {
            const char *filename, *codec_name;
            const AVCodec *codec;
            AVCodecContext *c= NULL;
            int i, ret, x, y;
            FILE *f;
            AVFrame *frame;
            AVPacket *pkt;
            uint8_t endcode[] = { 0, 0, 1, 0xb7 };
            if (argc <= 2) {
                fprintf(stderr, "Usage: %s  \n", argv[0]);
                exit(0);
            }
            filename = argv[1];
            codec_name = argv[2];
            /* find the mpeg1video encoder */
            codec = avcodec_find_encoder_by_name(codec_name);
            if (!codec) {
                fprintf(stderr, "Codec '%s' not found\n", codec_name);
                exit(1);
            }
            c = avcodec_alloc_context3(codec);
            if (!c) {
                fprintf(stderr, "Could not allocate video codec context\n");
                exit(1);
            }
            pkt = av_packet_alloc();
            if (!pkt)
                exit(1);
            /* put sample parameters */
            c->bit_rate = 400000;
            /* resolution must be a multiple of two */
            c->width = 352;
            c->height = 288;
            /* frames per second */
            c->time_base = (AVRational){1, 25};
            c->framerate = (AVRational){25, 1};
            /* emit one intra frame every ten frames
             * check frame pict_type before passing frame
             * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
             * then gop_size is ignored and the output of encoder
             * will always be I frame irrespective to gop_size
             */
            c->gop_size = 10;
            c->max_b_frames = 1;
            c->pix_fmt = AV_PIX_FMT_YUV420P;
            if (codec->id == AV_CODEC_ID_H264)
                av_opt_set(c->priv_data, "preset", "slow", 0);
            /* open it */
            ret = avcodec_open2(c, codec, NULL);
            if (ret < 0) {
                fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
                exit(1);
            }
            f = fopen(filename, "wb");
            if (!f) {
                fprintf(stderr, "Could not open %s\n", filename);
                exit(1);
            }
            frame = av_frame_alloc();
            if (!frame) {
                fprintf(stderr, "Could not allocate video frame\n");
                exit(1);
            }
            frame->format = c->pix_fmt;
            frame->width  = c->width;
            frame->height = c->height;
            ret = av_frame_get_buffer(frame, 32);
            if (ret < 0) {
                fprintf(stderr, "Could not allocate the video frame data\n");
                exit(1);
            }
            /* encode 1 second of video */
            for (i = 0; i < 25; i++) {
                fflush(stdout);
                /* make sure the frame data is writable */
                ret = av_frame_make_writable(frame);
                if (ret < 0)
                    exit(1);
                /* prepare a dummy image */
                /* Y */
                for (y = 0; y < c->height; y++) {
                    for (x = 0; x < c->width; x++) {
                        frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
                    }
                }
                /* Cb and Cr */
                for (y = 0; y < c->height/2; y++) {
                    for (x = 0; x < c->width/2; x++) {
                        frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
                        frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
                    }
                }
                frame->pts = i;
                /* encode the image */
                encode(c, frame, pkt, f);
            }
            /* flush the encoder */
            encode(c, NULL, pkt, f);
            /* add sequence end code to have a real MPEG file */
            if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO)
                fwrite(endcode, 1, sizeof(endcode), f);
            fclose(f);
            avcodec_free_context(&c);
            av_frame_free(&frame);
            av_packet_free(&pkt);
            return 0;
        }
    

    The code fails at encode call and every avcodec_receive_packet call returns AVERROR(EAGAIN)

    What am i missing here?

  • ffmpeg configure ERROR : libx264 not found

    1er décembre 2021, par Dion

    I want to configure ffmpeg on Ubuntu 64 bit, but I get an error: "ERROR: libx264 not found". ffmpeg doesn’t see libx264! Before that, I successfully configure it in QNX and Windows. I prevented a known bug with --disable-opencl , but it did not help. In my project the libx264 should be static.

    libx264 configure:

    ../configure --prefix=x264 --disable-cli --enable-static --disable-opencl
    
    platform:      X86_64
    byte order:    little-endian
    system:        LINUX
    cli:           no
    libx264:       internal
    shared:        no
    static:        yes
    asm:           yes
    interlaced:    yes
    avs:           no
    lavf:          no
    ffms:          no
    mp4:           no
    gpl:           yes
    thread:        posix
    opencl:        no
    filters:       crop select_every
    lto:           no
    debug:         no
    gprof:         no
    strip:         no
    PIC:           no
    bit depth:     all
    chroma format: all
    

    it's OK! Confused only platform: X86_64

    FFmpeg configure:

    ../configure --target-os=linux --prefix=ffmpeg --disable-programs \
    --disable-ffplay --disable-ffprobe --disable-doc \ 
    --disable-htmlpages --disable-manpages --disable-podpages \ 
    --disable-txtpages --disable-avdevice --disable-postproc \
    --disable-network --disable-encoders --enable-encoder=libx264 \
    --disable-decoders --enable-decoder=h264 --disable-hwaccels \
    --disable-muxers --enable-muxer=matroska --disable-demuxers \
    --disable-parsers --enable-parser=h264 --enable-gpl \
    --enable-libx264 \
    --extra-ldflags=-L../x264/lib \
    --extra-cflags=-I../x264/include
    
        ERROR: libx264 not found
    

    Paths exactly correct!

    if delete --enable-libx264:

    install prefix            ffmpeg
    source path               /home/osuser/develop/libs/source/ffmpeg-3.4.2
    C compiler                gcc
    C library                 glibc
    ARCH                      x86 (generic)
    big-endian                no
    runtime cpu detection     yes
    standalone assembly       yes
    x86 assembler             nasm
    ...
    

    This is what worries me... Why x86?

    ARCH x86 (generic)

    x86 assembler nasm

    Maybe this is the problem? How to configure ffmpeg in x86_64? --arch=x86_64 not help!

    UPDATE: The problem is fixed, when configure libx264, need to add --enable-pic

  • build x264 failed on apple M1, No working C compiler found

    29 novembre 2021, par louxiu
    1. Apple M1, clang
    Apple clang version 13.0.0 (clang-1300.0.29.3)
    Target: arm64-apple-darwin21.1.0
    Thread model: posix
    InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
    
    1. x264 version x264-snapshot-20191217-2245

    2. configure

     ./configure --prefix=/tmp/  --enable-static
    
    1. config.log
    checking for -Werror=unknown-warning-option... yes
    checking for -mdynamic-no-pic... yes
    x264 configure script
    Command line options: "--prefix=/tmp/" "--enable-static"
    
    checking whether gcc works... no
    Failed commandline was:
    --------------------------------------------------
    gcc conftest.c  -Wall -I. -I$(SRCPATH) -mdynamic-no-pic -arch armv7  -Werror=unknown-warning-option    -lm -arch armv7 -o conftest
    ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/lib/darwin/libclang_rt.osx.a, missing required architecture armv7 in file /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/lib/darwin/libclang_rt.osx.a (5 slices)
    ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libm.tbd, missing required architecture armv7 in file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libm.tbd (3 slices)
    ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, missing required architecture armv7 in file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd (3 slices)
    ld: dynamic main executables must link with libSystem.dylib for architecture armv7
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    --------------------------------------------------
    Failed program was:
    --------------------------------------------------
    int main (void) {  return 0; }
    --------------------------------------------------
    DIED: No working C compiler found.
    
  • FFmpeg libxh264 error on Slackware current

    22 novembre 2021, par unixbhaskar

    Well, here are specs which gives you heads up before suggesting ...

    bhaskar@Slackware_06:46:54_Sat Nov 20: :~>ffmpeg -codecs | grep h264
    ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
      built with gcc 11.2.0 (GCC)
      configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/doc/ffmpeg-4.4.1/html --mandir=/usr/man --disable-debug --enable-shared --disable-static --enable-gpl --enable-version3 --enable-avresample --arch=x86_64 --disable-encoder=aac --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gnutls --enable-libbluray --enable-libcaca --enable-libcdio --enable-frei0r --enable-openal --enable-libopus --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libmp3lame --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libpulse --enable-libsmbclient --enable-libxml2 --enable-librsvg --enable-libdrm
      libavutil      56. 70.100 / 56. 70.100
      libavcodec     58.134.100 / 58.134.100
      libavformat    58. 76.100 / 58. 76.100
      libavdevice    58. 13.100 / 58. 13.100
      libavfilter     7.110.100 /  7.110.100
      libavresample   4.  0.  0 /  4.  0.  0
      libswscale      5.  9.100 /  5.  9.100
      libswresample   3.  9.100 /  3.  9.100
      libpostproc    55.  9.100 / 55.  9.100
     DEV.LS h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_v4l2m2m ) (encoders: h264_v4l2m2m h264_vaapi )
    

    Also

    ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
      built with gcc 11.2.0 (GCC)
      configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/doc/ffmpeg-4.4.1/html --mandir=/usr/man --disable-debug --enable-shared --disable-static --enable-gpl --enable-version3 --enable-avresample --arch=x86_64 --disable-encoder=aac --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gnutls --enable-libbluray --enable-libcaca --enable-libcdio --enable-frei0r --enable-openal --enable-libopus --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libmp3lame --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libpulse --enable-libsmbclient --enable-libxml2 --enable-librsvg --enable-libdrm
      libavutil      56. 70.100 / 56. 70.100
      libavcodec     58.134.100 / 58.134.100
      libavformat    58. 76.100 / 58. 76.100
      libavdevice    58. 13.100 / 58. 13.100
      libavfilter     7.110.100 /  7.110.100
      libavresample   4.  0.  0 /  4.  0.  0
      libswscale      5.  9.100 /  5.  9.100
      libswresample   3.  9.100 /  3.  9.100
      libpostproc    55.  9.100 / 55.  9.100
    Guessed Channel Layout for Input Stream #0.0 : stereo
    Input #0, alsa, from 'pulse':
      Duration: N/A, start: 1637371168.782400, bitrate: 1536 kb/s
      Stream #0:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s
    [x11grab @ 0x87db00] Stream #0: not enough frames to estimate rate; consider increasing probesize
    Input #1, x11grab, from ':0':
      Duration: N/A, start: 1637371168.825367, bitrate: 839270 kb/s
      Stream #1:0: Video: rawvideo (BGR[0] / 0x524742), bgr0, 1366x768, 839270 kb/s, 25 fps, 1000k tbr, 1000k tbn, 1000k tbc
    Unknown encoder 'libx264'
    

    And ..

    bhaskar@Slackware_06:50:02_Sat Nov 20: :~>uname -a
    Linux Slackware 5.15.1-Slackware #1 SMP Sun Nov 7 10:48:30 IST 2021 x86_64 Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz GenuineIntel GNU/Linux
    bhaskar@Slackware_06:50:04_Sat Nov 20: :~>cat /etc/os-release
    ───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
           │ File: /etc/os-release
    ───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       1   │ NAME=Slackware
       2   │ VERSION="15.0"
       3   │ ID=slackware
       4   │ VERSION_ID=15.0
       5   │ PRETTY_NAME="Slackware 15.0 x86_64"
       6   │ ANSI_COLOR="0;34"
       7   │ CPE_NAME="cpe:/o:slackware:slackware_linux:15.0"
       8   │ HOME_URL="http://slackware.com/"
       9   │ SUPPORT_URL="http://www.linuxquestions.org/questions/slackware-14/"
      10   │ BUG_REPORT_URL="http://www.linuxquestions.org/questions/slackware-14/"
      11   │ VERSION_CODENAME=stable
    

    Wondering .....when try to build it like mentioned in this place :

    https://www.linuxquestions.org/questions/slackware-14/ffmpeg-unknown-encoder-%27libx264%27-4175677695/#post6140669

    Am I the only one , who failed to understand ,why the heck is not detecting ??

    Also,

    root@Slackware_06:52:59_Sat Nov 20:~#ldconfig -p | grep libxh264
    root@Slackware_06:53:12_Sat Nov 20:~#ldconfig -p | grep h264
    root@Slackware_06:54:18_Sat Nov 20:~#ls /usr/lib64/libxh2*
    ls: cannot access '/usr/lib64/libxh2*': No such file or directory
    root@Slackware_06:54:46_Sat Nov 20:~#ls /usr/lib/libx2*
    ls: cannot access '/usr/lib/libx2*': No such file or directory
    

    Finally....also

    bhaskar@Slackware_06:56:58_Sat Nov 20: :~>ffmpeg -encoders | grep h264
    ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
      built with gcc 11.2.0 (GCC)
      configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/doc/ffmpeg-4.4.1/html --mandir=/usr/man --disable-debug --enable-shared --disable-static --enable-gpl --enable-version3 --enable-avresample --arch=x86_64 --disable-encoder=aac --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gnutls --enable-libbluray --enable-libcaca --enable-libcdio --enable-frei0r --enable-openal --enable-libopus --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libmp3lame --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libpulse --enable-libsmbclient --enable-libxml2 --enable-librsvg --enable-libdrm
      libavutil      56. 70.100 / 56. 70.100
      libavcodec     58.134.100 / 58.134.100
      libavformat    58. 76.100 / 58. 76.100
      libavdevice    58. 13.100 / 58. 13.100
      libavfilter     7.110.100 /  7.110.100
      libavresample   4.  0.  0 /  4.  0.  0
      libswscale      5.  9.100 /  5.  9.100
      libswresample   3.  9.100 /  3.  9.100
      libpostproc    55.  9.100 / 55.  9.100
     V..... h264_v4l2m2m         V4L2 mem2mem H.264 encoder wrapper (codec h264)
     V....D h264_vaapi           H.264/AVC (VAAPI) (codec h264)
    

    Greatly appreciated if you come close or pinpoint the reason.

    Thanks.

  • How to get the bit size of a marcoblock in x264 ?

    29 octobre 2021, par Corvo Attano

    How to get the bit size of a marcoblock in x264 after encode ? Does x264 has any API to get the bit size of each marcoblocks after encoding a slice or a frame? If the answer is no, which function should I modify to handle the bit size after encoding marcoblocks ?