~ubuntu-branches/ubuntu/trusty/shotdetect/trusty

« back to all changes in this revision

Viewing changes to .pc/compilation_fixes.patch/src/film.cpp

  • Committer: Package Import Robot
  • Author(s): Giulio Paci
  • Date: 2012-04-06 18:52:47 UTC
  • Revision ID: package-import@ubuntu.com-20120406185247-56nvgk9ul4kguncw
Tags: 1.0.86-1
Initial release.
Closes: #475190.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Johan MATHE - johan.mathe@tremplin-utc.net - Centre
 
3
 * Pompfidou - IRI This library is free software; you can redistribute it 
 
4
 * and/or modify it under the terms of the GNU Lesser General Public
 
5
 * License as published by the Free Software Foundation; either version
 
6
 * 2.1 of the License, or (at your option) any later version. This
 
7
 * library is distributed in the hope that it will be useful, but WITHOUT
 
8
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
9
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
10
 * License for more details. You should have received a copy of the GNU
 
11
 * Lesser General Public License along with this library; if not, write to 
 
12
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
13
 * Boston, MA 02110-1301 USA $Id: film.cpp 141 2007-04-02 16:10:53Z
 
14
 * johmathe $ $Date: 2009-09-18 18:27:54 +0100 (Fri, 18 Sep 2009) $ 
 
15
 */
 
16
#include <sys/time.h>
 
17
#include <time.h>
 
18
#include "film.h"
 
19
#include "graph.h"
 
20
extern "C" {
 
21
#include <ffmpeg/swscale.h>
 
22
}
 
23
#define DEBUG
 
24
 
 
25
void
 
26
film::do_stats (int frame_num)
 
27
{
 
28
}
 
29
 
 
30
void
 
31
film::create_main_dir ()
 
32
{
 
33
  ostringstream str;
 
34
  struct stat *buf;
 
35
  str.str ("");
 
36
  str << this->global_path;
 
37
  buf = (struct stat *) malloc (sizeof (struct stat));
 
38
  if (stat (str.str ().c_str (), buf) == -1)
 
39
    {
 
40
#ifdef __WINDOWS__
 
41
      mkdir (str.str ().c_str ());
 
42
#else
 
43
      mkdir (str.str ().c_str (), 0777);
 
44
#endif
 
45
 
 
46
    }
 
47
 
 
48
}
 
49
 
 
50
void
 
51
film::CompareFrame (AVFrame * pFrame, AVFrame * pFramePrev)
 
52
{
 
53
  int y;
 
54
  int x;
 
55
  int diff;
 
56
  char c1, c2, c3;
 
57
  int c1tot, c2tot, c3tot;
 
58
  c1tot = 0;
 
59
  c2tot = 0;
 
60
  c3tot = 0;
 
61
  char c1prev, c2prev, c3prev;
 
62
  int score;
 
63
  score = 0;
 
64
  for (y = 0; y < height; y++)
 
65
    {
 
66
      for (x = 0; x < width; x++)
 
67
        {
 
68
 
 
69
          c1 = (char) *(pFrame->data[0] + y * pFrame->linesize[0] + x * 3);
 
70
          c2 = (char) *(pFrame->data[0] + y * pFrame->linesize[0] + x * 3 + 1);
 
71
          c3 = (char) *(pFrame->data[0] + y * pFrame->linesize[0] + x * 3 + 2);
 
72
 
 
73
          c1prev = (char) *(pFramePrev->data[0] + y * pFramePrev->linesize[0] + x * 3);
 
74
          c2prev = (char) *(pFramePrev->data[0] + y * pFramePrev->linesize[0] + x * 3 + 1);
 
75
          c3prev = (char) *(pFramePrev->data[0] + y * pFramePrev->linesize[0] + x * 3 + 2);
 
76
          c1tot += int (c1 + 127);
 
77
          c2tot += int (c2 + 127);
 
78
          c3tot += int (c3 + 127);
 
79
          score += abs ((c1 - c1prev));
 
80
          score += abs ((c2 - c2prev));
 
81
          score += abs ((c3 - c3prev));
 
82
        }
 
83
    }
 
84
  int nbpx = (height * width);
 
85
 
 
86
  /*
 
87
   * On se ramene à la moyenne 
 
88
   */
 
89
  score /= nbpx;
 
90
  c1tot /= nbpx;
 
91
  c2tot /= nbpx;
 
92
  c3tot /= nbpx;
 
93
 
 
94
 
 
95
  /*
 
96
   * Derivee numerique 
 
97
   */
 
98
  diff = abs (score - prev_score);
 
99
  prev_score = score;
 
100
 
 
101
  g->push_data (score, c1tot, c2tot, c3tot);
 
102
 
 
103
  if (diff > this->threshold && score > this->threshold)
 
104
    {
 
105
      shot s;
 
106
      s.fbegin = frame_number;
 
107
      s.msbegin = int ((frame_number * 1000) / fps);
 
108
      s.myid = shots.back ().myid + 1;
 
109
 
 
110
#ifdef DEBUG
 
111
      cerr << "Shot log :: " << s.msbegin << endl;
 
112
#endif
 
113
 
 
114
      /*
 
115
       * Convert to ms 
 
116
       */
 
117
      shots.back ().fduration = frame_number - shots.back ().fbegin;
 
118
      shots.back ().msduration = int (((shots.back ().fduration) * 1000) / fps);
 
119
 
 
120
      /*
 
121
       * Create images if necessary 
 
122
       */
 
123
      if (this->first_img_set)
 
124
        {
 
125
          image *im_begin = new image (this, width, height, s.myid, BEGIN);
 
126
          im_begin->SaveFrame (pFrame);
 
127
          s.img_begin = im_begin;
 
128
        }
 
129
      if (this->last_img_set)
 
130
        {
 
131
          image *im_end = new image (this, width, height, s.myid - 1, END);
 
132
          im_end->SaveFrame (pFramePrev);
 
133
          shots.back ().img_end = im_end;
 
134
        }
 
135
      shots.push_back (s);
 
136
    }
 
137
}
 
138
 
 
139
void
 
140
film::update_metadata ()
 
141
{
 
142
  char buf[256];
 
143
  /* Video metadata */
 
144
  if (videoStream != -1)
 
145
    {
 
146
      this->height = int (pFormatCtx->streams[videoStream]->codec->height);
 
147
      this->width = int (pFormatCtx->streams[videoStream]->codec->width);
 
148
      this->fps = av_q2d (pFormatCtx->streams[videoStream]->r_frame_rate);
 
149
      avcodec_string (buf, sizeof (buf), pFormatCtx->streams[videoStream]->codec, 0);
 
150
      this->codec.video = buf;
 
151
    }
 
152
  else
 
153
    {
 
154
      this->codec.video = "null";
 
155
      this->height = 0;
 
156
      this->width = 0;
 
157
      this->fps = 0;
 
158
    }
 
159
 
 
160
  /* Audio metadata */
 
161
  if (audioStream != -1)
 
162
    {
 
163
      avcodec_string (buf, sizeof (buf), pCodecCtxAudio, 0);
 
164
      this->codec.audio = buf;
 
165
      this->nchannel = pCodecCtxAudio->channels;
 
166
      this->samplerate = pCodecCtxAudio->sample_rate;
 
167
    }
 
168
  else
 
169
    {
 
170
      this->codec.audio = "null";
 
171
      this->nchannel = 0;
 
172
      this->samplerate = 0;
 
173
    }
 
174
 
 
175
  duration.secs = pFormatCtx->duration / AV_TIME_BASE;
 
176
  duration.us = pFormatCtx->duration % AV_TIME_BASE;
 
177
  duration.mstotal = int (duration.secs * 1000 + duration.us / 1000);
 
178
  duration.mins = duration.secs / 60;
 
179
  duration.secs %= 60;
 
180
  duration.hours = duration.mins / 60;
 
181
  duration.mins %= 60;
 
182
 
 
183
}
 
184
 
 
185
void
 
186
film::shotlog (string message)
 
187
{
 
188
#ifdef DEBUG
 
189
      cerr << "Shot log :: " << message << endl;
 
190
#endif
 
191
}
 
192
 
 
193
int
 
194
film::process ()
 
195
{
 
196
  int audioSize;
 
197
  uint8_t *buffer;
 
198
  uint8_t *buffer2;
 
199
  int frameFinished;
 
200
  int numBytes;
 
201
  shot s;
 
202
  static struct SwsContext *img_convert_ctx = NULL;
 
203
 
 
204
  create_main_dir ();
 
205
 
 
206
  string graphpath = this->global_path + "/";
 
207
  g = new graph (600, 400, graphpath, threshold, this);
 
208
  g->set_title ("Motion quantity");
 
209
 
 
210
  /*
 
211
   * Register all formats and codecs 
 
212
   */
 
213
  av_register_all ();
 
214
 
 
215
  if (av_open_input_file (&pFormatCtx, input_path.c_str (), NULL, 0, NULL) != 0)
 
216
    {
 
217
      string error_msg = "Impossible to open file";
 
218
      error_msg += input_path;
 
219
      shotlog (error_msg);
 
220
      return -1;                // Couldn't open file
 
221
    }
 
222
 
 
223
  /*
 
224
   * Retrieve stream information 
 
225
   */
 
226
  if (av_find_stream_info (pFormatCtx) < 0)
 
227
    return -1;                  // Couldn't find stream information
 
228
 
 
229
 
 
230
  // dump_format (pFormatCtx, 0, path.c_str (), false);
 
231
  videoStream = -1;
 
232
  audioStream = -1;
 
233
 
 
234
 
 
235
  /*
 
236
   * Detect streams types 
 
237
   */
 
238
  for (int j = 0; j < pFormatCtx->nb_streams; j++)
 
239
    {
 
240
      switch (pFormatCtx->streams[j]->codec->codec_type)
 
241
        {
 
242
        case CODEC_TYPE_VIDEO:
 
243
          videoStream = j;
 
244
          break;
 
245
 
 
246
        case CODEC_TYPE_AUDIO:
 
247
          audioStream = j;
 
248
          break;
 
249
 
 
250
        default:
 
251
          break;
 
252
        }
 
253
    }
 
254
 
 
255
 
 
256
 
 
257
  /*
 
258
   * Get a pointer to the codec context for the video stream 
 
259
   */
 
260
  if (audioStream != -1)
 
261
    {
 
262
          if (audio_set)
 
263
    {
 
264
      string xml_audio = graphpath + "/" + "audio.xml";
 
265
      init_xml (xml_audio);
 
266
    }
 
267
 
 
268
      pCodecCtxAudio = pFormatCtx->streams[audioStream]->codec;
 
269
      pCodecAudio = avcodec_find_decoder (pCodecCtxAudio->codec_id);
 
270
 
 
271
      if (pCodecAudio == NULL)
 
272
        return -1;              // Codec not found
 
273
      if (avcodec_open (pCodecCtxAudio, pCodecAudio) < 0)
 
274
        return -1;              // Could not open codec
 
275
 
 
276
    }
 
277
  update_metadata ();
 
278
  /*
 
279
   * Find the decoder for the video stream 
 
280
   */
 
281
  if (videoStream != -1)
 
282
    {
 
283
      pCodecCtx = pFormatCtx->streams[videoStream]->codec;
 
284
      pCodec = avcodec_find_decoder (pCodecCtx->codec_id);
 
285
 
 
286
      if (pCodec == NULL)
 
287
        return -1;              // Codec not found
 
288
      if (avcodec_open (pCodecCtx, pCodec) < 0)
 
289
        return -1;              // Could not open codec
 
290
 
 
291
      /*
 
292
       * Allocate video frame 
 
293
       */
 
294
      pFrame = avcodec_alloc_frame ();
 
295
      pFrameRGB = avcodec_alloc_frame ();
 
296
      pFrameRGBprev = avcodec_alloc_frame ();
 
297
 
 
298
      /*
 
299
       * Determine required buffer size and allocate buffer 
 
300
       */
 
301
      numBytes = avpicture_get_size (PIX_FMT_RGB24, width, height);
 
302
 
 
303
      buffer = (uint8_t *) malloc (sizeof (uint8_t) * numBytes);
 
304
      buffer2 = (uint8_t *) malloc (sizeof (uint8_t) * numBytes);
 
305
 
 
306
      /*
 
307
       * Assign appropriate parts of buffer to image planes in pFrameRGB 
 
308
       */
 
309
      avpicture_fill ((AVPicture *) pFrameRGB, buffer, PIX_FMT_RGB24, width, height);
 
310
 
 
311
      avpicture_fill ((AVPicture *) pFrameRGBprev, buffer2, PIX_FMT_RGB24, width, height);
 
312
 
 
313
 
 
314
      /*
 
315
       * Mise en place du premier plan 
 
316
       */
 
317
      s.fbegin = 0;
 
318
      s.msbegin = 0;
 
319
      s.myid = 0;
 
320
      shots.push_back (s);
 
321
 
 
322
 
 
323
 
 
324
    }
 
325
 
 
326
 
 
327
  checknumber = (samplerate * samplearg) / 1000;
 
328
 
 
329
  /*
 
330
   * Boucle de traitement principale du flux 
 
331
   */
 
332
  this->frame_number = 0;
 
333
  while (av_read_frame (pFormatCtx, &packet) >= 0)
 
334
    {
 
335
      if (packet.stream_index == videoStream)
 
336
        {
 
337
          avcodec_decode_video (pCodecCtx, pFrame, &frameFinished, packet.data, packet.size);
 
338
 
 
339
        if (frameFinished)
 
340
            {
 
341
              // Convert the image into RGB24
 
342
              if (! img_convert_ctx)
 
343
                {
 
344
                  img_convert_ctx = sws_getContext(width, height, pCodecCtx->pix_fmt,
 
345
                                                   width, height, PIX_FMT_RGB24, SWS_BICUBIC, 
 
346
                                                   NULL, NULL, NULL);
 
347
                  if (! img_convert_ctx) 
 
348
                  {
 
349
                    fprintf(stderr, "Cannot initialize the conversion context!\n");
 
350
                    exit(1);
 
351
                  }
 
352
                }
 
353
              
 
354
              /* API: int sws_scale(SwsContext *c, uint8_t *src, int srcStride[], int srcSliceY, int srcSliceH, uint8_t dst[], int dstStride[] )
 
355
               */
 
356
              sws_scale(img_convert_ctx, pFrame->data, 
 
357
                        pFrame->linesize, 0, 
 
358
                        pCodecCtx->height,
 
359
                        pFrameRGB->data, pFrameRGB->linesize);
 
360
 
 
361
              /*
 
362
                Old API doc (cf http://www.dranger.com/ffmpeg/functions.html )
 
363
                int img_convert(AVPicture *dst, int dst_pix_fmt,
 
364
                                const AVPicture *src, int src_pix_fmt,
 
365
                                int src_width, int src_height)
 
366
              */
 
367
              /*
 
368
              img_convert ((AVPicture *) pFrameRGB, PIX_FMT_RGB24, (AVPicture *) pFrame, pCodecCtx->pix_fmt, width, height);
 
369
              */
 
370
 
 
371
            this->frame_number ++;
 
372
              /* Si ce n'est pas la permiere image */
 
373
        if ( this->frame_number  > 2)
 
374
                {
 
375
                  CompareFrame (pFrameRGB, pFrameRGBprev);
 
376
                }
 
377
              else
 
378
                {
 
379
                  /*
 
380
                   * Cas ou c'est la premiere image, on cree la premiere image dans tous les cas 
 
381
                   */
 
382
                  image *begin_i = new image (this, width, height, s.myid, BEGIN);
 
383
                  begin_i->create_img_dir ();
 
384
                  begin_i->SaveFrame (pFrameRGB);
 
385
                  shots.back ().img_begin = begin_i;
 
386
                }
 
387
             memcpy (buffer2, buffer, numBytes);
 
388
            }
 
389
        }
 
390
      if (audio_set && (packet.stream_index == audioStream))
 
391
        {
 
392
          process_audio ();
 
393
        }
 
394
      /*
 
395
       * Free the packet that was allocated by av_read_frame 
 
396
       */
 
397
      if (packet.data != NULL)
 
398
        av_free_packet (&packet);
 
399
    }
 
400
 
 
401
  if (videoStream != -1)
 
402
    {
 
403
      /* Mise en place de la dernière image */
 
404
      int lastFrame = this->frame_number;
 
405
      shots.back ().fduration = lastFrame - shots.back ().fbegin;
 
406
      shots.back ().msduration = int (((shots.back ().fduration) * 1000) / fps);
 
407
      duration.mstotal = int (shots.back ().msduration + shots.back ().msbegin);
 
408
          image *end_i = new image (this, width, height, shots.back ().myid, END);
 
409
          end_i->SaveFrame (pFrameRGB);
 
410
          shots.back ().img_end = end_i;
 
411
 
 
412
      /*
 
413
       * Graphe de la qté de mvmt 
 
414
       */
 
415
      g->init_gd ();
 
416
      g->draw_all_canvas ();
 
417
      g->draw_color_datas ();
 
418
      g->draw_datas ();
 
419
      if (video_set)
 
420
        {
 
421
          string xml_color = graphpath + "/" + "video.xml";
 
422
          g->write_xml (xml_color);
 
423
        }
 
424
      g->save ();
 
425
 
 
426
      /*
 
427
       * Free the RGB images 
 
428
       */
 
429
      free (buffer);
 
430
      free (buffer2);
 
431
      av_free (pFrameRGB);
 
432
      av_free (pFrame);
 
433
      av_free (pFrameRGBprev);
 
434
      avcodec_close (pCodecCtx);
 
435
    }
 
436
  /*
 
437
   * Close the codec 
 
438
   */
 
439
  if (audioStream != -1)
 
440
    {
 
441
        /* Fermetrure du fichier xml */
 
442
      if (audio_set) close_xml ();
 
443
      avcodec_close (pCodecCtxAudio);
 
444
    }
 
445
 
 
446
  /*
 
447
   * Close the video file 
 
448
   */
 
449
  av_close_input_file (pFormatCtx);
 
450
 
 
451
 
 
452
}
 
453
 
 
454
void
 
455
film::init_xml (string filename)
 
456
{
 
457
  fd_xml_audio = fopen (filename.c_str (), "w");
 
458
  fprintf (fd_xml_audio, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<iri>\n<sound sampling=\"%d\" nchannels=\"%d\">", samplearg, nchannel);
 
459
}
 
460
 
 
461
int
 
462
film::close_xml ()
 
463
{
 
464
  fprintf (fd_xml_audio, "</sound>\n</iri>");
 
465
  return (fclose (fd_xml_audio));
 
466
}
 
467
 
 
468
 
 
469
void
 
470
film::process_audio ()
 
471
{
 
472
  int len1;
 
473
  int len;
 
474
  int data_size;
 
475
  static unsigned int samples_size = 0;
 
476
  int i;
 
477
  uint8_t *ptr;
 
478
 
 
479
  ptr = packet.data;
 
480
  len = packet.size;
 
481
 
 
482
  while (len > 0)
 
483
    {
 
484
      this->audio_buf = (short *) av_fast_realloc (this->audio_buf, &samples_size, FFMAX (packet.size, AVCODEC_MAX_AUDIO_FRAME_SIZE));
 
485
      data_size = samples_size;
 
486
      len1 = avcodec_decode_audio2 (pCodecCtxAudio, audio_buf, &data_size, ptr, len);
 
487
 
 
488
 
 
489
      if (len1 < 0)
 
490
        {
 
491
          /*
 
492
           * Error, breaking the frame 
 
493
           */
 
494
          len = 0;
 
495
          break;
 
496
        }
 
497
 
 
498
      len -= len1;
 
499
      ptr += len1;
 
500
 
 
501
      if (data_size > 0)
 
502
        {
 
503
 
 
504
          samples += data_size / (pCodecCtxAudio->channels * 2);
 
505
          for (i = 0; i < data_size; i += 2 * pCodecCtxAudio->channels)
 
506
            {
 
507
              sample_right = *((signed short int *) (audio_buf + i));
 
508
              /*
 
509
               * Si un seul canal, le sample droit = sample gauche 
 
510
               */
 
511
              if (pCodecCtxAudio->channels >= 1)
 
512
                sample_left = *((signed short int *) (audio_buf + i + 2));
 
513
              else
 
514
                sample_left = sample_right;
 
515
 
 
516
              /*
 
517
               * extraction des minimas et maximas 
 
518
               */
 
519
              if (minright > sample_right)
 
520
                minright = sample_right;
 
521
              if (maxright < sample_right)
 
522
                maxright = sample_right;
 
523
              if (minleft > sample_left)
 
524
                minleft = sample_left;
 
525
              if (maxleft < sample_left)
 
526
                maxleft = sample_left;
 
527
 
 
528
              /*
 
529
               * Get sample 
 
530
               */
 
531
              if (ech++ == checknumber)
 
532
                {
 
533
                  if (minright > minleft)
 
534
                    minright = minleft;
 
535
                  if (maxright > maxleft)
 
536
                    maxright = maxleft;
 
537
                  fprintf (fd_xml_audio, "<v c1d =\"%d\" c1u=\"%d\" />\n", minright / RATIO, maxright / RATIO);
 
538
                  minright = MAX_INT;
 
539
                  maxright = MIN_INT;
 
540
                  minleft = MAX_INT;
 
541
                  maxleft = MIN_INT;
 
542
 
 
543
                  /*
 
544
                   * Reset sample number 
 
545
                   */
 
546
                  ech = 0;
 
547
                }
 
548
            }
 
549
        }
 
550
    }
 
551
}
 
552
 
 
553
film::film()
 
554
{
 
555
  samplearg = 1000;
 
556
  samples = 0;
 
557
  minright = MAX_INT;
 
558
  maxright = MIN_INT;
 
559
  minleft = MAX_INT;
 
560
  maxleft = MIN_INT;
 
561
  ech = 0;
 
562
  nchannel = 1;
 
563
  audio_buf = NULL;
 
564
  this->first_img_set = false;
 
565
  this->last_img_set = false;
 
566
  this->audio_set = false;
 
567
  this->video_set = false;
 
568
  this->thumb_set = false;
 
569
  this->shot_set = false;
 
570
 
 
571
}