File: formattracker.c

package info (click to toggle)
gmerlin-avdecoder 2.0.0~svn6298~dfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,112 kB
  • sloc: ansic: 94,580; sh: 705; makefile: 705; awk: 43; sed: 16
file content (226 lines) | stat: -rw-r--r-- 5,676 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*****************************************************************
 * gmerlin-avdecoder - a general purpose multimedia decoding library
 *
 * Copyright (c) 2001 - 2012 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <config.h>

#include <stdlib.h>
#include <avdec_private.h>

#define TRACK_INTERLACING (1<<0)
#define TRACK_FRAMERATE   (1<<1)

struct bgav_video_format_tracker_s
  {
  bgav_stream_t * s;
  int do_track;
  
  bgav_packet_source_t src;

  bgav_packet_t * output_packet;
  int eof;
  
  gavl_framerate_mode_t fps_mode;
  int last_frame_duration;

  /* Interlacing flags */
  int have_top;
  int have_bottom;
  int have_progressive;
  };

static void set_eof(bgav_video_format_tracker_t * ft)
  {
  if(ft->eof)
    return;

  if(ft->do_track & TRACK_FRAMERATE)
    {
    ft->s->data.video.format->framerate_mode = ft->fps_mode;
    if(ft->fps_mode == GAVL_FRAMERATE_CONSTANT)
      ft->s->data.video.format->frame_duration = ft->last_frame_duration;
    }
  
  if(ft->do_track & TRACK_INTERLACING)
    {
    if(ft->have_progressive)
      {
      if(ft->have_top)
        {
        if(ft->have_bottom)
          {
          /* progressive + top + bottom  */
          ft->s->data.video.format->interlace_mode = GAVL_INTERLACE_MIXED;
          }
        else
          {
          /* progressive + top + !bottom  */
          ft->s->data.video.format->interlace_mode = GAVL_INTERLACE_MIXED_TOP;
          }
        }
      else /* !ft->have_top */
        {
        if(ft->have_bottom)
          {
          /* progressive + !top + bottom  */
          ft->s->data.video.format->interlace_mode = GAVL_INTERLACE_MIXED_BOTTOM;
          }
        else
          {
          /* progressive + !top + !bottom  */
          ft->s->data.video.format->interlace_mode = GAVL_INTERLACE_NONE;
          }
        }
      }
    else
      {
      if(ft->have_top)
        {
        if(ft->have_bottom)
          {
          /* !progressive + top + bottom  */
          ft->s->data.video.format->interlace_mode = GAVL_INTERLACE_MIXED;
          }
        else
          {
          /* !progressive + top + !bottom  */
          ft->s->data.video.format->interlace_mode = GAVL_INTERLACE_TOP_FIRST;
          }
        }
      else /* !ft->have_top */
        {
        if(ft->have_bottom)
          {
          /* !progressive + !top + bottom  */
          ft->s->data.video.format->interlace_mode = GAVL_INTERLACE_BOTTOM_FIRST;
          }
        else
          {
          /* !progressive + !top + !bottom  */
          ft->s->data.video.format->interlace_mode = GAVL_INTERLACE_MIXED;
          }
        }
      }
    }
  ft->do_track = 0;
  ft->eof = 1;
  }

static gavl_source_status_t
peek_func(void * ft1, bgav_packet_t ** ret, int force)
  {
  gavl_source_status_t st;
  bgav_video_format_tracker_t * ft = ft1;
  
  st = ft->src.peek_func(ft->src.data, ret, force);
  
  if(st == GAVL_SOURCE_EOF)
    set_eof(ft);
  return st;
  }

static gavl_source_status_t
get_func(void * ft1, bgav_packet_t ** ret)
  {
  gavl_source_status_t st;
  bgav_video_format_tracker_t * ft = ft1;

  st = ft->src.get_func(ft->src.data, ret);

  if(st == GAVL_SOURCE_EOF)
    {
    set_eof(ft);
    return st;
    }

  if(st != GAVL_SOURCE_OK)
    return st;
  
  /* Check what we have */
  
  if(ft->do_track & TRACK_FRAMERATE)
    {
    if((ft->last_frame_duration >= 0) &&
       ((*ret)->duration != ft->last_frame_duration))
      ft->fps_mode = GAVL_FRAMERATE_VARIABLE;
    ft->last_frame_duration = (*ret)->duration;
    }
  
  if(ft->do_track & TRACK_INTERLACING)
    {
    switch((*ret)->ilace)
      {
      case GAVL_INTERLACE_NONE:
        ft->have_progressive = 1;
        break;
      case GAVL_INTERLACE_TOP_FIRST:
        ft->have_top = 1;
        break;
      case GAVL_INTERLACE_BOTTOM_FIRST:
        ft->have_bottom = 1;
        break;
      default:
        break;
      }
    }
  return GAVL_SOURCE_OK;
  }

bgav_video_format_tracker_t *
bgav_video_format_tracker_create(bgav_stream_t * s)
  {
  bgav_video_format_tracker_t * ret;
  ret = calloc(1, sizeof(*ret));

  ret->s = s;

  bgav_packet_source_copy(&ret->src, &s->src);

  s->src.get_func = get_func;
  s->src.peek_func = peek_func;
  s->src.data = ret;
  
  switch(s->data.video.format->interlace_mode)
    {
    case GAVL_INTERLACE_MIXED:        /*!< Use interlace_mode of the frames */
    case GAVL_INTERLACE_MIXED_TOP:    /*!< Progressive + top    */
    case GAVL_INTERLACE_MIXED_BOTTOM: /*!< Progressive + bottom */
      ret->do_track |= TRACK_INTERLACING;
      break;
    default:
      break;
    }

  if(s->data.video.format->framerate_mode == GAVL_FRAMERATE_VARIABLE)
    {
    ret->do_track |= TRACK_FRAMERATE;
    ret->last_frame_duration = -1;
    ret->fps_mode = GAVL_FRAMERATE_CONSTANT;
    }

  

  return ret;
  }

void bgav_video_format_tracker_destroy(bgav_video_format_tracker_t *ft)
  {
  free(ft);
  }