* qatar/master: rtmp: Add support for SWFVerification api-example: use new video encoding API. x86: avcodec: Appropriately name files containing only init functions mpegvideo_mmx_template: drop some commented-out cruft libavresample: add mix level normalization option w32pthreads: Add missing #includes to make header compile standalone rtmp: Gracefully ignore _checkbw errors by tracking them rtmp: Do not send _checkbw calls as notifications prores: interlaced ProRes encoding Conflicts: doc/examples/decoding_encoding.c libavcodec/proresenc_kostya.c libavcodec/w32pthreads.h libavcodec/x86/Makefile libavformat/version.h
Showing the changes to doc/examples/decoding_encoding.c (1 of 16 files). Show all
Unified · Split
| @@ -31,10 +31,10 @@ · 30 unmodified lines | ||
| 31 | 31 | |
| 32 | 32 | #include <math.h> |
| 33 | 33 | |
| 34 | −#include <libavutil/imgutils.h> | |
| 35 | 34 | #include <libavutil/opt.h> |
| 36 | 35 | #include <libavcodec/avcodec.h> |
| 37 | 36 | #include <libavutil/audioconvert.h> |
| 37 | +#include <libavutil/imgutils.h> | |
| 38 | 38 | #include <libavutil/mathematics.h> |
| 39 | 39 | #include <libavutil/samplefmt.h> |
| 40 | 40 | |
| @@ -315,11 +315,11 @@ static void video_encode_example(const char *filename, int codec_id) · 274 unmodified lines | ||
| 315 | 315 | { |
| 316 | 316 | AVCodec *codec; |
| 317 | 317 | AVCodecContext *c= NULL; |
| 318 | − int i, out_size, x, y, outbuf_size; | |
| 318 | + int i, ret, x, y, got_output; | |
| 319 | 319 | FILE *f; |
| 320 | 320 | AVFrame *picture; |
| 321 | − uint8_t *outbuf; | |
| 322 | − int had_output=0; | |
| 321 | + AVPacket pkt; | |
| 322 | + uint8_t endcode[] = { 0, 0, 1, 0xb7 }; | |
| 323 | 323 | |
| 324 | 324 | printf("Encode video file %s\n", filename); |
| 325 | 325 | |
| @@ -359,17 +359,25 @@ static void video_encode_example(const char *filename, int codec_id) · 33 unmodified lines | ||
| 359 | 359 | exit(1); |
| 360 | 360 | } |
| 361 | 361 | |
| 362 | − /* alloc image and output buffer */ | |
| 363 | − outbuf_size = 100000 + 12*c->width*c->height; | |
| 364 | − outbuf = malloc(outbuf_size); | |
| 365 | − | |
| 366 | 362 | /* the image can be allocated by any means and av_image_alloc() is |
| 367 | 363 | * just the most convenient way if av_malloc() is to be used */ |
| 368 | − av_image_alloc(picture->data, picture->linesize, | |
| 369 | − c->width, c->height, c->pix_fmt, 1); | |
| 364 | + ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height, | |
| 365 | + c->pix_fmt, 32); | |
| 366 | + if (ret < 0) { | |
| 367 | + fprintf(stderr, "could not alloc raw picture buffer\n"); | |
| 368 | + exit(1); | |
| 369 | + } | |
| 370 | + | |
| 371 | + picture->format = c->pix_fmt; | |
| 372 | + picture->width = c->width; | |
| 373 | + picture->height = c->height; | |
| 370 | 374 | |
| 371 | 375 | /* encode 1 second of video */ |
| 372 | 376 | for(i=0;i<25;i++) { |
| 377 | + av_init_packet(&pkt); | |
| 378 | + pkt.data = NULL; // packet data will be allocated by the encoder | |
| 379 | + pkt.size = 0; | |
| 380 | + | |
| 373 | 381 | fflush(stdout); |
| 374 | 382 | /* prepare a dummy image */ |
| 375 | 383 | /* Y */ |
| @@ -387,35 +395,46 @@ static void video_encode_example(const char *filename, int codec_id) · 11 unmodified lines | ||
| 387 | 395 | } |
| 388 | 396 | } |
| 389 | 397 | |
| 398 | + picture->pts = i; | |
| 399 | + | |
| 390 | 400 | /* encode the image */ |
| 391 | − out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture); | |
| 392 | − had_output |= out_size; | |
| 393 | − printf("encoding frame %3d (size=%5d)\n", i, out_size); | |
| 394 | − fwrite(outbuf, 1, out_size, f); | |
| 401 | + ret = avcodec_encode_video2(c, &pkt, picture, &got_output); | |
| 402 | + if (ret < 0) { | |
| 403 | + fprintf(stderr, "error encoding frame\n"); | |
| 404 | + exit(1); | |
| 405 | + } | |
| 406 | + | |
| 407 | + if (got_output) { | |
| 408 | + printf("encoding frame %3d (size=%5d)\n", i, pkt.size); | |
| 409 | + fwrite(pkt.data, 1, pkt.size, f); | |
| 410 | + av_free_packet(&pkt); | |
| 411 | + } | |
| 395 | 412 | } |
| 396 | 413 | |
| 397 | 414 | /* get the delayed frames */ |
| 398 | − for(; out_size || !had_output; i++) { | |
| 415 | + for (got_output = 1; got_output; i++) { | |
| 399 | 416 | fflush(stdout); |
| 400 | 417 | |
| 401 | − out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL); | |
| 402 | − had_output |= out_size; | |
| 403 | − printf("write frame %3d (size=%5d)\n", i, out_size); | |
| 404 | − fwrite(outbuf, 1, out_size, f); | |
| 418 | + ret = avcodec_encode_video2(c, &pkt, NULL, &got_output); | |
| 419 | + if (ret < 0) { | |
| 420 | + fprintf(stderr, "error encoding frame\n"); | |
| 421 | + exit(1); | |
| 422 | + } | |
| 423 | + | |
| 424 | + if (got_output) { | |
| 425 | + printf("write frame %3d (size=%5d)\n", i, pkt.size); | |
| 426 | + fwrite(pkt.data, 1, pkt.size, f); | |
| 427 | + av_free_packet(&pkt); | |
| 428 | + } | |
| 405 | 429 | } |
| 406 | 430 | |
| 407 | 431 | /* add sequence end code to have a real mpeg file */ |
| 408 | − outbuf[0] = 0x00; | |
| 409 | − outbuf[1] = 0x00; | |
| 410 | − outbuf[2] = 0x01; | |
| 411 | − outbuf[3] = 0xb7; | |
| 412 | − fwrite(outbuf, 1, 4, f); | |
| 432 | + fwrite(endcode, 1, sizeof(endcode), f); | |
| 413 | 433 | fclose(f); |
| 414 | − free(outbuf); | |
| 415 | 434 | |
| 416 | 435 | avcodec_close(c); |
| 417 | 436 | av_free(c); |
| 418 | − av_free(picture->data[0]); | |
| 437 | + av_freep(&picture->data[0]); | |
| 419 | 438 | av_free(picture); |
| 420 | 439 | printf("\n"); |
| 421 | 440 | } |