File: parse_cavs.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 (218 lines) | stat: -rw-r--r-- 6,023 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
/*****************************************************************
 * 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 <cavs_header.h>
#include <mpv_header.h>

#define CAVS_NEED_SYNC                   0
#define CAVS_NEED_STARTCODE              1
#define CAVS_HAS_SEQ_CODE                2
#define CAVS_HAS_SEQ_HEADER              3
#define CAVS_HAS_PIC_CODE                4
#define CAVS_HAS_PIC_HEADER              5

#define STATE_SYNC                       100 // Need start code
#define STATE_SEQUENCE                   1 // Got sequence header
#define STATE_PICTURE                    2 // Got picture header

typedef struct
  {
  /* Sequence header */
  bgav_cavs_sequence_header_t seq;
  int have_seq;
  int has_picture_start;
  int state;
  } cavs_priv_t;

static void reset_cavs(bgav_video_parser_t * parser)
  {
  cavs_priv_t * priv = parser->priv;
  priv->state = STATE_SYNC;
  priv->has_picture_start = 0;
  }

static int parse_frame_cavs(bgav_video_parser_t * parser, bgav_packet_t * p,
                            int64_t pts_orig)
  {
  int start_code;
  int len;
  bgav_cavs_picture_header_t ph;
  const uint8_t * sc;
  const uint8_t * ptr = p->data;
  const uint8_t * end = p->data + p->data_size;
  cavs_priv_t * priv = parser->priv;
  
  while(1)
    {
    sc = bgav_mpv_find_startcode(ptr, end);
    if(!sc)
      return 0;
    
    start_code = bgav_cavs_get_start_code(sc);
    ptr = sc;
    //    fprintf(stderr, "Start code: %02x\n", sc[3]);
    //    gavl_hexdump(sc, 16, 16);
    switch(start_code)
      {
      case CAVS_CODE_SEQUENCE:
        if(!priv->have_seq)
          {
          len = bgav_cavs_sequence_header_read(parser->s->opt,
                                               &priv->seq,
                                               ptr, end - ptr);
          if(!len)
            return 0;
          
#ifdef DUMP_HEADERS
          bgav_cavs_sequence_header_dump(&priv->seq);
#endif

          ptr += len;
          
          bgav_mpv_get_framerate(priv->seq.frame_rate_code,
                                 &parser->format->timescale,
                                 &parser->format->frame_duration);
          
          parser->format->image_width  = priv->seq.horizontal_size;
          parser->format->image_height = priv->seq.vertical_size;
          parser->format->frame_width  =
            (parser->format->image_width + 15) & ~15;
          parser->format->frame_height  =
            (parser->format->image_height + 15) & ~15;
      
          priv->have_seq = 1;
          }
        else
          ptr += 4;
        break;
      case CAVS_CODE_PICTURE_I:
      case CAVS_CODE_PICTURE_PB:
        if(!priv->have_seq)
          {
          PACKET_SET_SKIP(p);
          ptr += 4;
          return 1;
          }
        
        len = bgav_cavs_picture_header_read(parser->s->opt,
                                            &ph, ptr, end - ptr, &priv->seq);

        if(!len)
          return 0;
#ifdef DUMP_HEADERS
        bgav_cavs_picture_header_dump(&ph, &priv->seq);
#endif
        PACKET_SET_CODING_TYPE(p, ph.coding_type);
        p->duration = parser->format->frame_duration;
        return 1;
        break;
      default:
        ptr += 4;
        break;
      }
    }
  return 0;
  }

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

    new_state = -1;

    start_code = bgav_cavs_get_start_code(sc);
        
    switch(start_code)
      {
      case CAVS_CODE_SEQUENCE:
        /* Sequence header */
        new_state = STATE_SEQUENCE;
        break;
      case CAVS_CODE_PICTURE_I:
      case CAVS_CODE_PICTURE_PB:
        new_state = STATE_PICTURE;
        break;
      }
    
    parser->pos = sc - parser->buf.buffer;
    
    if(new_state < 0)
      parser->pos += 4;
    else if(((new_state == STATE_PICTURE) && (priv->state == STATE_PICTURE)) ||
            (new_state < priv->state))
      {
      *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_cavs(bgav_video_parser_t * parser)
  {
  free(parser->priv);
  }

void bgav_video_parser_init_cavs(bgav_video_parser_t * parser)
  {
  cavs_priv_t * priv;
  priv = calloc(1, sizeof(*priv));

  priv->state = STATE_SYNC;
  parser->priv = priv;
  //  parser->parse = parse_cavs;
  parser->parse_frame = parse_frame_cavs;

  parser->cleanup = cleanup_cavs;
  parser->reset = reset_cavs;
  parser->find_frame_boundary = find_frame_boundary_cavs;
  }