Newest 'x264' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Unknown encoder 'libx264'

    19 décembre 2023, par why

    I installed ffmpeg 0.8.9 on ubuntu11 by

    ./configure --enable-gpl --enable-nonfree --enable-pthreads --enable-libfaac --enable-libmp3lame --enable-libx264

    When I run it

    ffmpeg -y -i test.mp4 -f mpegts -acodec libmp3lame -ar 48000 -ab 64k -vcodec libx264 -b 250k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 250k -maxrate 250k -bufsize 250k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 2 a.ts
    

    It said

    Unknown encoder 'libx264'

    (Note: the same error could occour with avconv)

    How can I fix this? Thanks!

  • Encode NV12 frames to h264 using x264enc (appsrc and appsink)

    9 octobre 2023, par Abdul Rehman

    I am trying to encode NV12 frames to h264 files. For that I found a following code that is encoding raw frames to jpeg using jpegenc. The code works fine with jpeg encoder after some minor changes for NV12 frames. However x264enc encode only first frame and the rest of them are outputting the pps error when running with mplayer.

    First frame output

    [h264 @ 0x7f1ea2ed3600]decoding for stream 0 failed
    [lavf] stream 0: video (h264), -vid 0
    VIDEO:  [H264]  1920x1080  0bpp  1.000 fps    0.0 kbps ( 0.0 kbyte/s)
    ==========================================================================
    Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
    libavcodec version 58.54.100 (external)
    Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
    ==========================================================================
    Load subtitles in ./
    Audio: no sound
    Starting playback...
    Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
    VO: [vdpau] 1920x1080 => 1920x1080 Planar YV12
    

    Error frame output:

    
    [h264 @ 0x7f9c2e44f4c0]non-existing PPS 0 referenced
    [h264 @ 0x7f9c2e44f4c0]non-existing PPS 0 referenced
    [h264 @ 0x7f9c2e44f4c0]decode_slice_header error
    
    [h264 @ 0x7f9c2eece600]decoding for stream 0 failed
    [h264 @ 0x7f9c2eece600]Could not find codec parameters for stream 0 (Video: h264, none): unspecified size
    Consider increasing the value for the 'analyzeduration' and 'probesize' options
    [lavf] stream 0: video (h264), -vid 0
    VIDEO:  [H264]  0x0  0bpp  25.000 fps    0.0 kbps ( 0.0 kbyte/s)
    ==========================================================================
    Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
    libavcodec version 58.54.100 (external)
    Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
    

    I have changed the pipeline, block size and added queue. Pipeline:

    pipeline = gst_pipeline_new("mypipeline");
    appsrc = gst_element_factory_make("appsrc", "mysource");
    queue1 = gst_element_factory_make("queue", "myqueue1");
    jpegenc = gst_element_factory_make("x264enc", "myenc");
    //parser = gst_element_factory_make("h264parse", "parser");
    appsink = gst_element_factory_make("appsink", "mysink");
    
    
    ...
    g_object_set(G_OBJECT(appsrc), "format", GST_FORMAT_TIME, "is-live", TRUE, "do-timestamp", TRUE, NULL);
    g_object_set(G_OBJECT(jpegenc), "tune", 0x00000004, NULL); // 0x00000004 is the ultrafast preset
    
    ...
    capsappsrc2Jpegenc = gst_caps_new_simple("video/x-raw", "format",
                G_TYPE_STRING, "NV12", "width", G_TYPE_INT, width, "height",
                G_TYPE_INT, height, "framerate", GST_TYPE_FRACTION, 1, 1, 
                "interlace-mode", G_TYPE_STRING, "progressive",
                "colorimetry", G_TYPE_STRING, "bt601",
                NULL);
    
        // blocksize is important for jpegenc to know how many data to expect from appsrc in a single frame, too
        char szTemp[64];
        sprintf(szTemp, "%d", 3110400);
        g_object_set(G_OBJECT (appsrc), "blocksize", szTemp,
                NULL);
    
    
  • 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

  • First/single frame encoded with ffmpeg/libavcodec library cannot be immediately decoded

    11 septembre 2023, par Marek Kijo

    I'm using libavcodec library and h264 codec to prepare the video stream on one end, transmit the encoded frames to the other PC and there decode it.

    What I noticed after receiving very first packet (first encoded video frame) and feeding decoder with it, it is not possible to decode that frame. Only when I receive another frame the first one can be decoded but 'current' one not. So in the end I have constantly one frame delay on the decoder side.

    I was trying different presets (focusing rather on 'ultrafast'), also 'zerolatency' tune, also whole variety of bit_rate values of AVCodecContext.

    I also tried to flush (with nullptr packet) after injecting first frame data, just to check if it is maybe because of some internal buffers optimization - the frame still not decoded. Experimenting with other codecs (like mpeg4) gives even worse 'dalay' in number of frames to the point when when first frames can become decodable.

    Is it normal, unavoidable because of some internal mechanisms? Otherwise how I can achieve real zero latency.

    Supplementary setup information:

    • max_b_frames set to 0 (higher value gives even more delay)
    • pix_fmt set to AV_PIX_FMT_YUV420P

    edit:

    Answering some comment question:

    (1) What is the decoder (or playback system)?

    Custom decoder written using libavcodec, the decoded frames are later displayed on screen by OpenGL.

    • initialization:
    parser_ = av_parser_init(AV_CODEC_ID_H264);
    codec_ = avcodec_find_decoder(AV_CODEC_ID_H264);
    context_ = avcodec_alloc_context3(codec_);
    context_->width = 1024;
    context_->height = 768;
    context_->thread_count = 1;
    if ((codec_->capabilities & AV_CODEC_CAP_TRUNCATED) == 0)
    {
      context_->flags |= AV_CODEC_FLAG_TRUNCATED;
    }
    if (avcodec_open2(context_, codec_, nullptr) < 0)
    {
      throw std::runtime_error{"avcodec_open2 failed"};
    }
    avcodec_flush_buffers(context_);
    
    • then player periodically calls of the method of decoder that suppose to check if the another frame can be retrieved and displayed:
    auto result = avcodec_receive_frame(context_, frame_);
    if (!buffer_.empty())
    {  // upload another packet for decoding
      int used;
      if (upload_package(buffer_.data(), buffer_.size(), &used))
      {
        buffer_.erase(buffer_.begin(), buffer_.begin() + used);
      }
    }
    if (result == AVERROR(EAGAIN) || result == AVERROR_EOF || result < 0)
    {
      return false;
    }
    yuv_to_rgb();
    return true;
    

    boolean return value informs if the decoding succeeded, and every time the buffer where the incomming packets are stored is checked and uploaded to libavcodec decoder

    • and that is how the method that uploads the buffer looks like:
    bool upload_package(const void* data, const std::size_t size, int* used)
    {
      auto result = av_parser_parse2(parser_, context_, &packet_->data, &packet_->size, reinterpret_cast(data), size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
      if (result < 0)
      {
        return false;
      }
      *used = result;
      if (packet_->size != 0)
      {
        result = avcodec_send_packet(context_, packet_);
        if (result < 0)
        {
          return false;
        }
      }
      return true;
    }
    

    (2) If possible, save each one as a .bin file and then share the links with us for testing.

    I will try to figure out something...

    (3) Show example C++ code of your encoder settings for H264...

    • initialization:
    codec_ = avcodec_find_encoder(AV_CODEC_ID_H264);
    context_ = avcodec_alloc_context3(codec_);
    
    context_->bit_rate = 1048576; // 1xMbit;
    context_->width = 1024;
    context_->height = 768;
    context_->time_base = {1, 30}; // 30 fps
    context_->pix_fmt = AV_PIX_FMT_YUV420P;
    context_->thread_count = 1;
    
    av_opt_set(context_->priv_data, "preset", "ultrafast", 0);
    av_opt_set(context_->priv_data, "tune", "zerolatency", 0);
    avcodec_open2(context_, codec_, nullptr);
    
    frame_->format = AV_PIX_FMT_YUV420P;
    frame_->width = 1024;
    frame_->height = 768;
    av_image_alloc(frame_->data, frame_->linesize, 1024, 768, AV_PIX_FMT_YUV420P, 32);
    
    • frame encoding:
    rgb_to_yuv();
    frame_->pts = frame_num_++;
    auto result = avcodec_send_frame(context_, frame_);
    while (result >= 0)
    {
      result = avcodec_receive_packet(context_, packet_);
      if (result == AVERROR(EAGAIN) || result == AVERROR_EOF)
      {
        return;
      }
      else if (result < 0)
      {
        throw std::runtime_error{"avcodec_receive_packet failed"};
      }
      // here the packet is send to the decoder, the whole packet is stored on the mentioned before buffer_ and uploaded with avcodec_send_packet
      // I can also add that the whole buffer/packet us uploaded at once
      stream_video_data(packet_->data, packet_->size); 
    }
    av_packet_unref(packet_);
    }
    

    edit2:

    I think I figured out the issue that I had.

    For every incoming data packet (encoded frame) I was calling first av_parser_parse2, and then I was sending the data through avcodec_send_packet. And I was not recalling that procedure having empty buffer_, so for the first frame data the av_parser_parse2 was never called after uploading it through avcodec_send_packet, for the second frame it was called and the first frame was parsed, so it could be properly decoded, but for that (second) frame the parse2 was also not called, and so on ...

    So the issue in my case was wrong sequence of av_parser_parse2 and avcodec_send_packet to handle the encoded data.

  • building ffmpeg for windows x264.h : No such file or directory

    22 août 2023, par 王伯荣

    I am building ffmpeg using MSYS2 in windows system.

    My compilation package is "i686-8.1.0-release-posix-dwarf" of mingw.

    My configure option parameters are as follows:

    ./configure --prefix="D:\fkit\b" --disable-static --enable-shared --enable-libx264 --enable-gpl --enable-version3 --enable-nonfree --enable-ffserver --enable-ffplay --disable-debug --disable-iconv --disable-zlib --extra-ldflags="-LD:\fkit\xb\lib -lx264" --extra-cflags="-ID:\ fkit\xb\include" --pkgconfigdir="D:\fkit\xb\lib\pkgconfig"

    As you can see, "D:fkit\xb" is my libx264 installation directory. I configured --extra-cflags and --extra-ldflags, and the configure script executed fine. But when I execute mingw32-make.exe, it prompts "libavcodec/libx264.c:36:10: fatal error: x264.h: No such file or directory"

    I don't have any ideas because everything I do seems normal.