ffmpeg api3 upgrade, rework bs filts, rm faac/d, fixes
[goodguy/history.git] / cinelerra-5.1 / cinelerra / ydiff.C
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include <sys/stat.h>
6 #include <sys/time.h>
7
8 extern "C" {
9 #include "libavfilter/buffersrc.h"
10 #include "libavfilter/buffersink.h"
11 #include "libavformat/avformat.h"
12 #include "libavformat/avio.h"
13 #include "libavcodec/avcodec.h"
14 #include "libavfilter/avfilter.h"
15 #include "libavutil/avutil.h"
16 #include "libavutil/imgutils.h"
17 #include "libavutil/opt.h"
18 #include "libavutil/pixdesc.h"
19 #include "libswresample/swresample.h"
20 #include "libswscale/swscale.h"
21 }
22
23 #include <gtk/gtk.h>
24 #include <gdk/gdk.h>
25
26 int done = 0;
27
28 void sigint(int n)
29 {
30   done = 1;
31 }
32
33
34 void dst_exit(GtkWidget *widget, gpointer data)
35 {
36    exit(0);
37 }
38
39 class gtk_window {
40 public:
41   gtk_window(int width, int height);
42   ~gtk_window();
43
44   GdkVisual *visual;
45   GtkWidget *window;
46   GtkWidget *image;
47   GtkWidget *panel_hbox;
48   GdkImage  *img0, *img1;
49   GdkPixbuf *pbuf0, *pbuf1;
50   unsigned char *bfr, *bfrs, *bfr0, *bfr1;
51   unsigned char **rows, **row0, **row1;
52   uint64_t flip_bfrs, flip_rows;
53   int width, height, linesize;
54   int done;
55
56   pthread_t tid;
57   static void *entry(void *t);
58   void start();
59   void *run();
60   uint8_t *next_bfr();
61   void post();
62
63   pthread_mutex_t draw;
64   void draw_lock() { pthread_mutex_lock(&draw); }
65   void draw_unlock() { pthread_mutex_unlock(&draw); }
66 };
67
68 gtk_window::gtk_window(int width, int height)
69 {
70   this->width = width;
71   this->height = height;
72   this->linesize = width*3;
73   visual = gdk_visual_get_system();
74   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
75   gtk_signal_connect(GTK_OBJECT(window),"destroy",
76      GTK_SIGNAL_FUNC(dst_exit),NULL);
77   gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE);
78   /* try for shared image bfr, only seems to work with gtk_rgb */
79   img0 = gdk_image_new(GDK_IMAGE_SHARED, visual, width, height);
80   pbuf0 = gdk_pixbuf_new_from_data((const guchar *)img0->mem,
81      GDK_COLORSPACE_RGB,FALSE,8,width,height,linesize,NULL,NULL);
82   bfr0 = gdk_pixbuf_get_pixels(pbuf0);
83   memset(bfr0,0,height*linesize);
84   image = gtk_image_new_from_pixbuf(pbuf0);
85   /* double buffered */
86   img1 = gdk_image_new(GDK_IMAGE_SHARED, visual, width, height);
87   pbuf1 = gdk_pixbuf_new_from_data((const guchar *)img1->mem,
88      GDK_COLORSPACE_RGB,FALSE,8,width,height,linesize,NULL,NULL);
89   bfr1 = gdk_pixbuf_get_pixels(pbuf1);
90   memset(bfr1,0,height*linesize);
91
92   row0 = new unsigned char *[height];
93   row1 = new unsigned char *[height];
94
95   for( int i=0; i<height; ++i ) {
96     row0[i] = bfr0 + i*linesize;
97     row1[i] = bfr1 + i*linesize;
98   }
99   bfrs = bfr0;
100   rows = row0;
101
102   flip_bfrs = ((uint64_t)bfr0 ^ (uint64_t)bfr1);
103   flip_rows = ((uint64_t)row0 ^ (uint64_t)row1);
104   pthread_mutex_init(&draw, 0);
105   post();
106   
107   panel_hbox = gtk_hbox_new(FALSE,0);
108   gtk_container_add(GTK_CONTAINER(window), panel_hbox);
109   /* pack image into panel */
110   gtk_box_pack_start(GTK_BOX(panel_hbox), image, TRUE, TRUE, 0);
111   gtk_widget_show_all(window);
112 }
113
114 gtk_window::~gtk_window()
115 {
116   gtk_widget_destroy(window);
117   g_object_unref(pbuf0);
118   g_object_unref(img0);
119   g_object_unref(pbuf1);
120   g_object_unref(img1);
121   delete [] row0;
122   delete [] row1;
123   pthread_mutex_destroy(&draw);
124 }
125
126 void *gtk_window::entry(void *t)
127 {
128   return ((gtk_window*)t)->run();
129 }
130
131 void gtk_window::start()
132 {
133   pthread_attr_t attr;
134   pthread_attr_init(&attr);
135   pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
136   done = 0;
137   pthread_create(&tid, &attr, &entry, this);
138   pthread_attr_destroy(&attr);
139 }
140
141 void *gtk_window::run()
142 {
143   while( !done ) {
144     if( !gtk_events_pending() ) {
145       if( !bfr ) { usleep(10000);  continue; }
146       GdkGC *blk = image->style->black_gc;
147       gdk_draw_rgb_image(image->window,blk, 0,0,width,height,
148          GDK_RGB_DITHER_NONE,bfr,linesize);
149       gdk_flush();
150       unsigned long *fbfrs = (unsigned long *)&bfrs;  *fbfrs ^= flip_bfrs;
151       unsigned long *frows = (unsigned long *)&rows;  *frows ^= flip_rows;
152       bfr = 0;
153       draw_unlock();
154     }
155     else
156       gtk_main_iteration();
157   }
158
159   return (void*)0;
160 }
161
162 uint8_t *gtk_window::next_bfr()
163 {
164   return bfrs;
165 }
166
167 void gtk_window::post()
168 {
169   draw_lock();
170   bfr = bfrs;
171 }
172
173
174 class ffcmpr {
175 public:
176   ffcmpr();
177   ~ffcmpr();
178   AVPacket ipkt;
179   AVFormatContext *fmt_ctx;
180   AVFrame *ipic;
181   AVStream *st;
182   AVCodecContext *ctx;
183   AVPixelFormat pix_fmt;
184   double frame_rate;
185   int width, height;
186   int need_packet, eof;
187   int open_decoder(const char *filename, int vid_no);
188   void close_decoder();
189   AVFrame *read_frame();
190 };
191
192 ffcmpr::ffcmpr()
193 {
194   av_init_packet(&this->ipkt);
195   this->fmt_ctx = 0;
196   this->ipic = 0;
197   this->st = 0;
198   this->ctx = 0 ;
199   this->frame_rate = 0;
200   this->need_packet = 0;
201   this->eof = 0;
202   this->pix_fmt = AV_PIX_FMT_NONE;
203   width = height = 0;
204 }
205
206 void ffcmpr::close_decoder()
207 {
208   av_packet_unref(&ipkt);
209   if( !fmt_ctx ) return;
210   if( ctx ) avcodec_free_context(&ctx);
211   avformat_close_input(&fmt_ctx);
212   av_frame_free(&ipic);
213 }
214
215 ffcmpr::~ffcmpr()
216 {
217   close_decoder();
218 }
219
220 int ffcmpr::open_decoder(const char *filename, int vid_no)
221 {
222   struct stat fst;
223   if( stat(filename, &fst) ) return 1;
224
225   av_log_set_level(AV_LOG_VERBOSE);
226   fmt_ctx = 0;
227   AVDictionary *fopts = 0;
228   av_register_all();
229   av_dict_set(&fopts, "formatprobesize", "5000000", 0);
230   av_dict_set(&fopts, "scan_all_pmts", "1", 0);
231   av_dict_set(&fopts, "threads", "auto", 0);
232   int ret = avformat_open_input(&fmt_ctx, filename, NULL, &fopts);
233   av_dict_free(&fopts);
234   if( ret < 0 ) {
235     fprintf(stderr,"file open failed: %s\n", filename);
236     return ret;
237   }
238   ret = avformat_find_stream_info(fmt_ctx, NULL);
239   if( ret < 0 ) {
240     fprintf(stderr,"file probe failed: %s\n", filename);
241     return ret;
242   }
243
244   this->st = 0;
245   for( int i=0; !this->st && ret>=0 && i<(int)fmt_ctx->nb_streams; ++i ) {
246     AVStream *fst = fmt_ctx->streams[i];
247     AVMediaType type = fst->codecpar->codec_type;
248     if( type != AVMEDIA_TYPE_VIDEO ) continue;
249     if( --vid_no < 0 ) this->st = fst;
250   }
251
252   AVCodecID codec_id = st->codecpar->codec_id;
253   AVDictionary *copts = 0;
254   //av_dict_copy(&copts, opts, 0);
255   AVCodec *decoder = avcodec_find_decoder(codec_id);
256   ctx = avcodec_alloc_context3(decoder);
257   avcodec_parameters_to_context(ctx, st->codecpar);
258   if( avcodec_open2(ctx, decoder, &copts) < 0 ) {
259     fprintf(stderr,"codec open failed: %s\n", filename);
260     return -1;
261   }
262   av_dict_free(&copts);
263   ipic = av_frame_alloc();
264   eof = 0;
265   need_packet = 1;
266
267   AVRational framerate = av_guess_frame_rate(fmt_ctx, st, 0);
268   this->frame_rate = !framerate.den ? 0 : (double)framerate.num / framerate.den;
269   this->pix_fmt = (AVPixelFormat)st->codecpar->format;
270   this->width  = st->codecpar->width;
271   this->height = st->codecpar->height;
272   return 0;
273 }
274
275 AVFrame *ffcmpr::read_frame()
276 {
277   av_frame_unref(ipic);
278
279   for( int retrys=1000; --retrys>=0; ) {
280     if( need_packet ) {
281       if( eof ) return 0;
282       AVPacket *pkt = &ipkt;
283       av_packet_unref(pkt);
284       int ret = av_read_frame(fmt_ctx, pkt);
285       if( ret < 0 ) {
286         if( ret != AVERROR_EOF ) return 0;
287         ret = 0;  eof = 1;  pkt = 0;
288       }
289       if( pkt ) {
290         if( pkt->stream_index != st->index ) continue;
291         if( !pkt->data || !pkt->size ) continue;
292       }
293       avcodec_send_packet(ctx, pkt);
294       need_packet = 0;
295     }
296     int ret = avcodec_receive_frame(ctx, ipic);
297     if( ret >= 0 ) return ipic;
298     if( ret != AVERROR(EAGAIN) ) {
299       eof = 1; need_packet = 0;
300       break;
301     }
302     need_packet = 1;
303   }
304   return 0;
305 }
306
307 static int diff_frame(AVFrame *afrm, AVFrame *bfrm, uint8_t *fp, int w, int h)
308 {
309   int n = 0, m = 0;
310   uint8_t *arow = afrm->data[0];
311   uint8_t *brow = bfrm->data[0];
312   int asz = afrm->linesize[0], bsz = afrm->linesize[0];
313   int rsz = w * 3;
314   for( int y=h; --y>=0; arow+=asz, brow+=bsz ) {
315     uint8_t *ap = arow, *bp = brow;
316     for( int x=rsz; --x>=0; ++ap,++bp ) {
317       int d = *ap - *bp, v = d + 128;
318       if( v > 255 ) v = 255;
319       else if( v < 0 ) v = 0;
320       *fp++ = v;
321       m += d;
322       if( d < 0 ) d = -d;
323       n += d;
324     }
325   }
326   int sz = h*rsz;
327   printf("%d %d %d %f", sz, m, n, (double)n/sz);
328   return n;
329 }
330
331 int main(int ac, char **av)
332 {
333   int ret;
334   setbuf(stdout,NULL);
335
336   gtk_set_locale();
337   gtk_init(&ac, &av);
338
339   ffcmpr a, b;
340   if( a.open_decoder(av[1],0) ) return 1;
341   if( b.open_decoder(av[2],0) ) return 1;
342
343   printf("file a:%s\n", av[1]);
344   printf("  id 0x%06x:", a.ctx->codec_id);
345   const AVCodecDescriptor *adesc = avcodec_descriptor_get(a.ctx->codec_id);
346   printf("  video %s\n", adesc ? adesc->name : " (unkn)");
347   printf(" %dx%d %5.2f", a.width, a.height, a.frame_rate);
348   const char *apix = av_get_pix_fmt_name(a.pix_fmt);
349   printf(" pix %s\n", apix ? apix : "(unkn)");
350
351   printf("file b:%s\n", av[2]);
352   printf("  id 0x%06x:", b.ctx->codec_id);
353   const AVCodecDescriptor *bdesc = avcodec_descriptor_get(b.ctx->codec_id);
354   printf("  video %s\n", bdesc ? bdesc->name : " (unkn)");
355   printf(" %dx%d %5.2f", b.width, b.height, b.frame_rate);
356   const char *bpix = av_get_pix_fmt_name(b.pix_fmt);
357   printf(" pix %s\n", bpix ? bpix : "(unkn)");
358
359 //  if( a.ctx->codec_id != b.ctx->codec_id ) { printf("codec mismatch\n"); return 1;}
360   if( a.width != b.width ) { printf("width mismatch\n"); return 1;}
361   if( a.height != b.height ) { printf("height mismatch\n"); return 1;}
362   if( a.frame_rate != b.frame_rate ) { printf("framerate mismatch\n"); return 1;}
363 //  if( a.pix_fmt != b.pix_fmt ) { printf("format mismatch\n"); return 1;}
364
365   signal(SIGINT,sigint);
366
367   struct SwsContext *a_cvt = sws_getCachedContext(0, a.width, a.height, a.pix_fmt,
368                 a.width, a.height, AV_PIX_FMT_RGB24, SWS_POINT, 0, 0, 0);
369   struct SwsContext *b_cvt = sws_getCachedContext(0, b.width, b.height, b.pix_fmt,
370                 b.width, b.height, AV_PIX_FMT_RGB24, SWS_POINT, 0, 0, 0);
371   if( !a_cvt || !b_cvt ) {
372     printf("sws_getCachedContext() failed\n");
373     return 1;
374   }
375
376   AVFrame *afrm = av_frame_alloc();
377   av_image_alloc(afrm->data, afrm->linesize,
378      a.width, a.height, AV_PIX_FMT_RGB24, 1);
379
380   AVFrame *bfrm = av_frame_alloc();
381   av_image_alloc(bfrm->data, bfrm->linesize,
382      b.width, b.height, AV_PIX_FMT_RGB24, 1);
383
384   gtk_window gw(a.width, a.height);
385   gw.start();
386
387   int64_t err = 0;
388   int frm_no = 0;
389
390   if( ac>3 && (ret=atoi(av[3])) ) {
391     while( ret > 0 ) { a.read_frame(); --ret; }
392     while( ret < 0 ) { b.read_frame(); ++ret; }
393   }
394
395   while( !done ) {
396     AVFrame *ap = a.read_frame();
397     if( !ap ) break;
398     AVFrame *bp = b.read_frame();
399     if( !bp ) break;
400     sws_scale(a_cvt, ap->data, ap->linesize, 0, ap->height,
401        afrm->data, afrm->linesize);
402     sws_scale(b_cvt, bp->data, bp->linesize, 0, bp->height,
403        bfrm->data, bfrm->linesize);
404     uint8_t *fbfr = gw.next_bfr();
405     ret = diff_frame(afrm, bfrm, fbfr, ap->width, ap->height);
406     err += ret;  ++frm_no;
407     printf("  %d\n",frm_no);
408     gw.post();
409   }
410
411   av_freep(&afrm->data);
412   av_frame_free(&afrm);
413   av_freep(&bfrm->data);
414   av_frame_free(&bfrm);
415   
416   b.close_decoder();
417   a.close_decoder();
418   return 0;
419 }
420