Recherche avancée

Médias (17)

Mot : - Tags -/wired

Autres articles (92)

  • Qualité du média après traitement

    21 juin 2013, par

    Le bon réglage du logiciel qui traite les média est important pour un équilibre entre les partis ( bande passante de l’hébergeur, qualité du média pour le rédacteur et le visiteur, accessibilité pour le visiteur ). Comment régler la qualité de son média ?
    Plus la qualité du média est importante, plus la bande passante sera utilisée. Le visiteur avec une connexion internet à petit débit devra attendre plus longtemps. Inversement plus, la qualité du média est pauvre et donc le média devient dégradé voire (...)

  • Les images

    15 mai 2013
  • Emballe médias : à quoi cela sert ?

    4 février 2011, par

    Ce plugin vise à gérer des sites de mise en ligne de documents de tous types.
    Il crée des "médias", à savoir : un "média" est un article au sens SPIP créé automatiquement lors du téléversement d’un document qu’il soit audio, vidéo, image ou textuel ; un seul document ne peut être lié à un article dit "média" ;

Sur d’autres sites (9019)

  • ffmpeg / libav encoding 264 from YUV420P [on hold]

    4 janvier 2017, par MrSmith

    I am having issues getting this thing to encode correctly. Either I get the container wrong or the content it seems.
    This code encodes a file just fine, but viewing it with VLC or Totem will just yield the first picture, not the remaining 100 frames.
    This is a step up from before as I dont get warnings that my container (mp4) is borked, however now the video wont play at all.. at least it played before :).

    If someone could put a finger somewhere on this code and call me a dumbass, that would be nice :)

    void encode_video(char *carrarr[]){
    av_log_set_level(AV_LOG_DEBUG);
    av_register_all();
    avcodec_register_all();
    AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);
    AVOutputFormat * outputFormat = av_guess_format("mp4", NULL, NULL);

    int test = avformat_alloc_output_context2(&outFmtCtx, outputFormat, NULL, NULL);
    if(test<0) exit(-1);

    AVStream * outStrm = avformat_new_stream(outFmtCtx, codec);
    avcodec_get_context_defaults3(outStrm->codec, codec);

    outStrm->codec->codec_id = AV_CODEC_ID_H264;
    outStrm->codec->coder_type = AVMEDIA_TYPE_VIDEO;

    outStrm->codec->bit_rate = 400000;
    outStrm->codec->width = 320;
    outStrm->codec->height = 240;
    outStrm->codec->time_base= (AVRational){1,25};
    outStrm->time_base=(AVRational){1,25};
    outStrm->codec->gop_size = 10; // emit one intra frame every ten frames
    outStrm->codec->max_b_frames=1;
    outStrm->codec->pix_fmt = AV_PIX_FMT_YUV420P;
    outStrm->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    // FOR H264 ONLY
    test = av_opt_set(outStrm->codec->priv_data, "preset", "slow", 0);
    if(test<0) exit(-2);
    test = avcodec_open2(outStrm->codec, codec, NULL);
    if(test<0) exit(-3);
    test = avio_open2(&outFmtCtx->pb, "myoutputfile.avi", AVIO_FLAG_WRITE, NULL, NULL);
    if(test<0) exit(-4);

    test = avformat_write_header(outFmtCtx, NULL);
    if(test<0) exit(-5);
    AVFrame * frame = avcodec_alloc_frame();
    if (!frame) exit(-6);

    frame->format = outStrm->codec->pix_fmt;
    frame->width  = outStrm->codec->width;
    frame->height = outStrm->codec->height;

    ret = av_image_alloc(frame->data, frame->linesize, frame->width, frame->height, outStrm->codec->pix_fmt, 32);
    if (ret < 0) exit(-7);

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

    int yrange = 240*320;
    int vrange = 240*320*0.25;

    for(int i=0;i<100;i++){
       char *carr = carrarr[i];

       char* Y = carr;
       char* U = carr+yrange;
       char* V = carr+yrange+vrange;
       frame->data[0] = (uint8_t*)Y;
       frame->data[1] = (uint8_t*)U;
       frame->data[2] = (uint8_t*)V;
       frame->pts = i;

       ret = avcodec_encode_video2(outStrm->codec, &pkt, frame, &got_output);
       if (ret < 0) {
           fprintf(stderr, "Error encoding frame\n");
           exit (EXIT_FAILURE);
       }
       if (got_output) {
           printf("Write frame %3d (size=%5d)\n", i, pkt.size);
           //fwrite(pkt.data, 1, pkt.size, file_encoded_video);
           av_interleaved_write_frame(outFmtCtx, &pkt);
           av_free_packet(&pkt);
       }
    }
    av_write_trailer(outFmtCtx);
    avio_close(outFmtCtx->pb);
    }

    carrarr is holding 100 frames of nice valid YUV420P data, I know this cause I’ve encoded it before AND I can output it to screen with SDL.

    Any ideas welcome. Thanks !

  • Error in video streaming using libavformat : VBV buffer size not set, muxing may fail

    18 octobre 2017, par Blue Sky

    I stream a video using libavformat as follows :

    static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
                           enum AVCodecID codec_id)
    {
    AVCodecContext *c;
    AVStream *st;
    /* find the encoder */
    *codec = avcodec_find_encoder(codec_id);
    if (!(*codec)) {
       fprintf(stderr, "Could not find encoder for '%s'\n",
               avcodec_get_name(codec_id));
       exit(1);
    }
    st = avformat_new_stream(oc, *codec);
    if (!st) {
       fprintf(stderr, "Could not allocate stream\n");
       exit(1);
    }
    st->id = oc->nb_streams-1;
    c = st->codec;
    switch ((*codec)->type) {
    case AVMEDIA_TYPE_AUDIO:
       c->sample_fmt  = (*codec)->sample_fmts ?
           (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
       c->bit_rate    = 64000;
       c->sample_rate = 44100;
       c->channels    = 2;
       break;
    case AVMEDIA_TYPE_VIDEO:
       c->codec_id = codec_id;
       c->bit_rate = 400000;
       /* Resolution must be a multiple of two. */
       c->width    = outframe_width;
       c->height   = outframe_height;
       /* timebase: This is the fundamental unit of time (in seconds) in terms
        * of which frame timestamps are represented. For fixed-fps content,
        * timebase should be 1/framerate and timestamp increments should be
        * identical to 1. */
       c->time_base.den = STREAM_FRAME_RATE;
       c->time_base.num = 1;
       c->gop_size      = 12; /* emit one intra frame every twelve frames at most */
       c->pix_fmt       = STREAM_PIX_FMT;
       if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
           /* just for testing, we also add B frames */
           c->max_b_frames = 2;
       }
       if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
           /* Needed to avoid using macroblocks in which some coeffs overflow.
            * This does not happen with normal video, it just happens here as
            * the motion of the chroma plane does not match the luma plane. */
           c->mb_decision = 2;
       }
    break;
    default:
       break;
    }
    /* Some formats want stream headers to be separate. */
    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
       c->flags |= CODEC_FLAG_GLOBAL_HEADER;
    return st;
    }

    But when I run this code, I get the following error/warning :

    [mpeg @ 01f3f040] VBV buffer size not set, muxing may fail

    Do you know how I can set the VBV buffer size in the code ? In fact, when I use ffplay to display the streamed video, ffplay doesn’t show anything for short videos but for long videos, it start displaying the video immediately. So, it looks like ffplay needs a buffer to be filled up by some amount so that it can start displaying the stream. Am I right ?

  • ffmpeg API muxing h264 endoced frames to mkv

    24 mars 2017, par Pawel K

    Hi I’m having some problems with muxing h264 encoded frames into mkv container using code of ffmpeg-3.2.4.
    I have ended up with the following code that is a mashup of code found on SO and muxing.c example of ffmpeg :
    (and yes I am aware that it is ugly, no errors checked etc. it is meant to be like that for clarity :) )

    char *filename = "out.mkv";
    const uint8_t SPS[] = { 0x67, 0x42, 0x40, 0x1F, 0x96, 0x54, 0x02, 0x80, 0x2D, 0xD0, 0x0F, 0x39, 0xEA };
    const uint8_t PPS[] = { 0x68, 0xCE, 0x38, 0x80 };
    int fps = 5;

    typedef struct OutputStream
    {
      AVStream *st;
      AVCodecContext *enc;

      /* pts of the next frame that will be generated */
      int64_t next_pts;
      int samples_count;
      AVFrame *frame;
      AVFrame *tmp_frame;
      float t, tincr, tincr2;
      struct SwsContext *sws_ctx;
      struct SwrContext *swr_ctx;
    } OutputStream;

    static void avlog_cb(void *s, int level, const char *szFmt, va_list varg)
    {
      vprintf(szFmt, varg);
    }

    void main()
    {
      AVOutputFormat *fmt;
      AVFormatContext *formatCtx;
      AVCodec *audio_codec;
      AVCodec *video_codec;
      OutputStream video_st = { 0 };
      OutputStream audio_st = { 0 };
      av_register_all();

      av_log_set_level(AV_LOG_TRACE);
      //av_log_set_callback(avlog_cb);

      //allocate output and format ctxs
      avformat_alloc_output_context2(&formatCtx, NULL, NULL, filename);
      fmt = formatCtx->oformat;

      //allocate streams
      video_codec = avcodec_find_encoder(fmt->video_codec);
      video_st.st = avformat_new_stream(formatCtx, NULL);
      video_st.st->id = 0;

      AVCodecContext *codecCtx =  avcodec_alloc_context3(video_codec);
      fmt->video_codec = AV_CODEC_ID_H264;
      video_st.enc = codecCtx;

      codecCtx->codec_id = fmt->video_codec;
      codecCtx->bit_rate = 400000;
      codecCtx->width  = 1080;
      codecCtx->height = 720;
      codecCtx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
      codecCtx->level = 31;

      video_st.st->time_base = (AVRational){ 1, fps };
      codecCtx->time_base = video_st.st->time_base;
      codecCtx->gop_size = 4;
      codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;

      //open video codec
      codecCtx->extradata_size = 24;
      codecCtx->extradata = (uint8_t *)av_malloc(codecCtx->extradata_size);
      uint8_t extra_data_array[] = { 0x01, SPS[1], SPS[2], SPS[3], 0xFF, 0xE1, 0xc0, 0, 0x42, 0x40, 0x1F, 0x96, 0x54, 0x02, 0x80, 0x2D, 0xD0, 0x0F, 0x39, 0xEA, 0x03, 0xCE, 0x38, 0x80 };
      memcpy(codecCtx->extradata, extra_data_array, codecCtx->extradata_size);

      AVCodecContext *c = video_st.enc;
      AVDictionary *opt = NULL;
      avcodec_open2(c, video_codec, &opt);
      avcodec_parameters_from_context(video_st.st->codecpar, c);

      //open output file
      avio_open(&formatCtx->pb, filename, AVIO_FLAG_WRITE);

      //write header
      int res = avformat_write_header(formatCtx, NULL);

      //write frames

      // get the frames from file
      uint32_t u32frameCnt = 0;

      do
      {
         int8_t i8frame_name[64] = "";

         uint8_t  *pu8framePtr = NULL;
         AVPacket pkt = { 0 };

         av_init_packet(&pkt);
         sprintf(i8frame_name, "frames/frame%d.bin", u32frameCnt++);
         //reading frames from files
         FILE *ph264Frame = fopen(i8frame_name, "r");
         if(NULL == ph264Frame)
         {
            goto leave;
         }

         //get file size
         fseek(ph264Frame, 0L, SEEK_END);
         uint32_t u32file_size = 0;
         u32file_size = ftell(ph264Frame);
         fseek(ph264Frame, 0L, SEEK_SET);

         pu8framePtr = malloc(u32file_size);
         uint32_t u32readout = fread(pu8framePtr, 1, u32file_size, ph264Frame);

         //if the read frame is a key frame i.e. nalu hdr type = 5 set it as a key frame
         if(0x65 == pu8framePtr[4])
         {
            pkt.flags = AV_PKT_FLAG_KEY;
         }
         pkt.data = (uint8_t *)pu8framePtr;
         pkt.size = u32readout;
         pkt.pts  = u32frameCnt;
         pkt.dts  = pkt.pts;

         av_packet_rescale_ts(&pkt, c->time_base, video_st.st->time_base);
         pkt.stream_index = video_st.st->index;
         av_interleaved_write_frame(formatCtx, &pkt);
         free(pu8framePtr);
         fclose(ph264Frame);
      }
      while(1);
    leave:

      av_write_trailer(formatCtx);
      av_dump_format(formatCtx, 0, filename, 1);
      avcodec_free_context(&video_st.enc);
      avio_closep(&formatCtx->pb);
      avformat_free_context(formatCtx);
    }

    It can be compiled with the following command line (after adding headers) :

    gcc file.c -o test_app -I/usr/local/include -L/usr/local/lib -lxcb-shm -lxcb -lX11 -lx264 -lm -lz -pthread -lswresample -lswscale -lavcodec -lavformat -lavdevice -lavutil

    The files that are read are valid annexB stream (valid as in it’s playable in vlc after concatenating into file) it is a Constrained Baseline 3.1 profile H264 and it comes from an IPcam’s interleaved RTCP/RTP stream (demuxed)

    The result is ... well I don’t see the picture. I get only black screen with the progress bar and timer running. I don’t know if I do something wrong with setting up the codecs and streams, or it’s just wrong timestamps.
    I know I got them wrong in some manner but I don’t understand that fully yet (how to calculate the correct presentation times), i.e. the stream and the codec both contain time_base field, and then I know that the sample rate of the video is 90kHz and the frame rate is 5 fps

    On top of it all the examples I’ve found have to some extend deprecated parts that change the flow/meaning of the application and that doesn’t help at all so thus If anyone could help I would appreciate it (I think not only me I would guess)

    Regards, Pawel