1 /*
2 * Copyright (c) 2012 Stefano Sabatini
3 * Copyright (c) 2014 Clément Bœsch
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24 /**
25 * @file libavcodec motion vectors extraction API usage example
26 * @example extract_mvs.c
27 *
28 * Read from input file, decode video stream and print a motion vectors
29 * representation to stdout.
30 */
31
35
40
44
46 {
49 fprintf(stderr,
"Error while sending a packet to the decoder: %s\n",
av_err2str(
ret));
51 }
52
56 break;
58 fprintf(stderr,
"Error while receiving a frame from the decoder: %s\n",
av_err2str(
ret));
60 }
61
65
68 if (sd) {
70 for (
i = 0;
i < sd->
size /
sizeof(*mvs);
i++) {
72 printf(
"%d,%2d,%2d,%2d,%4d,%4d,%4d,%4d,0x%"PRIx64
",%4d,%4d,%4d\n",
75 mv->dst_x,
mv->dst_y,
mv->flags,
76 mv->motion_x,
mv->motion_y,
mv->motion_scale);
77 }
78 }
80 }
81 }
82
83 return 0;
84 }
85
87 {
93
96 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
99 } else {
100 int stream_idx =
ret;
102
105 fprintf(stderr, "Failed to allocate codec\n");
107 }
108
111 fprintf(stderr, "Failed to copy codec parameters to codec context\n");
113 }
114
115 /* Init the video decoder */
120 fprintf(stderr, "Failed to open %s codec\n",
123 }
124
128 }
129
130 return 0;
131 }
132
133 int main(
int argc,
char **argv)
134 {
137
138 if (argc != 2) {
139 fprintf(stderr, "Usage: %s <video>\n", argv[0]);
140 exit(1);
141 }
143
145 fprintf(stderr,
"Could not open source file %s\n",
src_filename);
146 exit(1);
147 }
148
150 fprintf(stderr, "Could not find stream information\n");
151 exit(1);
152 }
153
155
157
159 fprintf(stderr, "Could not find video stream in the input, aborting\n");
161 goto end;
162 }
163
166 fprintf(stderr, "Could not allocate frame\n");
168 goto end;
169 }
170
173 fprintf(stderr, "Could not allocate AVPacket\n");
175 goto end;
176 }
177
178 printf(
"framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,flags,motion_x,motion_y,motion_scale\n");
179
180 /* read frames from the file */
186 break;
187 }
188
189 /* flush cached frames */
191
192 end:
198 }