History

examples/decoding_encoding: add and use the decode_write_frame function

Allow to factorize code in video_decode_example(), simplify.

Stefano Sabatini committed 14 years ago

Commit 33c45351 parent 4a227a71 file · +32 −33Browse files

Showing the changes to doc/examples/decoding_encoding.c (1 of 1 file). Show all

Unified · Split

doc/examples/decoding_encoding.c+32−33
@@ -487,15 +487,42 @@ static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, · 486 unmodified lines
487 fclose(f);487 fclose(f);
488}488}
489 489
490static 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
490static void video_decode_example(const char *outfilename, const char *filename)518static void video_decode_example(const char *outfilename, const char *filename)
491{519{
492 AVCodec *codec;520 AVCodec *codec;
493 AVCodecContext *c= NULL;521 AVCodecContext *c= NULL;
494 int frame_count, got_frame, len;522 int frame_count;
495 FILE *f;523 FILE *f;
496 AVFrame *frame;524 AVFrame *frame;
497 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];525 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
498 char buf[1024];
499 AVPacket avpkt;526 AVPacket avpkt;
500 527
501 av_init_packet(&avpkt);528 av_init_packet(&avpkt);
@@ -565,26 +592,9 @@ static void video_decode_example(const char *outfilename, const char *filename) · 63 unmodified lines
565 /* here, we use a stream based decoder (mpeg1video), so we592 /* here, we use a stream based decoder (mpeg1video), so we
566 feed decoder and see if it could decode a frame */593 feed decoder and see if it could decode a frame */
567 avpkt.data = inbuf;594 avpkt.data = inbuf;
568 while (avpkt.size > 0) {595 while (avpkt.size > 0)
569 len = avcodec_decode_video2(c, frame, &got_frame, &avpkt);596 if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
570 if (len < 0) {
571 fprintf(stderr, "Error while decoding frame %d\n", frame_count);
572 exit(1);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 /* some codecs, such as MPEG, transmit the I and P frame with a600 /* 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 chance to get the last frame of the video */602 chance to get the last frame of the video */
593 avpkt.data = NULL;603 avpkt.data = NULL;
594 avpkt.size = 0;604 avpkt.size = 0;
595 len = avcodec_decode_video2(c, frame, &got_frame, &avpkt);605 decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);
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 }
607 606
608 fclose(f);607 fclose(f);
609 608

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