debian/patches: new FFmpeg-5.0-compatibility.patch
[libam7xxx.git] / contrib / libam7xxx-compile-on-Ubuntu-14.04-LTS.diff
1 diff --git a/examples/am7xxx-modeswitch.c b/examples/am7xxx-modeswitch.c
2 index c304515..4f132d6 100644
3 --- a/examples/am7xxx-modeswitch.c
4 +++ b/examples/am7xxx-modeswitch.c
5 @@ -46,7 +46,7 @@ int main(void)
6                 goto out;
7         }
8  
9 -       libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_INFO);
10 +       libusb_set_debug(NULL, 3);
11  
12         usb_device = libusb_open_device_with_vid_pid(NULL,
13                                                      AM7XXX_STORAGE_VID,
14 diff --git a/examples/am7xxx-play.c b/examples/am7xxx-play.c
15 index 81aff84..1ee42e0 100644
16 --- a/examples/am7xxx-play.c
17 +++ b/examples/am7xxx-play.c
18 @@ -31,7 +31,6 @@
19  
20  #include <libavdevice/avdevice.h>
21  #include <libavformat/avformat.h>
22 -#include <libavutil/imgutils.h>
23  #include <libswscale/swscale.h>
24  
25  #include <am7xxx.h>
26 @@ -51,10 +50,10 @@ static int video_input_init(struct video_input_ctx *input_ctx,
27  {
28         AVInputFormat *input_format = NULL;
29         AVFormatContext *input_format_ctx;
30 -       AVCodecParameters *input_codec_params;
31         AVCodecContext *input_codec_ctx;
32         AVCodec *input_codec;
33         int video_index;
34 +       unsigned int i;
35         int ret;
36  
37         avdevice_register_all();
38 @@ -99,32 +98,34 @@ static int video_input_init(struct video_input_ctx *input_ctx,
39         av_dump_format(input_format_ctx, 0, input_path, 0);
40  
41         /* look for the first video_stream */
42 -       video_index = av_find_best_stream(input_format_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &input_codec, 0);
43 -       if (video_index < 0) {
44 +       video_index = -1;
45 +       for (i = 0; i < input_format_ctx->nb_streams; i++)
46 +               if (input_format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
47 +                       video_index = i;
48 +                       break;
49 +               }
50 +       if (video_index == -1) {
51                 fprintf(stderr, "cannot find any video streams\n");
52                 ret = -EINVAL;
53                 goto cleanup;
54         }
55  
56 -       input_codec_ctx = avcodec_alloc_context3(input_codec);
57 -       if (input_codec_ctx == NULL) {
58 -               fprintf(stderr, "failed to allocate the input codec context\n");
59 -               ret = -ENOMEM;
60 -               goto cleanup;
61 -       }
62 +       /* get a pointer to the codec context for the video stream */
63 +       input_codec_ctx = input_format_ctx->streams[video_index]->codec;
64  
65 -       input_codec_params = input_format_ctx->streams[video_index]->codecpar;
66 -       ret = avcodec_parameters_to_context(input_codec_ctx, input_codec_params);
67 -       if (ret < 0) {
68 -               fprintf(stderr, "cannot copy parameters to input codec context\n");
69 -               goto cleanup_ctx;
70 +       /* find the decoder for the video stream */
71 +       input_codec = avcodec_find_decoder(input_codec_ctx->codec_id);
72 +       if (input_codec == NULL) {
73 +               fprintf(stderr, "input_codec is NULL!\n");
74 +               ret = -EINVAL;
75 +               goto cleanup;
76         }
77  
78         /* open the decoder */
79         ret = avcodec_open2(input_codec_ctx, input_codec, NULL);
80         if (ret < 0) {
81                 fprintf(stderr, "cannot open input codec\n");
82 -               goto cleanup_ctx;
83 +               goto cleanup;
84         }
85  
86         input_ctx->format_ctx = input_format_ctx;
87 @@ -134,8 +135,6 @@ static int video_input_init(struct video_input_ctx *input_ctx,
88         ret = 0;
89         goto out;
90  
91 -cleanup_ctx:
92 -       avcodec_free_context(&input_codec_ctx);
93  cleanup:
94         avformat_close_input(&input_format_ctx);
95  out:
96 @@ -177,7 +176,7 @@ static int video_output_init(struct video_output_ctx *output_ctx,
97                 goto out;
98         }
99  
100 -       /* Calculate the new output dimension so the original frame is shown
101 +       /* Calculate the new output dimension so the original picture is shown
102          * in its entirety */
103         ret = am7xxx_calc_scaled_image_dimensions(dev,
104                                                   upscale,
105 @@ -226,7 +225,7 @@ static int video_output_init(struct video_output_ctx *output_ctx,
106         output_codec_ctx->qmin       = output_codec_ctx->qmax = ((100 - (quality - 1)) * FF_QUALITY_SCALE) / 100;
107         output_codec_ctx->mb_lmin    = output_codec_ctx->qmin * FF_QP2LAMBDA;
108         output_codec_ctx->mb_lmax    = output_codec_ctx->qmax * FF_QP2LAMBDA;
109 -       output_codec_ctx->flags      |= AV_CODEC_FLAG_QSCALE;
110 +       output_codec_ctx->flags      |= CODEC_FLAG_QSCALE;
111         output_codec_ctx->global_quality = output_codec_ctx->qmin * FF_QP2LAMBDA;
112  
113         /* find the encoder */
114 @@ -257,67 +256,6 @@ out:
115  }
116  
117  
118 -/*
119 - * Wrap the new avcodec API from FFMpeg 3.1 to minimize the changes in the
120 - * user code.
121 - *
122 - * If the use of the wrappers were to be made conditional, a check like the
123 - * following could be used:
124 - *
125 - *     #if (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101))
126 - *
127 - * As derived from the APIchanges document:
128 - * https://github.com/FFmpeg/FFmpeg/blob/master/doc/APIchanges
129 - *
130 - * The wrapper implementation has been taken from:
131 - * https://blogs.gentoo.org/lu_zero/2016/03/29/new-avcodec-api/
132 - */
133 -static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
134 -{
135 -       int ret;
136 -
137 -       *got_frame = 0;
138 -
139 -       if (pkt) {
140 -               ret = avcodec_send_packet(avctx, pkt);
141 -               /*
142 -                * In particular, we don't expect AVERROR(EAGAIN), because we
143 -                * read all decoded frames with avcodec_receive_frame() until
144 -                * done.
145 -                */
146 -               if (ret < 0)
147 -                       return ret == AVERROR_EOF ? 0 : ret;
148 -       }
149 -
150 -       ret = avcodec_receive_frame(avctx, frame);
151 -       if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
152 -               return ret;
153 -       if (ret >= 0)
154 -               *got_frame = 1;
155 -
156 -       return 0;
157 -}
158 -
159 -static int encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *frame)
160 -{
161 -       int ret;
162 -
163 -       *got_packet = 0;
164 -
165 -       ret = avcodec_send_frame(avctx, frame);
166 -       if (ret < 0)
167 -               return ret;
168 -
169 -       ret = avcodec_receive_packet(avctx, pkt);
170 -       if (!ret)
171 -               *got_packet = 1;
172 -       if (ret == AVERROR(EAGAIN))
173 -               return 0;
174 -
175 -       return ret;
176 -}
177 -
178 -
179  static int am7xxx_play(const char *input_format_string,
180                        AVDictionary **input_options,
181                        const char *input_path,
182 @@ -330,16 +268,16 @@ static int am7xxx_play(const char *input_format_string,
183  {
184         struct video_input_ctx input_ctx;
185         struct video_output_ctx output_ctx;
186 -       AVFrame *frame_raw;
187 -       AVFrame *frame_scaled;
188 +       AVFrame *picture_raw;
189 +       AVFrame *picture_scaled;
190         int out_buf_size;
191         uint8_t *out_buf;
192 -       int out_frame_size;
193 -       uint8_t *out_frame;
194 +       int out_picture_size;
195 +       uint8_t *out_picture;
196         struct SwsContext *sw_scale_ctx;
197         AVPacket in_packet;
198         AVPacket out_packet;
199 -       int got_frame;
200 +       int got_picture;
201         int got_packet;
202         int ret;
203  
204 @@ -356,44 +294,41 @@ static int am7xxx_play(const char *input_format_string,
205         }
206  
207         /* allocate an input frame */
208 -       frame_raw = av_frame_alloc();
209 -       if (frame_raw == NULL) {
210 -               fprintf(stderr, "cannot allocate the raw frame!\n");
211 +       picture_raw = avcodec_alloc_frame();
212 +       if (picture_raw == NULL) {
213 +               fprintf(stderr, "cannot allocate the raw picture frame!\n");
214                 ret = -ENOMEM;
215                 goto cleanup_output;
216         }
217  
218         /* allocate output frame */
219 -       frame_scaled = av_frame_alloc();
220 -       if (frame_scaled == NULL) {
221 -               fprintf(stderr, "cannot allocate the scaled frame!\n");
222 +       picture_scaled = avcodec_alloc_frame();
223 +       if (picture_scaled == NULL) {
224 +               fprintf(stderr, "cannot allocate the scaled picture!\n");
225                 ret = -ENOMEM;
226 -               goto cleanup_frame_raw;
227 +               goto cleanup_picture_raw;
228         }
229 -       frame_scaled->format = (output_ctx.codec_ctx)->pix_fmt;
230 -       frame_scaled->width = (output_ctx.codec_ctx)->width;
231 -       frame_scaled->height = (output_ctx.codec_ctx)->height;
232 +       picture_scaled->format = (output_ctx.codec_ctx)->pix_fmt;
233 +       picture_scaled->width = (output_ctx.codec_ctx)->width;
234 +       picture_scaled->height = (output_ctx.codec_ctx)->height;
235  
236         /* calculate the bytes needed for the output image and create buffer for the output image */
237 -       out_buf_size = av_image_get_buffer_size((output_ctx.codec_ctx)->pix_fmt,
238 -                                               (output_ctx.codec_ctx)->width,
239 -                                               (output_ctx.codec_ctx)->height,
240 -                                               1);
241 +       out_buf_size = avpicture_get_size((output_ctx.codec_ctx)->pix_fmt,
242 +                                         (output_ctx.codec_ctx)->width,
243 +                                         (output_ctx.codec_ctx)->height);
244         out_buf = av_malloc(out_buf_size * sizeof(uint8_t));
245         if (out_buf == NULL) {
246                 fprintf(stderr, "cannot allocate output data buffer!\n");
247                 ret = -ENOMEM;
248 -               goto cleanup_frame_scaled;
249 +               goto cleanup_picture_scaled;
250         }
251  
252 -       /* assign appropriate parts of buffer to image planes in frame_scaled */
253 -       av_image_fill_arrays(frame_scaled->data,
254 -                            frame_scaled->linesize,
255 -                            out_buf,
256 -                            (output_ctx.codec_ctx)->pix_fmt,
257 -                            (output_ctx.codec_ctx)->width,
258 -                            (output_ctx.codec_ctx)->height,
259 -                            1);
260 +       /* assign appropriate parts of buffer to image planes in picture_scaled */
261 +       avpicture_fill((AVPicture *)picture_scaled,
262 +                      out_buf,
263 +                      (output_ctx.codec_ctx)->pix_fmt,
264 +                      (output_ctx.codec_ctx)->width,
265 +                      (output_ctx.codec_ctx)->height);
266  
267         sw_scale_ctx = sws_getCachedContext(NULL,
268                                             (input_ctx.codec_ctx)->width,
269 @@ -430,8 +365,8 @@ static int am7xxx_play(const char *input_format_string,
270                 }
271  
272                 /* decode */
273 -               got_frame = 0;
274 -               ret = decode(input_ctx.codec_ctx, frame_raw, &got_frame, &in_packet);
275 +               got_picture = 0;
276 +               ret = avcodec_decode_video2(input_ctx.codec_ctx, picture_raw, &got_picture, &in_packet);
277                 if (ret < 0) {
278                         fprintf(stderr, "cannot decode video\n");
279                         run = 0;
280 @@ -439,41 +374,41 @@ static int am7xxx_play(const char *input_format_string,
281                 }
282  
283                 /* if we got the complete frame */
284 -               if (got_frame) {
285 +               if (got_picture) {
286                         /*
287 -                        * Rescaling the frame also changes its pixel format
288 +                        * Rescaling the picture also changes its pixel format
289                          * to the raw format supported by the projector if
290                          * this was set in video_output_init()
291                          */
292                         sws_scale(sw_scale_ctx,
293 -                                 (const uint8_t * const *)frame_raw->data,
294 -                                 frame_raw->linesize,
295 +                                 (const uint8_t * const *)picture_raw->data,
296 +                                 picture_raw->linesize,
297                                   0,
298                                   (input_ctx.codec_ctx)->height,
299 -                                 frame_scaled->data,
300 -                                 frame_scaled->linesize);
301 +                                 picture_scaled->data,
302 +                                 picture_scaled->linesize);
303  
304                         if (output_ctx.raw_output) {
305 -                               out_frame = out_buf;
306 -                               out_frame_size = out_buf_size;
307 +                               out_picture = out_buf;
308 +                               out_picture_size = out_buf_size;
309                         } else {
310 -                               frame_scaled->quality = (output_ctx.codec_ctx)->global_quality;
311 +                               picture_scaled->quality = (output_ctx.codec_ctx)->global_quality;
312                                 av_init_packet(&out_packet);
313                                 out_packet.data = NULL;
314                                 out_packet.size = 0;
315                                 got_packet = 0;
316 -                               ret = encode(output_ctx.codec_ctx,
317 -                                            &out_packet,
318 -                                            &got_packet,
319 -                                            frame_scaled);
320 +                               ret = avcodec_encode_video2(output_ctx.codec_ctx,
321 +                                                           &out_packet,
322 +                                                           picture_scaled,
323 +                                                           &got_packet);
324                                 if (ret < 0 || !got_packet) {
325                                         fprintf(stderr, "cannot encode video\n");
326                                         run = 0;
327                                         goto end_while;
328                                 }
329  
330 -                               out_frame = out_packet.data;
331 -                               out_frame_size = out_packet.size;
332 +                               out_picture = out_packet.data;
333 +                               out_picture_size = out_packet.size;
334                         }
335  
336  #ifdef DEBUG
337 @@ -485,7 +420,7 @@ static int am7xxx_play(const char *input_format_string,
338                                 else
339                                         snprintf(filename, NAME_MAX, "out.raw");
340                                 file = fopen(filename, "wb");
341 -                               fwrite(out_frame, 1, out_frame_size, file);
342 +                               fwrite(out_picture, 1, out_picture_size, file);
343                                 fclose(file);
344                         }
345  #else
346 @@ -496,8 +431,8 @@ static int am7xxx_play(const char *input_format_string,
347                                                       image_format,
348                                                       (output_ctx.codec_ctx)->width,
349                                                       (output_ctx.codec_ctx)->height,
350 -                                                     out_frame,
351 -                                                     out_frame_size);
352 +                                                     out_picture,
353 +                                                     out_picture_size);
354                         if (ret < 0) {
355                                 perror("am7xxx_send_image_async");
356                                 run = 0;
357 @@ -506,17 +441,17 @@ static int am7xxx_play(const char *input_format_string,
358                 }
359  end_while:
360                 if (!output_ctx.raw_output && got_packet)
361 -                       av_packet_unref(&out_packet);
362 -               av_packet_unref(&in_packet);
363 +                       av_free_packet(&out_packet);
364 +               av_free_packet(&in_packet);
365         }
366  
367         sws_freeContext(sw_scale_ctx);
368  cleanup_out_buf:
369         av_free(out_buf);
370 -cleanup_frame_scaled:
371 -       av_frame_free(&frame_scaled);
372 -cleanup_frame_raw:
373 -       av_frame_free(&frame_raw);
374 +cleanup_picture_scaled:
375 +       avcodec_free_frame(&picture_scaled);
376 +cleanup_picture_raw:
377 +       avcodec_free_frame(&picture_raw);
378  
379  cleanup_output:
380         /* Freeing the codec context is needed as well,
381 @@ -527,7 +462,6 @@ cleanup_output:
382  
383  cleanup_input:
384         avcodec_close(input_ctx.codec_ctx);
385 -       avcodec_free_context(&(input_ctx.codec_ctx));
386         avformat_close_input(&(input_ctx.format_ctx));
387  
388  out:
389 diff --git a/src/am7xxx.c b/src/am7xxx.c
390 index 8573a59..fd726af 100644
391 --- a/src/am7xxx.c
392 +++ b/src/am7xxx.c
393 @@ -1117,7 +1117,7 @@ AM7XXX_PUBLIC int am7xxx_init(am7xxx_context **ctx)
394                 goto out_free_context;
395         }
396  
397 -       libusb_set_debug((*ctx)->usb_context, LIBUSB_LOG_LEVEL_INFO);
398 +       libusb_set_debug((*ctx)->usb_context, 3);
399  
400         ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
401         if (ret < 0) {