History

api-example: use new video encoding API.

Anton Khirnov committed 14 years ago

Commit 661454a1 parent 29cfdd31 file · +44 −28Browse files

Showing the changes to libavcodec/api-example.c (1 of 1 file). Show all

Unified · Split

libavcodec/api-example.c+44−28
@@ -38,6 +38,7 @@ · 37 unmodified lines
3838
3939 #include "libavcodec/avcodec.h"
4040 #include "libavutil/audioconvert.h"
41+#include "libavutil/imgutils.h"
4142 #include "libavutil/mathematics.h"
4243 #include "libavutil/samplefmt.h"
4344
@@ -316,10 +317,11 @@ static void video_encode_example(const char *filename) · 272 unmodified lines
316317 {
317318 AVCodec *codec;
318319 AVCodecContext *c= NULL;
319− int i, out_size, size, x, y, outbuf_size;
320+ int i, ret, x, y, got_output;
320321 FILE *f;
321322 AVFrame *picture;
322− uint8_t *outbuf, *picture_buf;
323+ AVPacket pkt;
324+ uint8_t endcode[] = { 0, 0, 1, 0xb7 };
323325
324326 printf("Video encoding\n");
325327
@@ -356,21 +358,22 @@ static void video_encode_example(const char *filename) · 30 unmodified lines
356358 exit(1);
357359 }
358360
359− /* alloc image and output buffer */
360− outbuf_size = 100000;
361− outbuf = malloc(outbuf_size);
362− size = c->width * c->height;
363− picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
364−
365− picture->data[0] = picture_buf;
366− picture->data[1] = picture->data[0] + size;
367− picture->data[2] = picture->data[1] + size / 4;
368− picture->linesize[0] = c->width;
369− picture->linesize[1] = c->width / 2;
370− picture->linesize[2] = c->width / 2;
361+ ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height,
362+ c->pix_fmt, 32);
363+ if (ret < 0) {
364+ fprintf(stderr, "could not alloc raw picture buffer\n");
365+ exit(1);
366+ }
367+ picture->format = c->pix_fmt;
368+ picture->width = c->width;
369+ picture->height = c->height;
371370
372371 /* encode 1 second of video */
373372 for(i=0;i<25;i++) {
373+ av_init_packet(&pkt);
374+ pkt.data = NULL; // packet data will be allocated by the encoder
375+ pkt.size = 0;
376+
374377 fflush(stdout);
375378 /* prepare a dummy image */
376379 /* Y */
@@ -388,33 +391,46 @@ static void video_encode_example(const char *filename) · 11 unmodified lines
388391 }
389392 }
390393
394+ picture->pts = i;
395+
391396 /* encode the image */
392− out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
393− printf("encoding frame %3d (size=%5d)\n", i, out_size);
394− fwrite(outbuf, 1, out_size, f);
397+ ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
398+ if (ret < 0) {
399+ fprintf(stderr, "error encoding frame\n");
400+ exit(1);
401+ }
402+
403+ if (got_output) {
404+ printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
405+ fwrite(pkt.data, 1, pkt.size, f);
406+ av_free_packet(&pkt);
407+ }
395408 }
396409
397410 /* get the delayed frames */
398− for(; out_size; i++) {
411+ for (got_output = 1; got_output; i++) {
399412 fflush(stdout);
400413
401− out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
402− printf("write frame %3d (size=%5d)\n", i, out_size);
403− fwrite(outbuf, 1, out_size, f);
414+ ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
415+ if (ret < 0) {
416+ fprintf(stderr, "error encoding frame\n");
417+ exit(1);
418+ }
419+
420+ if (got_output) {
421+ printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
422+ fwrite(pkt.data, 1, pkt.size, f);
423+ av_free_packet(&pkt);
424+ }
404425 }
405426
406427 /* add sequence end code to have a real mpeg file */
407− outbuf[0] = 0x00;
408− outbuf[1] = 0x00;
409− outbuf[2] = 0x01;
410− outbuf[3] = 0xb7;
411− fwrite(outbuf, 1, 4, f);
428+ fwrite(endcode, 1, sizeof(endcode), f);
412429 fclose(f);
413− free(picture_buf);
414− free(outbuf);
415430
416431 avcodec_close(c);
417432 av_free(c);
433+ av_freep(&picture->data[0]);
418434 av_free(picture);
419435 printf("\n");
420436 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t