Newest 'libx264' Questions - Stack Overflow

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

Les articles publiés sur le site

  • x264/x265 options for fast decoding while preserving quality

    25 septembre 2023, par user3301993

    I want to encode some videos in H264 and H265, and I would like to ask for help in order to chose the adequate options in x264/x265.

    • My first priority is video quality. Lossless would be the best quality for example
    • My second priority is fast decoding speed (ie: I would like faster decoding without sacrificing quality)
      • fast decoding for me means that the decoding machine would use less CPU resources reading the resulting video
      • Less RAM consumption would be a plus
      • Latency is not important
    • I don't care that much if the output file ends up bigger. Also, encoding speed is not important

    I'm aware of the option -tune fastdecode in both x264 and x265. But apparently the quality gets a bit worse using it.

    For x264:

    -tune fastdecode is equivalent to --no-cabac --no-deblock --no-weightb --weightp 0 (My source is x264 --fullhelp)

    Which options preserve quality ?

    For x265:

    -tune fastdecode is equivalent to --no-deblock --no-sao --no-weightp --no-weightb --no-b-intra (according to x265 doc)

    Again, which options preserve quality ?

    I tried to read some documentation on these options, but I'm afraid I'm too stupid to understand if they preserve quality or not.

    To explain further what I mean by "preserving quality":

    I don't understand what the cabac option does exactly. But let's say for example that it adds some extra lossless compression, resulting in a smaller video file, but the decoding machine would need to do extra work to decompress the file

    In that case, the --no-cabac option would skip that extra compression, resulting in no loss of quality, with a bigger file size, and the decoding machine would not need to decompress the extra compression, saving CPU work on the decoding side

    In this scenario, I would like to add the --no-cabac option, as it speeds up decoding, while preserving quality.

    I hope I could get my point across

    Can anyone help me pick the right options ?

    Thanks in advance

  • FFmpeg : Audio is sped up after compressing AVI video [closed]

    21 août 2023, par Karen S

    I have an AVI video and I want to reduce the file size. When I run the FFmpeg command below, the resulting audio in the video is sped up, and the last bit of the video has no audio.

    The FFmpeg command

    ffmpeg -i "in.avi" -vcodec libx264 -acodec copy -crf 26 -vf "scale=-1:'min(720,ih)', yadif=1" out.avi
    

    The output messages

    ffmpeg version 2023-06-11-git-09621fd7d9-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
      built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
      configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
      libavutil      58. 13.100 / 58. 13.100
      libavcodec     60. 17.100 / 60. 17.100
      libavformat    60.  6.100 / 60.  6.100
      libavdevice    60.  2.100 / 60.  2.100
      libavfilter     9.  8.101 /  9.  8.101
      libswscale      7.  3.100 /  7.  3.100
      libswresample   4. 11.100 /  4. 11.100
      libpostproc    57.  2.100 / 57.  2.100
    [avi @ 000002255f70d540] Switching to NI mode, due to poor interleaving
    Input #0, avi, from 'in.avi':
      Duration: 00:10:40.64, start: 0.000000, bitrate: 28852 kb/s
      Stream #0:0: Video: dvvideo, yuv411p, 720x480 [SAR 32:27 DAR 16:9], 25000 kb/s, SAR 8:9 DAR 4:3, 60k fps, 29.97 tbr, 60k tbn
      Stream #0:1: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s
      Stream #0:2: Audio: pcm_s16le, 32000 Hz, stereo, s16, 1024 kb/s
    Stream mapping:
      Stream #0:0 -> #0:0 (dvvideo (native) -> h264 (libx264))
      Stream #0:1 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    [libx264 @ 000002255fbc4940] using SAR=8/9
    [libx264 @ 000002255fbc4940] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
    [libx264 @ 000002255fbc4940] profile High 4:2:2, level 3.1, 4:2:2, 8-bit
    Output #0, avi, to 'out.avi':
      Metadata:
        ISFT            : Lavf60.6.100
      Stream #0:0: Video: h264 (H264 / 0x34363248), yuv422p(tv, progressive), 720x480 [SAR 8:9 DAR 4:3], q=2-31, 59.94 fps, 59.94 tbn
        Metadata:
          encoder         : Lavc60.17.100 libx264
        Side data:
          cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
      Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 48000 Hz, stereo, s16, 1536 kb/s
    [dvvideo @ 000002255fb36d80] Concealing bitstream errors.33 bitrate=2437.5kbits/s speed=0.902x
        Last message repeated 15 times
    [dvvideo @ 000002255fb36d80] Concealing bitstream errors.77 bitrate=2439.5kbits/s speed=0.902x
        Last message repeated 60 times
    [dvvideo @ 0000022560071680] Concealing bitstream errors.72 bitrate=2461.8kbits/s speed=0.895x
        Last message repeated 18 times
    [dvvideo @ 0000022560071680] Concealing bitstream errors.12 bitrate=2459.9kbits/s speed=0.894x
    [out#0/avi @ 000002255f70d8c0] video:110055kB audio:80088kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.724923%
    frame=38400 fps= 57 q=-1.0 Lsize=  191521kB time=00:10:40.60 bitrate=2449.2kbits/s speed=0.946x
    [libx264 @ 000002255fbc4940] frame I:165   Avg QP:27.36  size: 37180
    [libx264 @ 000002255fbc4940] frame P:9710  Avg QP:30.58  size:  7328
    [libx264 @ 000002255fbc4940] frame B:28525 Avg QP:35.24  size:  1241
    [libx264 @ 000002255fbc4940] consecutive B-frames:  0.8%  0.3%  0.3% 98.6%
    [libx264 @ 000002255fbc4940] mb I  I16..4: 10.3% 67.6% 22.1%
    [libx264 @ 000002255fbc4940] mb P  I16..4:  1.0%  3.8%  0.8%  P16..4: 40.7% 12.1%  8.6%  0.0%  0.0%    skip:33.0%
    [libx264 @ 000002255fbc4940] mb B  I16..4:  0.1%  0.2%  0.0%  B16..8: 36.9%  1.8%  0.4%  direct: 1.4%  skip:59.1%  L0:45.0% L1:51.8% BI: 3.1%
    [libx264 @ 000002255fbc4940] 8x8 transform intra:66.5% inter:68.7%
    [libx264 @ 000002255fbc4940] coded y,uvDC,uvAC intra: 60.3% 64.7% 10.1% inter: 10.7% 6.9% 0.1%
    [libx264 @ 000002255fbc4940] i16 v,h,dc,p: 21% 35% 10% 34%
    [libx264 @ 000002255fbc4940] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 11% 23% 19%  6%  8%  7% 12%  6%  9%
    [libx264 @ 000002255fbc4940] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 13% 25% 16%  6% 10%  7% 12%  5%  6%
    [libx264 @ 000002255fbc4940] i8c dc,h,v,p: 58% 19% 18%  5%
    [libx264 @ 000002255fbc4940] Weighted P-Frames: Y:3.0% UV:1.5%
    [libx264 @ 000002255fbc4940] ref P L0: 56.9% 16.8% 19.3%  6.7%  0.2%
    [libx264 @ 000002255fbc4940] ref B L0: 87.5% 10.4%  2.2%
    [libx264 @ 000002255fbc4940] ref B L1: 93.2%  6.8%
    [libx264 @ 000002255fbc4940] kb/s:1407.29
    

    Since the audio stream is being copied I don't understand why the resulting audio sounds sped up. The video looks fine, it's just the audio that is the issue.

    I've tried other options too, such as converting the audio to aac or mp3 and the resulting audio still sounds sped up and high pitched.

    Does anyone know why the audio isn't simply being copied the way it sounds in the original AVI video and do you have any suggestions for how to fix the issue?

  • asm SIMD sniffer

    1er août 2023, par Андрей Тернити

    There is x264. It use a lot of x86 asm files. For example pixel-32.asm. This files can use different SIMD instruction set: mmx, 3DNow!, sse family, others

    I need the simple way to automatically analyze every file. I want get which SIMD family in which file are used. How?

    I think every asm file must contain information about which SIMD family it use (or information that no SIMD). Without this information it is very bad idea try to use this files...
    I am angry, my x86 CPU support mmx and 3DNow! only, but x264 try call sse, so I get "Illegal instruction" sometimes. I plan to make patch for x264.

    P.S. If you can make issues in official repo let me know.

    P.P.S. This thread on Doom9 (mirror).

  • Android h264 decode non-existing PPS 0 referenced

    8 juin 2023, par nmxprime

    In Android JNI, using ffmpeg with libx264 use below codes to encode and decode raw rgb data!. I should use swscale to convert rgb565 to yuv420p as required by H.264. But not clear about this conversion.Please help, where i am wrong, with regard the log i get!

    Code for Encoding

    codecinit()- called once(JNI wrapper function)

    int Java_com_my_package_codecinit (JNIEnv *env, jobject thiz) {
    avcodec_register_all();
    codec = avcodec_find_encoder(AV_CODEC_ID_H264);//AV_CODEC_ID_MPEG1VIDEO);
    if(codec->id == AV_CODEC_ID_H264)
        __android_log_write(ANDROID_LOG_ERROR, "set","h264_encoder");
    
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        __android_log_write(ANDROID_LOG_ERROR, "codec", "not found");
    
    }
        __android_log_write(ANDROID_LOG_ERROR, "codec", "alloc-contest3");
    c= avcodec_alloc_context3(codec);
    if(c == NULL)
        __android_log_write(ANDROID_LOG_ERROR, "avcodec","context-null");
    
    picture= av_frame_alloc();
    
    if(picture == NULL)
        __android_log_write(ANDROID_LOG_ERROR, "picture","context-null");
    
    c->bit_rate = 400000;
    c->height = 800;
    c->time_base= (AVRational){1,25};
    c->gop_size = 10; 
    c->max_b_frames=1;
    c->pix_fmt = AV_PIX_FMT_YUV420P;
    outbuf_size = 768000;
    c->width = 480;
    
    size = (c->width * c->height);
    
    if (avcodec_open2(c, codec,NULL) < 0) {
    
    __android_log_write(ANDROID_LOG_ERROR, "codec", "could not open");
    
    
    }
    
    ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height,
                         c->pix_fmt, 32);
    if (ret < 0) {
            __android_log_write(ANDROID_LOG_ERROR, "image","alloc-failed");
        fprintf(stderr, "could not alloc raw picture buffer\n");
    
    }
    
    picture->format = c->pix_fmt;
    picture->width  = c->width;
    picture->height = c->height;
    return 0;
    
    }
    

    encodeframe()-called in a while loop

    int Java_com_my_package_encodeframe (JNIEnv *env, jobject thiz,jbyteArray buffer) {
    jbyte *temp= (*env)->GetByteArrayElements(env, buffer, 0);
    Output = (char *)temp;
    const uint8_t * const inData[1] = { Output }; 
    const int inLinesize[1] = { 2*c->width };
    
    //swscale should implement here
    
        av_init_packet(&pkt);
        pkt.data = NULL;    // packet data will be allocated by the encoder
        pkt.size = 0;
    
        fflush(stdout);
    picture->data[0] = Output;
    ret = avcodec_encode_video2(c, &pkt, picture,&got_output);
    
        fprintf(stderr,"ret = %d, got-out = %d \n",ret,got_output);
         if (ret < 0) {
                    __android_log_write(ANDROID_LOG_ERROR, "error","encoding");
            if(got_output > 0)
            __android_log_write(ANDROID_LOG_ERROR, "got_output","is non-zero");
    
        }
    
        if (got_output) {
            fprintf(stderr,"encoding frame %3d (size=%5d): (ret=%d)\n", 1, pkt.size,ret);
            fprintf(stderr,"before caling decode");
            decode_inline(&pkt); //function that decodes right after the encode
            fprintf(stderr,"after caling decode");
    
    
            av_free_packet(&pkt);
        }
    
    
    fprintf(stderr,"y val: %d \n",y);
    
    
    (*env)->ReleaseByteArrayElements(env, buffer, Output, 0);
    return ((ret));
    }
    

    decode_inline() function

    decode_inline(AVPacket *avpkt){
    AVCodec *codec;
    AVCodecContext *c = NULL;
    int frame, got_picture, len = -1,temp=0;
    
    AVFrame *rawFrame, *rgbFrame;
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    char buf[1024];
    char rawBuf[768000],rgbBuf[768000];
    
    struct SwsContext *sws_ctx;
    
    memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
    avcodec_register_all();
    
    c= avcodec_alloc_context3(codec);
    if(c == NULL)
        __android_log_write(ANDROID_LOG_ERROR, "avcodec","context-null");
    
    codec = avcodec_find_decoder(AV_CODEC_ID_H264);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        fprintf(stderr, "codec = %p \n", codec);
        }
    c->pix_fmt = AV_PIX_FMT_YUV420P;
    c->width = 480;
    c->height = 800;
    
    rawFrame = av_frame_alloc();
    rgbFrame = av_frame_alloc();
    
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
        }
    sws_ctx = sws_getContext(c->width, c->height,/*PIX_FMT_RGB565BE*/
                PIX_FMT_YUV420P, c->width, c->height, AV_PIX_FMT_RGB565/*PIX_FMT_YUV420P*/,
                SWS_BILINEAR, NULL, NULL, NULL);
    
    
    frame = 0;
    
    unsigned short *decodedpixels = &rawBuf;
    rawFrame->data[0] = &rawBuf;
    rgbFrame->data[0] = &rgbBuf;
    
    fprintf(stderr,"size of avpkt %d \n",avpkt->size);
    temp = avpkt->size;
    while (temp > 0) {
            len = avcodec_decode_video2(c, rawFrame, &got_picture, avpkt);
    
            if (len < 0) {
                fprintf(stderr, "Error while decoding frame %d\n", frame);
                exit(1);
                }
            temp -= len;
            avpkt->data += len;
    
            if (got_picture) {
                printf("saving frame %3d\n", frame);
                fflush(stdout);
            //TODO  
            //memcpy(decodedpixels,rawFrame->data[0],rawFrame->linesize[0]); 
            //  decodedpixels +=rawFrame->linesize[0];
    
                frame++;
                }
    
            }
    
    avcodec_close(c);
    av_free(c);
    //free(rawBuf);
    //free(rgbBuf);
    av_frame_free(&rawFrame);
    av_frame_free(&rgbFrame);
    

    }

    The log i get

    For the decode_inline() function:


    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] non-existing PPS 0 referenced
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] decode_slice_header error
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] non-existing PPS 0 referenced
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] decode_slice_header error
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] non-existing PPS 0 referenced
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] decode_slice_header error
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] non-existing PPS 0 referenced
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] decode_slice_header error
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] non-existing PPS 0 referenced
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] decode_slice_header error
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] non-existing PPS 0 referenced
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] decode_slice_header error
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] non-existing PPS 0 referenced
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] decode_slice_header error
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] non-existing PPS 0 referenced
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] decode_slice_header error
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] non-existing PPS 0 referenced
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] decode_slice_header error
    01-02 14:50:50.160: I/stderr(3407): [h264 @ 0x8db540] Invalid mix of idr and non-idr slices
    01-02 14:50:50.160: I/stderr(3407): Error while decoding frame 0
    

    Edit: Changing GOP value:

    If i change c->gop_size = 3; as expected it emits one I frame every three frames. The non-existing PPS 0 referenced message is not there for in every third execution, but all other have this message

  • Flutter PNG screenshots are not working properly with FFMPEG

    7 juin 2023, par segga

    I'm trying to convert screenshots I took in my flutter app programmatically to an .mp4 video, but the video is just black/blank. I'm using PNG to encode the images saved and the .PNG extension too. This is the code base I'm currently using and I expected to be able to use these images with ffmpeg. I also tried the ImageFormat.rawRgba or so but I don't think I used that properly

    import 'dart:io';
    import 'package:flutter/rendering.dart';
    
    void captureScreenshot() async {
      try {
        RenderRepaintBoundary boundary = globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
        ui.Image image = await boundary.toImage(pixelRatio: 1.0);
        ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
        Uint8List imageData = byteData!.buffer.asUint8List();
    
        // Save the image to a file
        File file = File('/path/to/screenshot.png');
        await file.writeAsBytes(imageData);
        print('Screenshot saved successfully!');
      } catch (e) {
        print('Error capturing screenshot: $e');
      }
    }