Put the relevant code in one chunk, also check for frame allocation failures.
Showing the changes to doc/examples/decoding_encoding.c (1 of 1 file). Show all
Unified · Split
| @@ -332,7 +332,6 @@ static void video_encode_example(const char *filename, int codec_id) · 331 unmodified lines | |||
| 332 | } | 332 | } |
| 333 | 333 | ||
| 334 | c = avcodec_alloc_context3(codec); | 334 | c = avcodec_alloc_context3(codec); |
| 335 | picture= avcodec_alloc_frame(); | ||
| 336 | 335 | ||
| 337 | /* put sample parameters */ | 336 | /* put sample parameters */ |
| 338 | c->bit_rate = 400000; | 337 | c->bit_rate = 400000; |
| @@ -360,6 +359,15 @@ static void video_encode_example(const char *filename, int codec_id) · 21 unmodified lines | |||
| 360 | exit(1); | 359 | exit(1); |
| 361 | } | 360 | } |
| 362 | 361 | ||
| 362 | picture = avcodec_alloc_frame(); | ||
| 363 | if (!picture) { | ||
| 364 | fprintf(stderr, "Could not allocate video frame\n"); | ||
| 365 | exit(1); | ||
| 366 | } | ||
| 367 | picture->format = c->pix_fmt; | ||
| 368 | picture->width = c->width; | ||
| 369 | picture->height = c->height; | ||
| 370 | |||
| 363 | /* the image can be allocated by any means and av_image_alloc() is | 371 | /* the image can be allocated by any means and av_image_alloc() is |
| 364 | * just the most convenient way if av_malloc() is to be used */ | 372 | * just the most convenient way if av_malloc() is to be used */ |
| 365 | ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height, | 373 | ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height, |
| @@ -369,10 +377,6 @@ static void video_encode_example(const char *filename, int codec_id) · 3 unmodified lines | |||
| 369 | exit(1); | 377 | exit(1); |
| 370 | } | 378 | } |
| 371 | 379 | ||
| 372 | picture->format = c->pix_fmt; | ||
| 373 | picture->width = c->width; | ||
| 374 | picture->height = c->height; | ||
| 375 | |||
| 376 | /* encode 1 second of video */ | 380 | /* encode 1 second of video */ |
| 377 | for(i=0;i<25;i++) { | 381 | for(i=0;i<25;i++) { |
| 378 | av_init_packet(&pkt); | 382 | av_init_packet(&pkt); |
| @@ -483,8 +487,6 @@ static void video_decode_example(const char *outfilename, const char *filename) · 104 unmodified lines | |||
| 483 | } | 487 | } |
| 484 | 488 | ||
| 485 | c = avcodec_alloc_context3(codec); | 489 | c = avcodec_alloc_context3(codec); |
| 486 | picture= avcodec_alloc_frame(); | ||
| 487 | |||
| 488 | if(codec->capabilities&CODEC_CAP_TRUNCATED) | 490 | if(codec->capabilities&CODEC_CAP_TRUNCATED) |
| 489 | c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */ | 491 | c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */ |
| 490 | 492 | ||
| @@ -506,6 +508,12 @@ static void video_decode_example(const char *outfilename, const char *filename) · 15 unmodified lines | |||
| 506 | exit(1); | 508 | exit(1); |
| 507 | } | 509 | } |
| 508 | 510 | ||
| 511 | picture = avcodec_alloc_frame(); | ||
| 512 | if (!picture) { | ||
| 513 | fprintf(stderr, "Could not allocate video frame\n"); | ||
| 514 | exit(1); | ||
| 515 | } | ||
| 516 | |||
| 509 | frame = 0; | 517 | frame = 0; |
| 510 | for(;;) { | 518 | for(;;) { |
| 511 | avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); | 519 | avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); |