Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>this code works with YUV raw images. I got segfault trying to encode an RGB raw image. but you can use <code>swscale()</code> to convert RGB to YUV</p> <pre><code>int main() { int ret; AVFrame *frame; AVPacket pkt; av_register_all(); AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG); if (!codec) { printf("Codec not found\n"); exit(1); } AVCodecContext* c = avcodec_alloc_context3(codec); if (!c) { printf("Could not allocate video codec context\n"); exit(1); } c-&gt;bit_rate = 400000; c-&gt;width = 320; c-&gt;height = 200; c-&gt;time_base= (AVRational){1,25}; c-&gt;pix_fmt = AV_PIX_FMT_YUVJ420P; if (avcodec_open2(c, codec, NULL) &lt; 0) { printf("Could not open codec\n"); exit(1); } frame = avcodec_alloc_frame(); if (!frame) { printf("Could not allocate video frame\n"); exit(1); } frame-&gt;format = c-&gt;pix_fmt; frame-&gt;width = c-&gt;width; frame-&gt;height = c-&gt;height; ret = av_image_alloc(frame-&gt;data, frame-&gt;linesize, c-&gt;width, c-&gt;height, c-&gt;pix_fmt, 32); if (ret &lt; 0) { printf("Could not allocate raw picture buffer\n"); exit(1); } av_init_packet(&amp;pkt); pkt.data = NULL; pkt.size = 0; /* prepare a dummy image */ /* Y */ for(int y=0;y&lt;c-&gt;height;y++) { for(int x=0;x&lt;c-&gt;width;x++) { frame-&gt;data[0][y * frame-&gt;linesize[0] + x] = x + y; } } /* Cb and Cr */ for(int y=0;y&lt;c-&gt;height/2;y++) { for(int x=0;x&lt;c-&gt;width/2;x++) { frame-&gt;data[1][y * frame-&gt;linesize[1] + x] = 128 + y; frame-&gt;data[2][y * frame-&gt;linesize[2] + x] = 64 + x; } } frame-&gt;pts = 1; int got_output = 0; ret = avcodec_encode_video2(c, &amp;pkt, frame, &amp;got_output); if (ret &lt; 0) { printf("Error encoding frame\n"); exit(1); } if (got_output) { printf("got frame\n"); FILE* f = fopen("x.jpg", "wb"); fwrite(pkt.data, 1, pkt.size, f); av_free_packet(&amp;pkt); } avcodec_close(c); av_free(c); av_freep(&amp;frame-&gt;data[0]); avcodec_free_frame(&amp;frame); printf("\n"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload