Allow to factorize code in video_decode_example(), simplify.
Showing the changes to doc/examples/decoding_encoding.c (1 of 1 file). Show all
Unified · Split
| @@ -487,15 +487,42 @@ static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, · 486 unmodified lines | ||
| 487 | 487 | fclose(f); |
| 488 | 488 | } |
| 489 | 489 | |
| 490 | +static int decode_write_frame(const char *outfilename, AVCodecContext *avctx, | |
| 491 | + AVFrame *frame, int *frame_count, AVPacket *pkt, int last) | |
| 492 | +{ | |
| 493 | + int len, got_frame; | |
| 494 | + char buf[1024]; | |
| 495 | + | |
| 496 | + len = avcodec_decode_video2(avctx, frame, &got_frame, pkt); | |
| 497 | + if (len < 0) { | |
| 498 | + fprintf(stderr, "Error while decoding frame %d\n", *frame_count); | |
| 499 | + return len; | |
| 500 | + } | |
| 501 | + if (got_frame) { | |
| 502 | + printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count); | |
| 503 | + fflush(stdout); | |
| 504 | + | |
| 505 | + /* the picture is allocated by the decoder, no need to free it */ | |
| 506 | + snprintf(buf, sizeof(buf), outfilename, *frame_count); | |
| 507 | + pgm_save(frame->data[0], frame->linesize[0], | |
| 508 | + avctx->width, avctx->height, buf); | |
| 509 | + (*frame_count)++; | |
| 510 | + } | |
| 511 | + if (pkt->data) { | |
| 512 | + pkt->size -= len; | |
| 513 | + pkt->data += len; | |
| 514 | + } | |
| 515 | + return 0; | |
| 516 | +} | |
| 517 | + | |
| 490 | 518 | static void video_decode_example(const char *outfilename, const char *filename) |
| 491 | 519 | { |
| 492 | 520 | AVCodec *codec; |
| 493 | 521 | AVCodecContext *c= NULL; |
| 494 | − int frame_count, got_frame, len; | |
| 522 | + int frame_count; | |
| 495 | 523 | FILE *f; |
| 496 | 524 | AVFrame *frame; |
| 497 | 525 | uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
| 498 | − char buf[1024]; | |
| 499 | 526 | AVPacket avpkt; |
| 500 | 527 | |
| 501 | 528 | av_init_packet(&avpkt); |
| @@ -565,26 +592,9 @@ static void video_decode_example(const char *outfilename, const char *filename) · 63 unmodified lines | ||
| 565 | 592 | /* here, we use a stream based decoder (mpeg1video), so we |
| 566 | 593 | feed decoder and see if it could decode a frame */ |
| 567 | 594 | avpkt.data = inbuf; |
| 568 | − while (avpkt.size > 0) { | |
| 569 | − len = avcodec_decode_video2(c, frame, &got_frame, &avpkt); | |
| 570 | − if (len < 0) { | |
| 571 | − fprintf(stderr, "Error while decoding frame %d\n", frame_count); | |
| 595 | + while (avpkt.size > 0) | |
| 596 | + if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0) | |
| 572 | 597 | exit(1); |
| 573 | − } | |
| 574 | − if (got_frame) { | |
| 575 | − printf("Saving frame %3d\n", frame_count); | |
| 576 | − fflush(stdout); | |
| 577 | − | |
| 578 | − /* the picture is allocated by the decoder. no need to | |
| 579 | − free it */ | |
| 580 | − snprintf(buf, sizeof(buf), outfilename, frame_count); | |
| 581 | − pgm_save(frame->data[0], frame->linesize[0], | |
| 582 | − c->width, c->height, buf); | |
| 583 | − frame_count++; | |
| 584 | − } | |
| 585 | − avpkt.size -= len; | |
| 586 | − avpkt.data += len; | |
| 587 | − } | |
| 588 | 598 | } |
| 589 | 599 | |
| 590 | 600 | /* some codecs, such as MPEG, transmit the I and P frame with a |
| @@ -592,18 +602,7 @@ static void video_decode_example(const char *outfilename, const char *filename) · 1 unmodified line | ||
| 592 | 602 | chance to get the last frame of the video */ |
| 593 | 603 | avpkt.data = NULL; |
| 594 | 604 | avpkt.size = 0; |
| 595 | − len = avcodec_decode_video2(c, frame, &got_frame, &avpkt); | |
| 596 | − if (got_frame) { | |
| 597 | − printf("Saving last frame %3d\n", frame_count); | |
| 598 | − fflush(stdout); | |
| 599 | − | |
| 600 | − /* the picture is allocated by the decoder. no need to | |
| 601 | − free it */ | |
| 602 | − snprintf(buf, sizeof(buf), outfilename, frame_count); | |
| 603 | − pgm_save(frame->data[0], frame->linesize[0], | |
| 604 | − c->width, c->height, buf); | |
| 605 | − frame_count++; | |
| 606 | − } | |
| 605 | + decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1); | |
| 607 | 606 | |
| 608 | 607 | fclose(f); |
| 609 | 608 | |