File: parse_mpv.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 (479 lines) | stat: -rw-r--r-- 13,822 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*****************************************************************
 * 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 <string.h>
#include <stdlib.h>


#include <avdec_private.h>
#include <parser.h>
#include <videoparser_priv.h>

#include <mpv_header.h>

#define LOG_DOMAIN "parse_mpv"

/* States for finding the frame boundary */

#define STATE_SYNC                            100
#define STATE_SEQUENCE                        1
#define STATE_GOP                             2
#define STATE_PICTURE                         3
#define STATE_SLICE                           4

typedef struct
  {
  /* Sequence header */
  bgav_mpv_sequence_header_t sh;
  int have_sh;
  int state;
  
  int d10; // Special handling for D10 */
  
  int framerate_from_container;

  int have_header;
  
  } mpeg12_priv_t;

static void reset_mpeg12(bgav_video_parser_t * parser)
  {
  mpeg12_priv_t * priv = parser->priv;
  priv->state = STATE_SYNC;
  }

static int extract_header(bgav_video_parser_t * parser, bgav_packet_t * p,
                          const uint8_t * header_end)
  {
  mpeg12_priv_t * priv = parser->priv;
  
  if(!p->header_size)
    p->header_size = header_end - p->data;
  
  if(priv->have_header)
    return 1;

  if(!parser->s->ci.global_header)
    bgav_stream_set_extradata(parser->s, p->data, header_end - p->data);
  
  if(parser->s->fourcc == BGAV_MK_FOURCC('m','p','g','v'))
    {
    if(priv->sh.mpeg2)
      {
      parser->s->fourcc = BGAV_MK_FOURCC('m','p','v','2');
      gavl_dictionary_set_string_nocopy(parser->s->m, GAVL_META_FORMAT,
                              bgav_sprintf("MPEG-2"));
      }
    else
      {
      parser->s->fourcc = BGAV_MK_FOURCC('m','p','v','1');
      gavl_dictionary_set_string_nocopy(parser->s->m, GAVL_META_FORMAT,
                              bgav_sprintf("MPEG-1"));
      }
    }

  /* Set framerate */
  
  if(!parser->format->timescale)
    {
    bgav_mpv_get_framerate(priv->sh.frame_rate_index,
                           &parser->format->timescale,
                           &parser->format->frame_duration);
    
    if(priv->sh.mpeg2)
      {
      parser->format->timescale *= (priv->sh.ext.timescale_ext+1) * 2;
      parser->format->frame_duration *= (priv->sh.ext.frame_duration_ext+1) * 2;
      parser->format->framerate_mode = GAVL_FRAMERATE_VARIABLE;
      }
    }

  /* Set picture size */
  
  if(!parser->format->image_width)
    bgav_mpv_get_size(&priv->sh, parser->format);
  
  /* Special handling for D10 */
  if(priv->d10)
    {
    if(parser->format->image_height == 608)
      parser->format->image_height = 576;
    else if(parser->format->image_height == 512)
      parser->format->image_height = 486;
    }
  
  /* Set pixel size */
  bgav_mpv_get_pixel_aspect(&priv->sh, parser->format);
  
  /* Pixelformat */
  if(parser->format->pixelformat == GAVL_PIXELFORMAT_NONE)
    parser->format->pixelformat = bgav_mpv_get_pixelformat(&priv->sh);

  /* Other stuff */
  if(priv->sh.mpeg2 && priv->sh.ext.low_delay)
    parser->s->ci.flags &= ~GAVL_COMPRESSION_HAS_B_FRAMES;

  /* Bitrate */
  parser->s->codec_bitrate = priv->sh.bitrate * 400;
  if(priv->sh.mpeg2)
    parser->s->codec_bitrate += (priv->sh.ext.bitrate_ext << 18) * 400;

  /* VBV buffer size */
  parser->s->ci.video_buffer_size = priv->sh.vbv_buffer_size_value;
  if(priv->sh.mpeg2)
    parser->s->ci.video_buffer_size +=
      (priv->sh.ext.vbv_buffer_size_ext<<10);

  parser->s->ci.video_buffer_size *= (1024 * 16);
  parser->s->ci.video_buffer_size /= 8; // bits -> bytes
  
  priv->have_header = 1;
  return 1;
  }

static int parse_frame_mpeg12(bgav_video_parser_t * parser, bgav_packet_t * p,
                              int64_t pts_orig)
  {
  const uint8_t * sc;
  mpeg12_priv_t * priv = parser->priv;
  //  cache_t * c;
  bgav_mpv_picture_extension_t pe;
  bgav_mpv_picture_header_t    ph;
  int start_code;
  int len;
  int delta_d;
  int got_sh = 0;
  int ret = 0;
  
  const uint8_t * start =   p->data;
  const uint8_t * end = p->data + p->data_size;
  
  /* Check for sequence end code within this frame */

  if(p->data_size >= 4)
    {
    end = p->data + (p->data_size - 4);
    if(GAVL_PTR_2_32BE(end) == 0x000001B7)
      p->sequence_end_pos = p->data_size - 4;
    }

  
  end = p->data + p->data_size;
  
  while(1)
    {
    sc = bgav_mpv_find_startcode(start, end);
    if(!sc)
      return ret;
    
    start_code = bgav_mpv_get_start_code(sc, 1);

    /* Update position */
    start = sc;
    
    switch(start_code)
      {
      case MPEG_CODE_SEQUENCE:
        if(!priv->have_sh)
          {
          len = bgav_mpv_sequence_header_parse(parser->s->opt,
                                               &priv->sh,
                                               start, end - start);
          if(!len)
            return 0;

          /* Sequence header and sequence end in one packet means
             still images */
          if(p->sequence_end_pos)
            {
            if(!STREAM_IS_STILL(parser->s))
              {
              gavl_log(GAVL_LOG_INFO, LOG_DOMAIN,
                       "Detected still image");
              STREAM_SET_STILL(parser->s);
              }
            }
          
          priv->have_sh = 1;
          start += len;
          }
        else
          start += 4;
        got_sh = 1;

        break;
      case MPEG_CODE_SEQUENCE_EXT:
        if(priv->have_sh && !priv->sh.mpeg2)
          {
          len =
            bgav_mpv_sequence_extension_parse(parser->s->opt,
                                              &priv->sh.ext,
                                              start, end - start);
          if(!len)
            return 0;
          priv->sh.mpeg2 = 1;
          start += len;
          }
        else
          start += 4;
        break;
      case MPEG_CODE_PICTURE:
        if(!priv->have_sh)
          PACKET_SET_SKIP(p);
        else if(got_sh && !extract_header(parser, p, sc))
          return 0;
        
        len = bgav_mpv_picture_header_parse(parser->s->opt,
                                            &ph, start, end - start);
        
        if(parser->format->framerate_mode == GAVL_FRAMERATE_STILL)
          {
          parser->timestamp = gavl_time_rescale(parser->s->timescale,
                                                parser->format->timescale,
                                                pts_orig);
          p->duration = -1;
          }
        else
          p->duration = parser->format->frame_duration;
          
        if(!len)
          return PARSER_ERROR;

        PACKET_SET_CODING_TYPE(p, ph.coding_type);
        
        if(got_sh)
          {
          if(!(parser->flags & PARSER_NO_I_FRAMES) &&
             (ph.coding_type == BGAV_CODING_TYPE_P))
            {
            parser->flags |= PARSER_NO_I_FRAMES;
            gavl_log(GAVL_LOG_DEBUG, LOG_DOMAIN,
                     "Detected intra slice refresh");
            }
          }
        
        start += len;
        
        if(!priv->sh.mpeg2)
          return 1;
        break;
      case MPEG_CODE_PICTURE_EXT:
        len = bgav_mpv_picture_extension_parse(parser->s->opt,
                                               &pe, start, end - start);
        if(!len)
          return PARSER_ERROR;

        /* Set interlacing stuff */
        switch(pe.picture_structure)
          {
          case MPEG_PICTURE_TOP_FIELD:
            PACKET_SET_FIELD_PIC(p);
            p->ilace = GAVL_INTERLACE_TOP_FIRST;
            break;
          case MPEG_PICTURE_BOTTOM_FIELD:
            PACKET_SET_FIELD_PIC(p);
            p->ilace = GAVL_INTERLACE_BOTTOM_FIRST;
            break;
          case MPEG_PICTURE_FRAME:

            if(p->duration > 0)
              {
              if(pe.repeat_first_field)
                {
                delta_d = 0;
                if(priv->sh.ext.progressive_sequence)
                  {
                  if(pe.top_field_first)
                    delta_d = parser->format->frame_duration * 2;
                  else
                    delta_d = parser->format->frame_duration;
                  }
                else if(pe.progressive_frame)
                  delta_d = parser->format->frame_duration / 2;
              
                p->duration += delta_d;
                }
              }
            
            if(!pe.repeat_first_field && !priv->sh.ext.progressive_sequence)
              {
              if(pe.progressive_frame)
                p->ilace = GAVL_INTERLACE_NONE;
              else if(pe.top_field_first)
                p->ilace = GAVL_INTERLACE_TOP_FIRST;
              else
                p->ilace = GAVL_INTERLACE_BOTTOM_FIRST;
              }
            break;
          }
        // start += len;
        return 1;
        break;
      case MPEG_CODE_GOP:
        {
        bgav_mpv_gop_header_t        gh;

        if(got_sh && !extract_header(parser, p, sc))
          return 0;
        
        len = bgav_mpv_gop_header_parse(parser->s->opt,
                                        &gh, start, end - start);
        
        if(!len)
          return PARSER_ERROR;
        
        start += len;

        if(!parser->format->timecode_format.int_framerate && parser->format->frame_duration)
          {
          parser->format->timecode_format.int_framerate =
            parser->format->timescale /
            parser->format->frame_duration;
          if(gh.drop)
            parser->format->timecode_format.flags |=
              GAVL_TIMECODE_DROP_FRAME;
          }

        if(parser->format->timecode_format.int_framerate)
          {
          gavl_timecode_from_hmsf(&p->tc,
                                  gh.hours,
                                  gh.minutes,
                                  gh.seconds,
                                  gh.frames);
          }
        }
        break;
      case MPEG_CODE_SLICE:
        return 1;
      default:
        start += 4;
      }
    }
  }

static int find_frame_boundary_mpeg12(bgav_video_parser_t * parser, int * skip)

  {
  const uint8_t * sc;
  int start_code;
  mpeg12_priv_t * priv = parser->priv;
  int new_state;
  
  while(1)
    {
    sc = bgav_mpv_find_startcode(parser->buf.buffer + parser->pos,
                                 parser->buf.buffer + parser->buf.size - 1);
    if(!sc)
      {
      parser->pos = parser->buf.size - 3;
      if(parser->pos < 0)
        parser->pos = 0;
      return 0;
      }

    start_code = bgav_mpv_get_start_code(sc, 0);

    new_state = -1;
    switch(start_code)
      {
      case MPEG_CODE_SEQUENCE:
        /* Sequence header */
        new_state = STATE_SEQUENCE;
        break;
      case MPEG_CODE_PICTURE:
        new_state = STATE_PICTURE;
        break;
      case MPEG_CODE_GOP:
        new_state = STATE_GOP;
        break;
      case MPEG_CODE_SLICE:
        new_state = STATE_SLICE;
        break;
      case MPEG_CODE_END:
        //        fprintf(stderr, "Got sequence end\n");
        /* Sequence end is always a picture start */
        parser->pos = (sc - parser->buf.buffer) + 4;
        *skip = 4;
        priv->state = STATE_SEQUENCE;
        return 1;
        break;
      case MPEG_CODE_EXTENSION:
        break;
      }

    parser->pos = sc - parser->buf.buffer;
    
    if(new_state < 0)
      parser->pos += 4;
    else if(((new_state <= STATE_PICTURE) && (new_state < priv->state)) ||
            ((priv->state == STATE_SYNC) && (new_state >=  STATE_PICTURE)))
      {
      *skip = 4;
      parser->pos = sc - parser->buf.buffer;
      priv->state = new_state;
      return 1;
      }
    else
      {
      parser->pos += 4;
      priv->state = new_state;
      }
    }
  return 0;
  }

static void cleanup_mpeg12(bgav_video_parser_t * parser)
  {
  free(parser->priv);
  }

void bgav_video_parser_init_mpeg12(bgav_video_parser_t * parser)
  {
  mpeg12_priv_t * priv;
  priv = calloc(1, sizeof(*priv));
  parser->priv        = priv;
  priv->state = STATE_SYNC;
  //  parser->parse       = parse_mpeg12;
  parser->parse_frame = parse_frame_mpeg12;
  parser->cleanup     = cleanup_mpeg12;
  parser->reset       = reset_mpeg12;
  parser->find_frame_boundary = find_frame_boundary_mpeg12;

  parser->s->ci.flags |= GAVL_COMPRESSION_HAS_B_FRAMES;
  
  if((parser->s->fourcc == BGAV_MK_FOURCC('m', 'x', '5', 'p')) ||
     (parser->s->fourcc == BGAV_MK_FOURCC('m', 'x', '4', 'p')) ||
     (parser->s->fourcc == BGAV_MK_FOURCC('m', 'x', '3', 'p')) ||
     (parser->s->fourcc == BGAV_MK_FOURCC('m', 'x', '5', 'n')) ||
     (parser->s->fourcc == BGAV_MK_FOURCC('m', 'x', '4', 'n')) ||
     (parser->s->fourcc == BGAV_MK_FOURCC('m', 'x', '3', 'n')))
    {
    parser->s->codec_bitrate =
      (((parser->s->fourcc & 0x0000FF00) >> 8) - '0') * 10000000;
    priv->d10 = 1;
    parser->s->ci.flags &= ~(GAVL_COMPRESSION_HAS_P_FRAMES|
                             GAVL_COMPRESSION_HAS_B_FRAMES);
    parser->format->interlace_mode = GAVL_INTERLACE_TOP_FIRST;
    }
  
  }