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
38 38
39#include "libavcodec/avcodec.h"39#include "libavcodec/avcodec.h"
40#include "libavutil/audioconvert.h"40#include "libavutil/audioconvert.h"
41#include "libavutil/imgutils.h"
41#include "libavutil/mathematics.h"42#include "libavutil/mathematics.h"
42#include "libavutil/samplefmt.h"43#include "libavutil/samplefmt.h"
43 44
@@ -316,10 +317,11 @@ static void video_encode_example(const char *filename) · 272 unmodified lines
316{317{
317 AVCodec *codec;318 AVCodec *codec;
318 AVCodecContext *c= NULL;319 AVCodecContext *c= NULL;
319 int i, out_size, size, x, y, outbuf_size;320 int i, ret, x, y, got_output;
320 FILE *f;321 FILE *f;
321 AVFrame *picture;322 AVFrame *picture;
322 uint8_t *outbuf, *picture_buf;323 AVPacket pkt;
324 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
323 325
324 printf("Video encoding\n");326 printf("Video encoding\n");
325 327
@@ -356,21 +358,22 @@ static void video_encode_example(const char *filename) · 30 unmodified lines
356 exit(1);358 exit(1);
357 }359 }
358 360
359 /* alloc image and output buffer */361 ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height,
360 outbuf_size = 100000;362 c->pix_fmt, 32);
361 outbuf = malloc(outbuf_size);363 if (ret < 0) {
362 size = c->width * c->height;364 fprintf(stderr, "could not alloc raw picture buffer\n");
363 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */365 exit(1);
364 366 }
365 picture->data[0] = picture_buf;367 picture->format = c->pix_fmt;
366 picture->data[1] = picture->data[0] + size;368 picture->width = c->width;
367 picture->data[2] = picture->data[1] + size / 4;369 picture->height = c->height;
368 picture->linesize[0] = c->width;
369 picture->linesize[1] = c->width / 2;
370 picture->linesize[2] = c->width / 2;
371 370
372 /* encode 1 second of video */371 /* encode 1 second of video */
373 for(i=0;i<25;i++) {372 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
374 fflush(stdout);377 fflush(stdout);
375 /* prepare a dummy image */378 /* prepare a dummy image */
376 /* Y */379 /* Y */
@@ -388,33 +391,46 @@ static void video_encode_example(const char *filename) · 11 unmodified lines
388 }391 }
389 }392 }
390 393
394 picture->pts = i;
395
391 /* encode the image */396 /* encode the image */
392 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);397 ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
393 printf("encoding frame %3d (size=%5d)\n", i, out_size);398 if (ret < 0) {
394 fwrite(outbuf, 1, out_size, f);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 }
395 }408 }
396 409
397 /* get the delayed frames */410 /* get the delayed frames */
398 for(; out_size; i++) {411 for (got_output = 1; got_output; i++) {
399 fflush(stdout);412 fflush(stdout);
400 413
401 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);414 ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
402 printf("write frame %3d (size=%5d)\n", i, out_size);415 if (ret < 0) {
403 fwrite(outbuf, 1, out_size, f);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 }
404 }425 }
405 426
406 /* add sequence end code to have a real mpeg file */427 /* add sequence end code to have a real mpeg file */
407 outbuf[0] = 0x00;428 fwrite(endcode, 1, sizeof(endcode), f);
408 outbuf[1] = 0x00;
409 outbuf[2] = 0x01;
410 outbuf[3] = 0xb7;
411 fwrite(outbuf, 1, 4, f);
412 fclose(f);429 fclose(f);
413 free(picture_buf);
414 free(outbuf);
415 430
416 avcodec_close(c);431 avcodec_close(c);
417 av_free(c);432 av_free(c);
433 av_freep(&picture->data[0]);
418 av_free(picture);434 av_free(picture);
419 printf("\n");435 printf("\n");
420}436}

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