]> git.sesse.net Git - ffmpeg/blobdiff - doc/examples/extract_mvs.c
Merge commit '730c02326094bcfb1fa67f10a7e7b22f03f5a88f'
[ffmpeg] / doc / examples / extract_mvs.c
index 8b22b40c116aa6161c8eb90bb99d8e5595811dba..7ae934ead3d510abb8cf47dba5af607f2e9a352c 100644 (file)
@@ -31,23 +31,26 @@ static const char *src_filename = NULL;
 
 static int video_stream_idx = -1;
 static AVFrame *frame = NULL;
-static AVPacket pkt;
 static int video_frame_count = 0;
 
-static int decode_packet(int *got_frame, int cached)
+static int decode_packet(const AVPacket *pkt)
 {
-    int decoded = pkt.size;
-
-    *got_frame = 0;
+    int ret = avcodec_send_packet(video_dec_ctx, pkt);
+    if (ret < 0) {
+        fprintf(stderr, "Error while sending a packet to the decoder: %s\n", av_err2str(ret));
+        return ret;
+    }
 
-    if (pkt.stream_index == video_stream_idx) {
-        int ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
-        if (ret < 0) {
-            fprintf(stderr, "Error decoding video frame (%s)\n", av_err2str(ret));
+    while (ret >= 0)  {
+        ret = avcodec_receive_frame(video_dec_ctx, frame);
+        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
+            break;
+        } else if (ret < 0) {
+            fprintf(stderr, "Error while receiving a frame from the decoder: %s\n", av_err2str(ret));
             return ret;
         }
 
-        if (*got_frame) {
+        if (ret >= 0) {
             int i;
             AVFrameSideData *sd;
 
@@ -58,15 +61,16 @@ static int decode_packet(int *got_frame, int cached)
                 for (i = 0; i < sd->size / sizeof(*mvs); i++) {
                     const AVMotionVector *mv = &mvs[i];
                     printf("%d,%2d,%2d,%2d,%4d,%4d,%4d,%4d,0x%"PRIx64"\n",
-                           video_frame_count, mv->source,
-                           mv->w, mv->h, mv->src_x, mv->src_y,
-                           mv->dst_x, mv->dst_y, mv->flags);
+                        video_frame_count, mv->source,
+                        mv->w, mv->h, mv->src_x, mv->src_y,
+                        mv->dst_x, mv->dst_y, mv->flags);
                 }
             }
+            av_frame_unref(frame);
         }
     }
 
-    return decoded;
+    return 0;
 }
 
 static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
@@ -116,7 +120,8 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
 
 int main(int argc, char **argv)
 {
-    int ret = 0, got_frame;
+    int ret = 0;
+    AVPacket pkt = { 0 };
 
     if (argc != 2) {
         fprintf(stderr, "Usage: %s <video>\n", argv[0]);
@@ -155,30 +160,17 @@ int main(int argc, char **argv)
 
     printf("framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,flags\n");
 
-    /* initialize packet, set data to NULL, let the demuxer fill it */
-    av_init_packet(&pkt);
-    pkt.data = NULL;
-    pkt.size = 0;
-
     /* read frames from the file */
     while (av_read_frame(fmt_ctx, &pkt) >= 0) {
-        AVPacket orig_pkt = pkt;
-        do {
-            ret = decode_packet(&got_frame, 0);
-            if (ret < 0)
-                break;
-            pkt.data += ret;
-            pkt.size -= ret;
-        } while (pkt.size > 0);
-        av_packet_unref(&orig_pkt);
+        if (pkt.stream_index == video_stream_idx)
+            ret = decode_packet(&pkt);
+        av_packet_unref(&pkt);
+        if (ret < 0)
+            break;
     }
 
     /* flush cached frames */
-    pkt.data = NULL;
-    pkt.size = 0;
-    do {
-        decode_packet(&got_frame, 1);
-    } while (got_frame);
+    decode_packet(NULL);
 
 end:
     avcodec_free_context(&video_dec_ctx);