Recherche avancée

Médias (2)

Mot : - Tags -/doc2img

Autres articles (108)

  • Les formats acceptés

    28 janvier 2010, par

    Les commandes suivantes permettent d’avoir des informations sur les formats et codecs gérés par l’installation local de ffmpeg :
    ffmpeg -codecs ffmpeg -formats
    Les format videos acceptés en entrée
    Cette liste est non exhaustive, elle met en exergue les principaux formats utilisés : h264 : H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 m4v : raw MPEG-4 video format flv : Flash Video (FLV) / Sorenson Spark / Sorenson H.263 Theora wmv :
    Les formats vidéos de sortie possibles
    Dans un premier temps on (...)

  • Encoding and processing into web-friendly formats

    13 avril 2011, par

    MediaSPIP automatically converts uploaded files to internet-compatible formats.
    Video files are encoded in MP4, Ogv and WebM (supported by HTML5) and MP4 (supported by Flash).
    Audio files are encoded in MP3 and Ogg (supported by HTML5) and MP3 (supported by Flash).
    Where possible, text is analyzed in order to retrieve the data needed for search engine detection, and then exported as a series of image files.
    All uploaded files are stored online in their original format, so you can (...)

  • Ajouter notes et légendes aux images

    7 février 2011, par

    Pour pouvoir ajouter notes et légendes aux images, la première étape est d’installer le plugin "Légendes".
    Une fois le plugin activé, vous pouvez le configurer dans l’espace de configuration afin de modifier les droits de création / modification et de suppression des notes. Par défaut seuls les administrateurs du site peuvent ajouter des notes aux images.
    Modification lors de l’ajout d’un média
    Lors de l’ajout d’un média de type "image" un nouveau bouton apparait au dessus de la prévisualisation (...)

Sur d’autres sites (6626)

  • FFMpeg encoded video will only play in FFPlay

    8 novembre 2013, par mohM

    I've been debugging my program for a couple of weeks now with the output video only showing a blank screen (was testing with VLC, WMP and WMPClassic). I happened to try using FFPlay and lo and behold the video works perfectly. I've read that this is usually caused by an incorrect pixel format, and that switching to PIX_FMT_YUV420P will make it work universally...but I'm already using that pixel format in the encoding process. Is there anything else that is causing this ?

    AVCodec* codec;
    AVCodecContext* c = NULL;
    uint8_t* outbuf;
    int i, out_size, outbuf_size;

    avcodec_register_all();

    printf("Video encoding\n");

    // Find the mpeg1 video encoder
    codec = avcodec_find_encoder(CODEC_ID_H264);
    if (!codec) {
       fprintf(stderr, "Codec not found\n");
       exit(1);
    }
    else printf("H264 codec found\n");

    c = avcodec_alloc_context3(codec);

    c->bit_rate = 400000;
    c->width = 1920;                                        // resolution must be a multiple of two (1280x720),(1900x1080),(720x480)
    c->height = 1200;
    c->time_base.num = 1;                                   // framerate numerator
    c->time_base.den = 25;                                  // framerate denominator
    c->gop_size = 10;                                       // emit one intra frame every ten frames
    c->max_b_frames = 1;                                    // maximum number of b-frames between non b-frames
    //c->keyint_min = 1;                                        // minimum GOP size
    //c->i_quant_factor = (float)0.71;                      // qscale factor between P and I frames
    //c->b_frame_strategy = 20;
    //c->qcompress = (float)0.6;
    //c->qmin = 20;                                         // minimum quantizer
    //c->qmax = 51;                                         // maximum quantizer
    //c->max_qdiff = 4;                                     // maximum quantizer difference between frames
    //c->refs = 4;                                          // number of reference frames
    //c->trellis = 1;                                           // trellis RD Quantization
    c->pix_fmt = PIX_FMT_YUV420P;
    c->codec_id = CODEC_ID_H264;
    //c->codec_type = AVMEDIA_TYPE_VIDEO;

    // Open the encoder
    if (avcodec_open2(c, codec,NULL) < 0) {
       fprintf(stderr, "Could not open codec\n");
       exit(1);
    }
    else printf("H264 codec opened\n");

    outbuf_size = 100000 + c->width*c->height*(32>>3);//*(32>>3);           // alloc image and output buffer
    outbuf = static_cast(malloc(outbuf_size));
    printf("Setting buffer size to: %d\n",outbuf_size);

    FILE* f = fopen("example.mpg","wb");
    if(!f) printf("x  -  Cannot open video file for writing\n");
    else printf("Opened video file for writing\n");

    // encode 5 seconds of video
    for(i=0;iwidth, c->height);
       uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes*sizeof(uint8_t));

       AVFrame* inpic = avcodec_alloc_frame();
       AVFrame* outpic = avcodec_alloc_frame();

       outpic->pts = (int64_t)((float)i * (1000.0/((float)(c->time_base.den))) * 90);
       avpicture_fill((AVPicture*)inpic, (uint8_t*)pPixels, PIX_FMT_RGB32, c->width, c->height);                   // Fill picture with image
       avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, c->width, c->height);
       av_image_alloc(outpic->data, outpic->linesize, c->width, c->height, c->pix_fmt, 1);

       inpic->data[0] += inpic->linesize[0]*(screenHeight-1);                                                      // Flipping frame
       inpic->linesize[0] = -inpic->linesize[0];                                                                   // Flipping frame

       struct SwsContext* fooContext = sws_getContext(screenWidth, screenHeight, PIX_FMT_RGB32, c->width, c->height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
       sws_scale(fooContext, inpic->data, inpic->linesize, 0, c->height, outpic->data, outpic->linesize);

       // encode the image
       out_size = avcodec_encode_video(c, outbuf, outbuf_size, outpic);
       printf("Encoding frame %3d (size=%5d)\n", i, out_size);
       fwrite(outbuf, 1, out_size, f);
       delete [] pPixels;
       av_free(outbuffer);    
       av_free(inpic);
       av_free(outpic);
    }

    // get the delayed frames
    for(; out_size; i++) {
       fflush(stdout);

       out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
       printf("Writing frame %3d (size=%5d)\n", i, out_size);
       fwrite(outbuf, 1, out_size, f);
    }

    // add sequence end code to have a real mpeg file
    outbuf[0] = 0x00;
    outbuf[1] = 0x00;
    outbuf[2] = 0x01;
    outbuf[3] = 0xb7;
    fwrite(outbuf, 1, 4, f);
    fclose(f);

    avcodec_close(c);
    free(outbuf);
    av_free(c);
    printf("Closed codec and Freed\n");
  • Ffmpeg in Android Studio

    7 décembre 2016, par Ahmed Abd-Elmeged

    i’am trying to add Ffmpeg library to use it in Live Streaming app based in Youtube APi when i add it and i use Cmake Build tool the header files only shown in the c native class which i create and i want it to be included in the other header files
    which came with library how can i do it in cmake script or onther way ?

    This my cmake build script

    # Sets the minimum version of CMake required to build the native
    # library. You should either keep the default value or only pass a
    # value of 3.4.0 or lower.

    cmake_minimum_required(VERSION 3.4.1)

    # Creates and names a library, sets it as either STATIC
    # or SHARED, and provides the relative paths to its source code.
    # You can define multiple libraries, and CMake builds it for you.
    # Gradle automatically packages shared libraries with your APK.

    add_library( # Sets the name of the library.
                ffmpeg-jni

                # Sets the library as a shared library.
                SHARED

                # Provides a relative path to your source file(s).
                # Associated headers in the same location as their source
                # file are automatically included.
                src/main/cpp/ffmpeg-jni.c )

    # Searches for a specified prebuilt library and stores the path as a
    # variable. Because system libraries are included in the search path by
    # default, you only need to specify the name of the public NDK library
    # you want to add. CMake verifies that the library exists before
    # completing its build.

    find_library( # Sets the name of the path variable.
                 log-lib

                 # Specifies the name of the NDK library that
                 # you want CMake to locate.
                 log )

    # Specifies libraries CMake should link to your target library. You
    # can link multiple libraries, such as libraries you define in the
    # build script, prebuilt third-party libraries, or system libraries.

    target_link_libraries( # Specifies the target library.
                          ffmpeg-jni

                          # Links the target library to the log library
                          # included in the NDK.
                          ${log-lib} )

    include_directories(src/main/cpp/include/ffmpeglib)

    include_directories(src/main/cpp/include/libavcodec)

    include_directories(src/main/cpp/include/libavformat)

    include_directories(src/main/cpp/include/libavutil)

    This my native class i create as native file i android studio and i also have an error in jintJNIcall not found

    #include <android></android>log.h>
    #include
    #include

    #include "libavcodec/avcodec.h"
    #include "libavformat/avformat.h"
    #include "libavutil/opt.h"



    #ifdef __cplusplus
    extern "C" {
    #endif

    JNIEXPORT jboolean
    JNICALL Java_com_example_venrto_ffmpegtest_Ffmpeg_init(JNIEnv *env, jobject thiz,
                                                            jint width, jint height,
                                                            jint audio_sample_rate,
                                                            jstring rtmp_url);
    JNIEXPORT void JNICALL Java_com_example_venrto_ffmpegtest_Ffmpeg_shutdown(JNIEnv
                                                                               *env,
                                                                               jobject thiz
    );
    JNIEXPORT jintJNICALL
           Java_com_example_venrto_ffmpegtest_Ffmpeg_encodeVideoFrame(JNIEnv
                                                                        *env,
                                                                        jobject thiz,
                                                                        jbyteArray
                                                                        yuv_image);
    JNIEXPORT jint
    JNICALL Java_com_example_venrto_ffmpegtest_Ffmpeg_encodeAudioFrame(JNIEnv *env,
                                                                        jobject thiz,
                                                                        jshortArray audio_data,
                                                                        jint length);

    #ifdef __cplusplus
    }
    #endif

    #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, "ffmpeg-jni", __VA_ARGS__)
    #define URL_WRONLY 2
    static AVFormatContext *fmt_context;
    static AVStream *video_stream;
    static AVStream *audio_stream;

    static int pts
           = 0;
    static int last_audio_pts = 0;

    // Buffers for UV format conversion
    static unsigned char *u_buf;
    static unsigned char *v_buf;

    static int enable_audio = 1;
    static int64_t audio_samples_written = 0;
    static int audio_sample_rate = 0;

    // Stupid buffer for audio samples. Not even a proper ring buffer
    #define AUDIO_MAX_BUF_SIZE 16384  // 2x what we get from Java
    static short audio_buf[AUDIO_MAX_BUF_SIZE];
    static int audio_buf_size = 0;

    void AudioBuffer_Push(const short *audio, int num_samples) {
       if (audio_buf_size >= AUDIO_MAX_BUF_SIZE - num_samples) {
           LOGI("AUDIO BUFFER OVERFLOW: %i + %i > %i", audio_buf_size, num_samples,
                AUDIO_MAX_BUF_SIZE);
           return;
       }
       for (int i = 0; i &lt; num_samples; i++) {
           audio_buf[audio_buf_size++] = audio[i];
       }
    }

    int AudioBuffer_Size() { return audio_buf_size; }

    short *AudioBuffer_Get() { return audio_buf; }

    void AudioBuffer_Pop(int num_samples) {
       if (num_samples > audio_buf_size) {
           LOGI("Audio buffer Pop WTF: %i vs %i", num_samples, audio_buf_size);
           return;
       }
       memmove(audio_buf, audio_buf + num_samples, num_samples * sizeof(short));
       audio_buf_size -= num_samples;
    }

    void AudioBuffer_Clear() {
       memset(audio_buf, 0, sizeof(audio_buf));
       audio_buf_size = 0;
    }

    static void log_callback(void *ptr, int level, const char *fmt, va_list vl) {
       char x[2048];
       vsnprintf(x, 2048, fmt, vl);
       LOGI(x);
    }

    JNIEXPORT jboolean
    JNICALL Java_com_example_venrto_ffmpegtest_Ffmpeg_init(JNIEnv *env, jobject thiz,
                                                            jint width, jint height,
                                                            jint audio_sample_rate_param,
                                                            jstring rtmp_url) {
       avcodec_register_all();
       av_register_all();
       av_log_set_callback(log_callback);

       fmt_context = avformat_alloc_context();
       AVOutputFormat *ofmt = av_guess_format("flv", NULL, NULL);
       if (ofmt) {
           LOGI("av_guess_format returned %s", ofmt->long_name);
       } else {
           LOGI("av_guess_format fail");
           return JNI_FALSE;
       }

       fmt_context->oformat = ofmt;
       LOGI("creating video stream");
       video_stream = av_new_stream(fmt_context, 0);

       if (enable_audio) {
           LOGI("creating audio stream");
           audio_stream = av_new_stream(fmt_context, 1);
       }

       // Open Video Codec.
       // ======================
       AVCodec *video_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
       if (!video_codec) {
           LOGI("Did not find the video codec");
           return JNI_FALSE;  // leak!
       } else {
           LOGI("Video codec found!");
       }
       AVCodecContext *video_codec_ctx = video_stream->codec;
       video_codec_ctx->codec_id = video_codec->id;
       video_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
       video_codec_ctx->level = 31;

       video_codec_ctx->width = width;
       video_codec_ctx->height = height;
       video_codec_ctx->pix_fmt = PIX_FMT_YUV420P;
       video_codec_ctx->rc_max_rate = 0;
       video_codec_ctx->rc_buffer_size = 0;
       video_codec_ctx->gop_size = 12;
       video_codec_ctx->max_b_frames = 0;
       video_codec_ctx->slices = 8;
       video_codec_ctx->b_frame_strategy = 1;
       video_codec_ctx->coder_type = 0;
       video_codec_ctx->me_cmp = 1;
       video_codec_ctx->me_range = 16;
       video_codec_ctx->qmin = 10;
       video_codec_ctx->qmax = 51;
       video_codec_ctx->keyint_min = 25;
       video_codec_ctx->refs = 3;
       video_codec_ctx->trellis = 0;
       video_codec_ctx->scenechange_threshold = 40;
       video_codec_ctx->flags |= CODEC_FLAG_LOOP_FILTER;
       video_codec_ctx->me_method = ME_HEX;
       video_codec_ctx->me_subpel_quality = 6;
       video_codec_ctx->i_quant_factor = 0.71;
       video_codec_ctx->qcompress = 0.6;
       video_codec_ctx->max_qdiff = 4;
       video_codec_ctx->time_base.den = 10;
       video_codec_ctx->time_base.num = 1;
       video_codec_ctx->bit_rate = 3200 * 1000;
       video_codec_ctx->bit_rate_tolerance = 0;
       video_codec_ctx->flags2 |= 0x00000100;

       fmt_context->bit_rate = 4000 * 1000;

       av_opt_set(video_codec_ctx, "partitions", "i8x8,i4x4,p8x8,b8x8", 0);
       av_opt_set_int(video_codec_ctx, "direct-pred", 1, 0);
       av_opt_set_int(video_codec_ctx, "rc-lookahead", 0, 0);
       av_opt_set_int(video_codec_ctx, "fast-pskip", 1, 0);
       av_opt_set_int(video_codec_ctx, "mixed-refs", 1, 0);
       av_opt_set_int(video_codec_ctx, "8x8dct", 0, 0);
       av_opt_set_int(video_codec_ctx, "weightb", 0, 0);

       if (fmt_context->oformat->flags &amp; AVFMT_GLOBALHEADER)
           video_codec_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;

       LOGI("Opening video codec");
       AVDictionary *vopts = NULL;
       av_dict_set(&amp;vopts, "profile", "main", 0);
       //av_dict_set(&amp;vopts, "vprofile", "main", 0);
       av_dict_set(&amp;vopts, "rc-lookahead", 0, 0);
       av_dict_set(&amp;vopts, "tune", "film", 0);
       av_dict_set(&amp;vopts, "preset", "ultrafast", 0);
       av_opt_set(video_codec_ctx->priv_data, "tune", "film", 0);
       av_opt_set(video_codec_ctx->priv_data, "preset", "ultrafast", 0);
       av_opt_set(video_codec_ctx->priv_data, "tune", "film", 0);
       int open_res = avcodec_open2(video_codec_ctx, video_codec, &amp;vopts);
       if (open_res &lt; 0) {
           LOGI("Error opening video codec: %i", open_res);
           return JNI_FALSE;   // leak!
       }

       // Open Audio Codec.
       // ======================

       if (enable_audio) {
           AudioBuffer_Clear();
           audio_sample_rate = audio_sample_rate_param;
           AVCodec *audio_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
           if (!audio_codec) {
               LOGI("Did not find the audio codec");
               return JNI_FALSE;  // leak!
           } else {
               LOGI("Audio codec found!");
           }
           AVCodecContext *audio_codec_ctx = audio_stream->codec;
           audio_codec_ctx->codec_id = audio_codec->id;
           audio_codec_ctx->codec_type = AVMEDIA_TYPE_AUDIO;
           audio_codec_ctx->bit_rate = 128000;
           audio_codec_ctx->bit_rate_tolerance = 16000;
           audio_codec_ctx->channels = 1;
           audio_codec_ctx->profile = FF_PROFILE_AAC_LOW;
           audio_codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLT;
           audio_codec_ctx->sample_rate = 44100;

           LOGI("Opening audio codec");
           AVDictionary *opts = NULL;
           av_dict_set(&amp;opts, "strict", "experimental", 0);
           open_res = avcodec_open2(audio_codec_ctx, audio_codec, &amp;opts);
           LOGI("audio frame size: %i", audio_codec_ctx->frame_size);

           if (open_res &lt; 0) {
               LOGI("Error opening audio codec: %i", open_res);
               return JNI_FALSE;   // leak!
           }
       }

       const jbyte *url = (*env)->GetStringUTFChars(env, rtmp_url, NULL);

       // Point to an output file
       if (!(ofmt->flags &amp; AVFMT_NOFILE)) {
           if (avio_open(&amp;fmt_context->pb, url, URL_WRONLY) &lt; 0) {
               LOGI("ERROR: Could not open file %s", url);
               return JNI_FALSE;  // leak!
           }
       }
       (*env)->ReleaseStringUTFChars(env, rtmp_url, url);

       LOGI("Writing output header.");
       // Write file header
       if (avformat_write_header(fmt_context, NULL) != 0) {
           LOGI("ERROR: av_write_header failed");
           return JNI_FALSE;
       }

       pts = 0;
       last_audio_pts = 0;
       audio_samples_written = 0;

       // Initialize buffers for UV format conversion
       int frame_size = video_codec_ctx->width * video_codec_ctx->height;
       u_buf = (unsigned char *) av_malloc(frame_size / 4);
       v_buf = (unsigned char *) av_malloc(frame_size / 4);

       LOGI("ffmpeg encoding init done");
       return JNI_TRUE;
    }

    JNIEXPORT void JNICALL
    Java_com_example_venrto_ffmpegtest_Ffmpeg_shutdown(JNIEnv
                                                        *env,
                                                        jobject thiz
    ) {
       av_write_trailer(fmt_context);
       avio_close(fmt_context
                          ->pb);
       avcodec_close(video_stream
                             ->codec);
       if (enable_audio) {
           avcodec_close(audio_stream
                                 ->codec);
       }
       av_free(fmt_context);
       av_free(u_buf);
       av_free(v_buf);

       fmt_context = NULL;
       u_buf = NULL;
       v_buf = NULL;
    }

    JNIEXPORT jintJNICALL
    Java_com_example_venrto_ffmpegtest_Ffmpeg_encodeVideoFrame(JNIEnv
                                                                *env,
                                                                jobject thiz,
                                                                jbyteArray
                                                                yuv_image) {
       int yuv_length = (*env)->GetArrayLength(env, yuv_image);
       unsigned char *yuv_data = (*env)->GetByteArrayElements(env, yuv_image, 0);

       AVCodecContext *video_codec_ctx = video_stream->codec;
    //LOGI("Yuv size: %i w: %i h: %i", yuv_length, video_codec_ctx->width, video_codec_ctx->height);

       int frame_size = video_codec_ctx->width * video_codec_ctx->height;

       const unsigned char *uv = yuv_data + frame_size;

    // Convert YUV from NV12 to I420. Y channel is the same so we don't touch it,
    // we just have to deinterleave UV.
       for (
               int i = 0;
               i &lt; frame_size / 4; i++) {
           v_buf[i] = uv[i * 2];
           u_buf[i] = uv[i * 2 + 1];
       }

       AVFrame source;
       memset(&amp;source, 0, sizeof(AVFrame));
       source.data[0] =
               yuv_data;
       source.data[1] =
               u_buf;
       source.data[2] =
               v_buf;
       source.linesize[0] = video_codec_ctx->
               width;
       source.linesize[1] = video_codec_ctx->width / 2;
       source.linesize[2] = video_codec_ctx->width / 2;

    // only for bitrate regulation. irrelevant for sync.
       source.
               pts = pts;
       pts++;

       int out_length = frame_size + (frame_size / 2);
       unsigned char *out = (unsigned char *) av_malloc(out_length);
       int compressed_length = avcodec_encode_video(video_codec_ctx, out, out_length, &amp;source);

       (*env)->
               ReleaseByteArrayElements(env, yuv_image, yuv_data,
                                        0);

    // Write to file too
       if (compressed_length > 0) {
           AVPacket pkt;
           av_init_packet(&amp;pkt);
           pkt.
                   pts = last_audio_pts;
           if (video_codec_ctx->coded_frame &amp;&amp; video_codec_ctx->coded_frame->key_frame) {
               pkt.flags |= 0x0001;
           }
           pkt.
                   stream_index = video_stream->index;
           pkt.
                   data = out;
           pkt.
                   size = compressed_length;
           if (
                   av_interleaved_write_frame(fmt_context,
                                              &amp;pkt) != 0) {
               LOGI("Error writing video frame");
           }
       } else {
           LOGI("??? compressed_length &lt;= 0");
       }

       last_audio_pts++;

       av_free(out);
       return
               compressed_length;
    }

    JNIEXPORT jintJNICALL
    Java_com_example_venrto_ffmpegtest_Ffmpeg_encodeAudioFrame(JNIEnv
                                                                *env,
                                                                jobject thiz,
                                                                jshortArray
                                                                audio_data,
                                                                jint length
    ) {
       if (!enable_audio) {
           return 0;
       }

       short *audio = (*env)->GetShortArrayElements(env, audio_data, 0);
    //LOGI("java audio buffer size: %i", length);

       AVCodecContext *audio_codec_ctx = audio_stream->codec;

       unsigned char *out = av_malloc(128000);

       AudioBuffer_Push(audio, length
       );

       int total_compressed = 0;
       while (

               AudioBuffer_Size()

               >= audio_codec_ctx->frame_size) {
           AVPacket pkt;
           av_init_packet(&amp;pkt);

           int compressed_length = avcodec_encode_audio(audio_codec_ctx, out, 128000,
                                                        AudioBuffer_Get());

           total_compressed +=
                   compressed_length;
           audio_samples_written += audio_codec_ctx->
                   frame_size;

           int new_pts = (audio_samples_written * 1000) / audio_sample_rate;
           if (compressed_length > 0) {
               pkt.
                       size = compressed_length;
               pkt.
                       pts = new_pts;
               last_audio_pts = new_pts;
    //LOGI("audio_samples_written: %i  comp_length: %i   pts: %i", (int)audio_samples_written, (int)compressed_length, (int)new_pts);
               pkt.flags |= 0x0001;
               pkt.
                       stream_index = audio_stream->index;
               pkt.
                       data = out;
               if (
                       av_interleaved_write_frame(fmt_context,
                                                  &amp;pkt) != 0) {
                   LOGI("Error writing audio frame");
               }
           }
           AudioBuffer_Pop(audio_codec_ctx
                                   ->frame_size);
       }

       (*env)->
               ReleaseShortArrayElements(env, audio_data, audio,
                                         0);

       av_free(out);
       return
               total_compressed;
    }

    this an example for a header file which i have an error include

    #include "libavutil/samplefmt.h"
    #include "libavutil/avutil.h"
    #include "libavutil/cpu.h"
    #include "libavutil/dict.h"
    #include "libavutil/log.h"
    #include "libavutil/pixfmt.h"
    #include "libavutil/rational.h"

    #include "libavutil/version.h"

    in the libavcodec/avcodec.h

    This code based in this example :
    https://github.com/youtube/yt-watchme

  • libavcodec : ffprobe on file encoded with FFV1 codec reports "read_quant_table error"

    20 novembre 2016, par Ali Alidoust

    I’m using the following code to encode a series of frames into an mkv or avi file with FFV1 encoding :

      HRESULT Session::createContext(LPCSTR filename, UINT width, UINT height, UINT fps_num, UINT fps_den) {
       LOG("Exporting to file: ", filename);

       AVCodecID codecId = AV_CODEC_ID_FFV1;
       this->pixelFormat = AV_PIX_FMT_YUV420P;

       this->codec = avcodec_find_encoder(codecId);
       RET_IF_NULL(this->codec, "Could not create codec", E_FAIL);

       this->oformat = av_guess_format(NULL, filename, NULL);
       RET_IF_NULL(this->oformat, "Could not create format", E_FAIL);
       this->oformat->video_codec = codecId;
       this->width = width;
       this->height = height;
       this->codecContext = avcodec_alloc_context3(this->codec);
       RET_IF_NULL(this->codecContext, "Could not allocate context for the codec", E_FAIL);

       this->codecContext->codec = this->codec;
       this->codecContext->codec_id = codecId;

       this->codecContext->pix_fmt = pixelFormat;
       this->codecContext->width = this->width;
       this->codecContext->height = this->height;
       this->codecContext->time_base.num = fps_den;
       this->codecContext->time_base.den = fps_num;

       this->codecContext->gop_size = 1;


       RET_IF_FAILED_AV(avformat_alloc_output_context2(&amp;fmtContext, this->oformat, NULL, NULL), "Could not allocate format context", E_FAIL);
       RET_IF_NULL(this->fmtContext, "Could not allocate format context", E_FAIL);

       this->fmtContext->oformat = this->oformat;
       this->fmtContext->video_codec_id = codecId;

       this->stream = avformat_new_stream(this->fmtContext, this->codec);
       RET_IF_NULL(this->stream, "Could not create new stream", E_FAIL);
       this->stream->time_base = this->codecContext->time_base;
       RET_IF_FAILED_AV(avcodec_parameters_from_context(this->stream->codecpar, this->codecContext), "Could not convert AVCodecContext to AVParameters", E_FAIL);

       if (this->fmtContext->oformat->flags &amp; AVFMT_GLOBALHEADER)
       {
           this->codecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
       }

       av_opt_set_int(this->codecContext->priv_data, "coder", 0, 0);
       av_opt_set_int(this->codecContext->priv_data, "context", 1, 0);
       av_opt_set_int(this->codecContext->priv_data, "slicecrc", 1, 0);
       //av_opt_set_int(this->codecContext->priv_data, "slicecrc", 1, 0);
       //av_opt_set_int(this->codecContext->priv_data, "pix_fmt", pixelFormat, 0);

       RET_IF_FAILED_AV(avcodec_open2(this->codecContext, this->codec, NULL), "Could not open codec", E_FAIL);
       RET_IF_FAILED_AV(avio_open(&amp;this->fmtContext->pb, filename, AVIO_FLAG_WRITE), "Could not open output file", E_FAIL);
       RET_IF_NULL(this->fmtContext->pb, "Could not open output file", E_FAIL);
       RET_IF_FAILED_AV(avformat_write_header(this->fmtContext, NULL), "Could not write header", E_FAIL);

       frame = av_frame_alloc();
       RET_IF_NULL(frame, "Could not allocate frame", E_FAIL);
       frame->format = this->codecContext->pix_fmt;
       frame->width = width;
       frame->height = height;
       return S_OK;
    }

    HRESULT Session::writeFrame(IMFSample * pSample) {
       IMFMediaBuffer *mediaBuffer = NULL;
       BYTE *pDataNV12 = NULL;
       DWORD length;

       RET_IF_FAILED(pSample->ConvertToContiguousBuffer(&amp;mediaBuffer), "Could not convert IMFSample to contagiuous buffer", E_FAIL);
       RET_IF_FAILED(mediaBuffer->GetCurrentLength(&amp;length), "Could not get buffer length", E_FAIL);
       RET_IF_FAILED(mediaBuffer->Lock(&amp;pDataNV12, NULL, NULL), "Could not lock the buffer", E_FAIL);
       BYTE *pDataYUV420P = new BYTE[length];
       this->convertNV12toYUV420P(pDataNV12, pDataYUV420P, this->width, this->height);
       RET_IF_FAILED(av_image_fill_arrays(frame->data, frame->linesize, pDataYUV420P, pixelFormat, this->width, this->height, 1), "Could not fill the frame with data from the buffer", E_FAIL);
       LOG_IF_FAILED(mediaBuffer->Unlock(), "Could not unlock the buffer");

       frame->pts = av_rescale_q(this->pts++, this->codecContext->time_base, this->stream->time_base);

       AVPacket pkt;

       av_init_packet(&amp;pkt);
       pkt.data = NULL;
       pkt.size = 0;

       RET_IF_FAILED_AV(avcodec_send_frame(this->codecContext, frame), "Could not send the frame to the encoder", E_FAIL);
       delete[] pDataYUV420P;
       if (SUCCEEDED(avcodec_receive_packet(this->codecContext, &amp;pkt))) {
           RET_IF_FAILED_AV(av_interleaved_write_frame(this->fmtContext, &amp;pkt), "Could not write the received packet.", E_FAIL);
       }

       av_packet_unref(&amp;pkt);

       return S_OK;
    }

    HRESULT Session::endSession() {
       LOG("Ending session...");

       LOG("Closing files...")
       LOG_IF_FAILED_AV(av_write_trailer(this->fmtContext), "Could not finalize the output file.");
       LOG_IF_FAILED_AV(avio_close(this->fmtContext->pb), "Could not close the output file.");
       LOG_IF_FAILED_AV(avcodec_close(this->codecContext), "Could not close the codec.");
       av_free(this->codecContext);
       LOG("Done.")
       return S_OK;
    }

    The problem is that the generated file is not playable in either VLC or MPC-HC. However, MPC-HC reports following info in file properties :

    General
    Unique ID                      : 202978442142665779317960161865934977227 (0x98B439D9BE859109BD5EC00A62A238CB)
    Complete name                  : T:\Test.mkv
    Format                         : Matroska
    Format version                 : Version 4 / Version 2
    File size                      : 24.6 MiB
    Duration                       : 147ms
    Overall bit rate               : 1 401 Mbps
    Writing application            : Lavf57.57.100
    Writing library                : Lavf57.57.100

    Video
    ID                             : 1
    Format                         : FFV1
    Format version                 : Version 0
    Codec ID                       : V_MS/VFW/FOURCC / FFV1
    Duration                       : 147ms
    Width                          : 1 280 pixels
    Height                         : 720 pixels
    Display aspect ratio           : 16:9
    Frame rate mode                : Constant
    Frame rate                     : 1 000.000 fps
    Color space                    : YUV
    Chroma subsampling             : 4:2:0
    Bit depth                      : 8 bits
    Compression mode               : Lossless
    Default                        : Yes
    Forced                         : No
    DURATION                       : 00:00:00.147000000
    coder_type                     : Golomb Rice

    Something to note is that it reports 1000 FPS which is weird since I’ve set AVCodecContext::time_base in the code.

    UPDATE 1 :

    I managed to set the correct fps by setting time_base property of the stream :

    this->stream->time_base.den = fps_num;
    this->stream->time_base.num = fps_den;

    VLC plays the output file but it shows VLC logo instead of the video, as if there is no video stream in the file.

    UPDATE 2 :

    Cleaned up the code. Now if I set codecId = AV_CODEC_ID_MPEG2VIDEO the output file is valid and is played in both VLC and MPC-HC. Using ffprobe on the file with FFV1 encoding yields the following result :

    C:\root\apps\ffmpeg>ffprobe.exe t:\test.avi
    ffprobe version 3.2 Copyright (c) 2007-2016 the FFmpeg developers
     built with gcc 5.4.0 (GCC)
     configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --disable-w32threads --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-libebur128 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
     libavutil      55. 34.100 / 55. 34.100
     libavcodec     57. 64.100 / 57. 64.100
     libavformat    57. 56.100 / 57. 56.100
     libavdevice    57.  1.100 / 57.  1.100
     libavfilter     6. 65.100 /  6. 65.100
     libswscale      4.  2.100 /  4.  2.100
     libswresample   2.  3.100 /  2.  3.100
     libpostproc    54.  1.100 / 54.  1.100
    [ffv1 @ 00000000006b83a0] read_quant_table error
    Input #0, avi, from 't:\test.avi':
     Metadata:
       encoder         : Lavf57.56.100
     Duration: 00:00:04.94, start: 0.000000, bitrate: 107005 kb/s
       Stream #0:0: Video: ffv1 (FFV1 / 0x31564646), yuv420p, 1280x720, 107717 kb/s, 29.97 fps, 29.97 tbr, 29.97 tbn, 29.97 tbc