Recherche avancée

Médias (91)

Autres articles (72)

  • Contribute to a better visual interface

    13 avril 2011

    MediaSPIP is based on a system of themes and templates. Templates define the placement of information on the page, and can be adapted to a wide range of uses. Themes define the overall graphic appearance of the site.
    Anyone can submit a new graphic theme or template and make it available to the MediaSPIP community.

  • Récupération d’informations sur le site maître à l’installation d’une instance

    26 novembre 2010, par

    Utilité
    Sur le site principal, une instance de mutualisation est définie par plusieurs choses : Les données dans la table spip_mutus ; Son logo ; Son auteur principal (id_admin dans la table spip_mutus correspondant à un id_auteur de la table spip_auteurs)qui sera le seul à pouvoir créer définitivement l’instance de mutualisation ;
    Il peut donc être tout à fait judicieux de vouloir récupérer certaines de ces informations afin de compléter l’installation d’une instance pour, par exemple : récupérer le (...)

  • Les tâches Cron régulières de la ferme

    1er décembre 2010, par

    La gestion de la ferme passe par l’exécution à intervalle régulier de plusieurs tâches répétitives dites Cron.
    Le super Cron (gestion_mutu_super_cron)
    Cette tâche, planifiée chaque minute, a pour simple effet d’appeler le Cron de l’ensemble des instances de la mutualisation régulièrement. Couplée avec un Cron système sur le site central de la mutualisation, cela permet de simplement générer des visites régulières sur les différents sites et éviter que les tâches des sites peu visités soient trop (...)

Sur d’autres sites (8375)

  • FFMPEG and STB_Image Create awful Picture

    9 février 2023, par murage kibicho

    I was learning how to use the FFMPEG C api and I was trying to encode a jpeg into a MPEG file. I load the JPEG into (unsigned char *) using the stb-image library. Then I create a (uint8_t *) and copy my rgb values. Finally, I convert RGB to YUV420 using sws_scale. However, a portion of my image blurs out when I perform the encoding.Bad Image
/
This is the original imageoriginal image

    
Perhaps I allocate my frame buffer incorrectly ?

    


    ret = av_frame_get_buffer(frame, 0);


    


    
This is my entire program

    


    #define STB_IMAGE_IMPLEMENTATION&#xA;#include "stb_image.h"&#xA;#define STB_IMAGE_WRITE_IMPLEMENTATION&#xA;#include "stb_image_write.h"&#xA;#define STB_IMAGE_RESIZE_IMPLEMENTATION&#xA;#include "stb_image_resize.h"&#xA;#include &#xA;&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;//gcc stack.c -lm -o stack.o `pkg-config --cflags --libs libavformat libavcodec libswresample libswscale libavutil` &amp;&amp; ./stack.o&#xA;&#xA;/*&#xA;int i : pts of current frame&#xA;*/&#xA;void PictureToFrame(int i, AVFrame *frame, int height, int width)&#xA;{&#xA;    //Use stb image to get rgb values&#xA;        char *fileName = "profil.jpeg";&#xA;        int imageHeight = 0;&#xA;        int imageWidth = 0;&#xA;        int colorChannels = 0;&#xA;        int arrayLength = 0;&#xA;    unsigned char *image = stbi_load(fileName,&amp;imageWidth,&amp;imageHeight,&amp;colorChannels,0);&#xA;    &#xA;    printf("(height: %d, width: %d)\n",imageHeight, imageWidth);&#xA;    assert(colorChannels == 3 &amp;&amp; imageHeight == height &amp;&amp; imageWidth == width);&#xA;    &#xA;    //Convert unsigned char * to uint8_t *&#xA;    arrayLength = imageHeight * imageWidth * colorChannels;&#xA;    uint8_t *rgb = calloc(arrayLength, sizeof(uint8_t));&#xA;    int j = arrayLength-1;&#xA;    for(int i = 0; i &lt; arrayLength; i&#x2B;&#x2B;)&#xA;    {&#xA;        rgb[i] = (uint8_t) image[i];&#xA;        }&#xA;        &#xA;        //Use SwsContext to scale RGB to YUV420P and write to frame&#xA;        const int in_linesize[1] = { 3* imageWidth};&#xA;        struct SwsContext *sws_context = NULL;&#xA;        sws_context = sws_getCachedContext(sws_context,&#xA;            imageWidth, imageHeight, AV_PIX_FMT_RGB24,&#xA;            imageWidth, imageHeight, AV_PIX_FMT_YUV420P,&#xA;            0, 0, 0, 0);&#xA;        sws_scale(sws_context, (const uint8_t * const *)&amp;rgb, in_linesize, 0,&#xA;            imageHeight, frame->data, frame->linesize);&#xA;        //Save frame pts&#xA;        frame->pts = i;&#xA;        &#xA;        //Free alloc&#x27;d data&#xA;        stbi_image_free(image);&#xA;        sws_freeContext(sws_context);&#xA;        free(rgb);&#xA;}&#xA;static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt, FILE *outfile)&#xA;{&#xA;    int returnValue;&#xA;    /* send the frame to the encoder */&#xA;    if(frame)&#xA;    {&#xA;        printf("Send frame %3"PRId64"\n", frame->pts);&#xA;    }&#xA;        returnValue = avcodec_send_frame(enc_ctx, frame);&#xA;    if(returnValue &lt; 0)&#xA;    {&#xA;        printf("Error sending a frame for encoding\n");&#xA;        return;&#xA;    }&#xA;    while(returnValue >= 0)&#xA;    {&#xA;        returnValue = avcodec_receive_packet(enc_ctx, pkt);&#xA;        if(returnValue == AVERROR(EAGAIN) || returnValue == AVERROR_EOF)&#xA;        {&#xA;            return;&#xA;        }&#xA;        else if(returnValue &lt; 0)&#xA;        {&#xA;            printf("Error during encoding\n");&#xA;            return;&#xA;        }&#xA;&#xA;        printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);&#xA;        fwrite(pkt->data, 1, pkt->size, outfile);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;}&#xA;&#xA;&#xA;int main(int argc, char **argv)&#xA;{&#xA;    const char *filename, *codec_name;&#xA;    const AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    int i, ret, x, y;&#xA;    FILE *f;&#xA;    AVFrame *frame;&#xA;    AVPacket *pkt;&#xA;    uint8_t endcode[] = { 0, 0, 1, 0xb7 };&#xA;&#xA;    filename = "outo.mp4";&#xA;    codec_name = "mpeg1video";//"mpeg1video";//"libx264";&#xA;&#xA;&#xA;    /* find the mpeg1video encoder */&#xA;    codec = avcodec_find_encoder_by_name(codec_name);&#xA;    if(!codec)&#xA;    {&#xA;        printf("Error finding codec\n");&#xA;    return 0;&#xA;    }&#xA;&#xA;    c = avcodec_alloc_context3(codec);&#xA;    if(!c)&#xA;    {&#xA;        printf("Error allocating c\n");&#xA;    return 0;&#xA;    }&#xA;&#xA;    pkt = av_packet_alloc();&#xA;    if(!pkt)&#xA;    {&#xA;        printf("Error allocating pkt\n");&#xA;    return 0;&#xA;    }&#xA;&#xA;    /* put sample parameters */&#xA;    c->bit_rate = 400000;&#xA;    /* resolution must be a multiple of two */&#xA;    c->width = 800;&#xA;    c->height = 800;&#xA;    /* frames per second */&#xA;    c->time_base = (AVRational){1, 25};&#xA;    c->framerate = (AVRational){25, 1};&#xA;    c->gop_size = 10;&#xA;    c->max_b_frames = 1;&#xA;    c->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;&#xA;    if(codec->id == AV_CODEC_ID_H264)&#xA;    {&#xA;        av_opt_set(c->priv_data, "preset", "slow", 0);&#xA;    }&#xA;        &#xA;&#xA;    /* open it */&#xA;    ret = avcodec_open2(c, codec, NULL);&#xA;    if(ret &lt; 0) &#xA;    {&#xA;        printf("Error opening codec\n");&#xA;    return 0;&#xA;    }&#xA;&#xA;    f = fopen(filename, "wb");&#xA;    if(!f)&#xA;    {&#xA;         printf("Error opening file\n");&#xA;     return 0;&#xA;    }&#xA;&#xA;    frame = av_frame_alloc();&#xA;    if(!frame)&#xA;    {&#xA;        printf("Error allocating frame\n");&#xA;    return 0;&#xA;    }&#xA;    frame->format = c->pix_fmt;&#xA;    frame->width  = c->width;&#xA;    frame->height = c->height;&#xA;&#xA;    //I suspect this is the problem&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if(ret &lt; 0)&#xA;    {&#xA;        fprintf(stderr, "Could not allocate the video frame data\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* encode 25 frames*/&#xA;    for(i = 0; i &lt; 25; i&#x2B;&#x2B;) &#xA;    {&#xA;&#xA;        /* make sure the frame data is writable */&#xA;        ret = av_frame_make_writable(frame);&#xA;        if(ret &lt; 0)&#xA;        {&#xA;            return 0;&#xA;        }&#xA;        //FIll Frame with picture data&#xA;        PictureToFrame(i, frame, c->height, c->width);&#xA;&#xA;        /* encode the image */&#xA;        encode(c, frame, pkt, f);&#xA;    }&#xA;&#xA;    /* flush the encoder */&#xA;    encode(c, NULL, pkt, f);&#xA;&#xA;    /* add sequence end code to have a real MPEG file */&#xA;    if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO)&#xA;        fwrite(endcode, 1, sizeof(endcode), f);&#xA;    fclose(f);&#xA;&#xA;    avcodec_free_context(&amp;c);&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;

    &#xA;

  • How AVCodecContext bitrate, framerate and timebase is used when encoding single frame

    28 mars 2023, par Cyrus

    I am trying to learn FFmpeg from examples as there is a tight schedule. The task is to encode a raw YUV image into JPEG format of the given width and height. I have found examples from ffmpeg official website, which turns out to be quite straight-forward. However there are some fields in AVCodecContext that I thought only makes sense when encoding videos(e.g. bitrate, framerate, timebase, gopsize, max_b_frames etc).

    &#xA;

    I understand on a high level what those values are when it comes to videos, but do I need to care about those when I just want a single image ? Currently for testing, I am just setting them as dummy values and it seems to work. But I want to make sure that I am not making terrible assumptions that will break in the long run.

    &#xA;

    EDIT :

    &#xA;

    Here is the code I got. Most of them are copy and paste from examples, with some changes to replace old APIs with newer ones.

    &#xA;

    #include "thumbnail.h"&#xA;#include "libavcodec/avcodec.h"&#xA;#include "libavutil/imgutils.h"&#xA;#include &#xA;#include &#xA;#include &#xA;&#xA;void print_averror(int error_code) {&#xA;    char err_msg[100] = {0};&#xA;    av_strerror(error_code, err_msg, 100);&#xA;    printf("Reason: %s\n", err_msg);&#xA;}&#xA;&#xA;ffmpeg_status_t save_yuv_as_jpeg(uint8_t* source_buffer, char* output_thumbnail_filename, int thumbnail_width, int thumbnail_height) {&#xA;    const AVCodec* mjpeg_codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);&#xA;    if (!mjpeg_codec) {&#xA;        printf("Codec for mjpeg cannot be found.\n");&#xA;        return FFMPEG_THUMBNAIL_CODEC_NOT_FOUND;&#xA;    }&#xA;&#xA;    AVCodecContext* codec_ctx = avcodec_alloc_context3(mjpeg_codec);&#xA;    if (!codec_ctx) {&#xA;        printf("Codec context cannot be allocated for the given mjpeg codec.\n");&#xA;        return FFMPEG_THUMBNAIL_ALLOC_CONTEXT_FAILED;&#xA;    }&#xA;&#xA;    AVPacket* pkt = av_packet_alloc();&#xA;    if (!pkt) {&#xA;        printf("Thumbnail packet cannot be allocated.\n");&#xA;        return FFMPEG_THUMBNAIL_ALLOC_PACKET_FAILED;&#xA;    }&#xA;&#xA;    AVFrame* frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        printf("Thumbnail frame cannot be allocated.\n");&#xA;        return FFMPEG_THUMBNAIL_ALLOC_FRAME_FAILED;&#xA;    }&#xA;&#xA;    // The part that I don&#x27;t understand&#xA;    codec_ctx->bit_rate = 400000;&#xA;    codec_ctx->width = thumbnail_width;&#xA;    codec_ctx->height = thumbnail_height;&#xA;    codec_ctx->time_base = (AVRational){1, 25};&#xA;    codec_ctx->framerate = (AVRational){1, 25};&#xA;&#xA;    codec_ctx->gop_size = 10;&#xA;    codec_ctx->max_b_frames = 1;&#xA;    codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;    int ret = av_image_fill_arrays(frame->data, frame->linesize, source_buffer, AV_PIX_FMT_YUV420P, thumbnail_width, thumbnail_height, 32);&#xA;    if (ret &lt; 0) {&#xA;        print_averror(ret);&#xA;        printf("Pixel format: yuv420p, width: %d, height: %d\n", thumbnail_width, thumbnail_height);&#xA;        return FFMPEG_THUMBNAIL_FILL_FRAME_DATA_FAILED;&#xA;    }&#xA;&#xA;    ret = avcodec_send_frame(codec_ctx, frame);&#xA;    if (ret &lt; 0) {&#xA;        print_averror(ret);&#xA;        printf("Failed to send frame to encoder.\n");&#xA;        return FFMPEG_THUMBNAIL_FILL_SEND_FRAME_FAILED;&#xA;    }&#xA;&#xA;    ret = avcodec_receive_packet(codec_ctx, pkt);&#xA;    if (ret &lt; 0) {&#xA;        print_averror(ret);&#xA;        printf("Failed to receive packet from encoder.\n");&#xA;        return FFMPEG_THUMBNAIL_FILL_SEND_FRAME_FAILED;&#xA;    }&#xA;&#xA;    // store the thumbnail in output&#xA;    int fd = open(output_thumbnail_filename, O_CREAT | O_RDWR);&#xA;    write(fd, pkt->data, pkt->size);&#xA;    close(fd);&#xA;&#xA;    // freeing allocated structs&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;    return FFMPEG_SUCCESS;&#xA;}&#xA;

    &#xA;

  • How to publish selfmade stream with ffmpeg and c++ to rtmp server ?

    10 janvier 2024, par rLino

    Have a nice day to you, people !

    &#xA;&#xA;

    I am writing an application for Windows that will capture the screen and send the stream to Wowza server by rtmp (for broadcasting). My application use ffmpeg and Qt.&#xA;I capture the screen with WinApi, convert a buffer to YUV444(because it's simplest) and encode frame as described at the file decoding_encoding.c (from FFmpeg examples) :

    &#xA;&#xA;

    ///////////////////////////&#xA;//Encoder initialization&#xA;///////////////////////////&#xA;avcodec_register_all();&#xA;codec=avcodec_find_encoder(AV_CODEC_ID_H264);&#xA;c = avcodec_alloc_context3(codec);&#xA;c->width=scr_width;&#xA;c->height=scr_height;&#xA;c->bit_rate = 400000;&#xA;int base_num=1;&#xA;int base_den=1;//for one frame per second&#xA;c->time_base= (AVRational){base_num,base_den};&#xA;c->gop_size = 10;&#xA;c->max_b_frames=1;&#xA;c->pix_fmt = AV_PIX_FMT_YUV444P;&#xA;av_opt_set(c->priv_data, "preset", "slow", 0);&#xA;&#xA;frame = avcodec_alloc_frame();&#xA;frame->format = c->pix_fmt;&#xA;frame->width  = c->width;&#xA;frame->height = c->height;&#xA;&#xA;for(int counter=0;counter&lt;10;counter&#x2B;&#x2B;)&#xA;{&#xA;///////////////////////////&#xA;//Capturing Screen&#xA;///////////////////////////&#xA;    GetCapScr(shotbuf,scr_width,scr_height);//result: shotbuf is filled by screendata from HBITMAP&#xA;///////////////////////////&#xA;//Convert buffer to YUV444 (standard formula)&#xA;//It&#x27;s handmade function because of problems with prepare buffer to swscale from HBITMAP&#xA;///////////////////////////&#xA;    RGBtoYUV(shotbuf,frame->linesize,frame->data,scr_width,scr_height);//result in frame->data&#xA;///////////////////////////&#xA;//Encode Screenshot&#xA;///////////////////////////&#xA;    av_init_packet(&amp;pkt);&#xA;    pkt.data = NULL;    // packet data will be allocated by the encoder&#xA;    pkt.size = 0;&#xA;    frame->pts = counter;&#xA;    avcodec_encode_video2(c, &amp;pkt, frame, &amp;got_output);&#xA;    if (got_output) &#xA;    {&#xA;        //I think that  sending packet by rtmp  must be here!&#xA;        av_free_packet(&amp;pkt);             &#xA;&#xA;    }&#xA;&#xA;}&#xA;// Get the delayed frames&#xA;for (int got_output = 1,i=0; got_output; i&#x2B;&#x2B;)&#xA;{&#xA;    ret = avcodec_encode_video2(c, &amp;pkt, NULL, &amp;got_output);&#xA;    if (ret &lt; 0)&#xA;        {&#xA;            fprintf(stderr, "Error encoding frame\n");&#xA;            exit(1);&#xA;        }&#xA;        if (got_output)&#xA;        {&#xA;        //I think that  sending packet by rtmp  must be here!&#xA;        av_free_packet(&amp;pkt);      &#xA;        }&#xA;}&#xA;&#xA;///////////////////////////&#xA;//Deinitialize encoder&#xA;///////////////////////////&#xA;avcodec_close(c);&#xA;av_free(c);&#xA;av_freep(&amp;frame->data[0]);&#xA;avcodec_free_frame(&amp;frame);&#xA;

    &#xA;&#xA;

    I need to send video stream generated by this code to RTMP server.&#xA;In other words, I need c++/c analog for this command :

    &#xA;&#xA;

    ffmpeg -re -i "sample.h264" -f flv rtmp://sample.url.com/screen/test_stream&#xA;

    &#xA;&#xA;

    It's useful, but I don't want to save stream to file, I want to use ffmpeg libraries for realtime encoding screen capture and sending encoded frames to RTMP server inside my own application.&#xA;Please give me a little example how to initialize AVFormatContext properly and to send my encoded video AVPackets to server.

    &#xA;&#xA;

    Thanks.

    &#xA;