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
487487 fclose(f);
488488 }
489489
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+
490518 static void video_decode_example(const char *outfilename, const char *filename)
491519 {
492520 AVCodec *codec;
493521 AVCodecContext *c= NULL;
494− int frame_count, got_frame, len;
522+ int frame_count;
495523 FILE *f;
496524 AVFrame *frame;
497525 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
498− char buf[1024];
499526 AVPacket avpkt;
500527
501528 av_init_packet(&avpkt);
@@ -565,26 +592,9 @@ static void video_decode_example(const char *outfilename, const char *filename) · 63 unmodified lines
565592 /* here, we use a stream based decoder (mpeg1video), so we
566593 feed decoder and see if it could decode a frame */
567594 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)
572597 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− }
588598 }
589599
590600 /* 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
592602 chance to get the last frame of the video */
593603 avpkt.data = NULL;
594604 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);
607606
608607 fclose(f);
609608

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