Showing the changes to libavcodec/api-example.c (1 of 1 file). Show all
Unified · Split
| @@ -38,6 +38,7 @@ · 37 unmodified lines | ||
| 38 | 38 | |
| 39 | 39 | #include "libavcodec/avcodec.h" |
| 40 | 40 | #include "libavutil/audioconvert.h" |
| 41 | +#include "libavutil/imgutils.h" | |
| 41 | 42 | #include "libavutil/mathematics.h" |
| 42 | 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 | 318 | AVCodec *codec; |
| 318 | 319 | AVCodecContext *c= NULL; |
| 319 | − int i, out_size, size, x, y, outbuf_size; | |
| 320 | + int i, ret, x, y, got_output; | |
| 320 | 321 | FILE *f; |
| 321 | 322 | AVFrame *picture; |
| 322 | − uint8_t *outbuf, *picture_buf; | |
| 323 | + AVPacket pkt; | |
| 324 | + uint8_t endcode[] = { 0, 0, 1, 0xb7 }; | |
| 323 | 325 | |
| 324 | 326 | printf("Video encoding\n"); |
| 325 | 327 | |
| @@ -356,21 +358,22 @@ static void video_encode_example(const char *filename) · 30 unmodified lines | ||
| 356 | 358 | exit(1); |
| 357 | 359 | } |
| 358 | 360 | |
| 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; | |
| 371 | 370 | |
| 372 | 371 | /* encode 1 second of video */ |
| 373 | 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 | 377 | fflush(stdout); |
| 375 | 378 | /* prepare a dummy image */ |
| 376 | 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 | 396 | /* 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 | + } | |
| 395 | 408 | } |
| 396 | 409 | |
| 397 | 410 | /* get the delayed frames */ |
| 398 | − for(; out_size; i++) { | |
| 411 | + for (got_output = 1; got_output; i++) { | |
| 399 | 412 | fflush(stdout); |
| 400 | 413 | |
| 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 | + } | |
| 404 | 425 | } |
| 405 | 426 | |
| 406 | 427 | /* 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); | |
| 412 | 429 | fclose(f); |
| 413 | − free(picture_buf); | |
| 414 | − free(outbuf); | |
| 415 | 430 | |
| 416 | 431 | avcodec_close(c); |
| 417 | 432 | av_free(c); |
| 433 | + av_freep(&picture->data[0]); | |
| 418 | 434 | av_free(picture); |
| 419 | 435 | printf("\n"); |
| 420 | 436 | } |