summaryrefslogtreecommitdiff
path: root/src/libmpeg2new/xine_decoder.c
blob: 2ee805d6199c7d6517056644a397c6980f1b31eb (plain)
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
/* 
 * Copyright (C) 2000-2002 the xine project
 * 
 * This file is part of xine, a free video player.
 * 
 * xine 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.
 * 
 * xine 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 * $Id: xine_decoder.c,v 1.7 2003/06/14 02:31:01 jcdutton Exp $
 *
 * stuff needed to turn libmpeg2 into a xine decoder plugin
 */


#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <assert.h>

#include "./include/mpeg2.h"
#include "xine_internal.h"
#include "video_out.h"
#include "buffer.h"

/*
#define LOG
*/
/*
#define LOG_FRAME_ALLOC_FREE
*/

typedef struct {
  video_decoder_class_t   decoder_class;
} mpeg2_class_t;


typedef struct mpeg2_video_decoder_s {
  video_decoder_t  video_decoder;
  mpeg2dec_t      *mpeg2dec;
  mpeg2_class_t   *class;
  xine_stream_t   *stream;
  int32_t         force_aspect;
  int32_t         aspect_ratio;
  int32_t         aspect_ratio_float;
  uint32_t        img_state[30];
  uint32_t	  frame_number;
  uint32_t        rff_pattern;
  
} mpeg2_video_decoder_t;


static void mpeg2_video_print_bad_state(uint32_t * img_state) {
  int32_t n,m;
  m=0;
  for(n=0;n<30;n++) {
    if (img_state[n]>0) {
      printf("%d = %u\n",n, img_state[n]);
      m++;
    }
  }
  if (m > 3) assert(0);
  if (m == 0) printf("NO FRAMES\n");
} 

static void mpeg2_video_print_fbuf(const mpeg2_fbuf_t * fbuf) {
  printf("%p",fbuf);
  vo_frame_t * img;
  if (fbuf) {
    img = (vo_frame_t *) fbuf->id;
    if (img) {
      printf (", img=%p, (id=%d)\n",
             img, img->id);
    } else {
      printf (", img=NULL\n");
    }
  } else {
    printf ("\n");
  }
}

static void mpeg2_video_decode_data (video_decoder_t *this_gen, buf_element_t *buf_element) {
  mpeg2_video_decoder_t *this = (mpeg2_video_decoder_t *) this_gen;
  uint8_t * current = buf_element->content;
  uint8_t * end = buf_element->content + buf_element->size;
  const mpeg2_info_t * info;
  mpeg2_state_t state;
  vo_frame_t * img;
  uint32_t picture_structure;
  int32_t frame_skipping;

  /* handle aspect hints from xine-dvdnav */
  if (buf_element->decoder_flags & BUF_FLAG_SPECIAL) {
    if (buf_element->decoder_info[1] == BUF_SPECIAL_ASPECT) {
      this->force_aspect = buf_element->decoder_info[2];
      if (buf_element->decoder_info[3] == 0x1 && buf_element->decoder_info[2] == XINE_VO_ASPECT_ANAMORPHIC)
        /* letterboxing is denied, we have to do pan&scan */
        this->force_aspect = XINE_VO_ASPECT_PAN_SCAN;
    }
    
    return;
  }

  if (buf_element->decoder_flags != 0) return;

  mpeg2_buffer (this->mpeg2dec, current, end);

  info = mpeg2_info (this->mpeg2dec);
  while ((state = mpeg2_parse (this->mpeg2dec)) != STATE_BUFFER) {
#if 0
    printf("libmpeg2:decode_data:current_fbuf=");
    mpeg2_video_print_fbuf(info->current_fbuf);
    printf("libmpeg2:decode_data:display_fbuf=");
    mpeg2_video_print_fbuf(info->display_fbuf);
    printf("libmpeg2:decode_data:discard_fbuf=");
    mpeg2_video_print_fbuf(info->discard_fbuf);
#endif 
    switch (state) {
      case STATE_SEQUENCE:
        /* might set nb fbuf, convert format, stride */
        /* might set fbufs */
        this->stream->stream_info[XINE_STREAM_INFO_VIDEO_WIDTH]     = info->sequence->picture_width;
        this->stream->stream_info[XINE_STREAM_INFO_VIDEO_HEIGHT]    = info->sequence->picture_height;
        this->stream->stream_info[XINE_STREAM_INFO_FRAME_DURATION]  = info->sequence->frame_period / 300;
        if (this->force_aspect > 0) {
          this->aspect_ratio = this->force_aspect;
          switch (info->sequence->pixel_width) {
            case XINE_VO_ASPECT_PAN_SCAN:
            case XINE_VO_ASPECT_ANAMORPHIC:
              this->aspect_ratio_float = 10000 * 16.0 /9.0;
              break;
            case XINE_VO_ASPECT_DVB:         /* 2.11:1 */
              this->aspect_ratio_float = 10000 * 2.11/1.0;
              break;
            case XINE_VO_ASPECT_SQUARE:      /* square pels */
              this->aspect_ratio_float = 10000;
              break;
            default:
              this->aspect_ratio_float = 10000 * 4.0 / 3.0;
              break;
          }
        } else {
          this->aspect_ratio_float = (10000 * info->sequence->pixel_width) / info->sequence->pixel_height;
          if (this->aspect_ratio_float > 20000) {
            this->aspect_ratio = XINE_VO_ASPECT_DVB;
          } else if (this->aspect_ratio_float > 15000) {
            this->aspect_ratio = XINE_VO_ASPECT_ANAMORPHIC;
          } else if (this->aspect_ratio_float > 12000) {
            this->aspect_ratio = XINE_VO_ASPECT_4_3;
          } else {
            this->aspect_ratio = XINE_VO_ASPECT_SQUARE;
          }
        }
        this->stream->stream_info[XINE_STREAM_INFO_VIDEO_RATIO] = this->aspect_ratio_float;


        if (info->sequence->flags & SEQ_FLAG_MPEG2) {
          this->stream->meta_info[XINE_META_INFO_VIDEOCODEC]  = strdup ("MPEG 2 (libmpeg2new)");
        } else {
          this->stream->meta_info[XINE_META_INFO_VIDEOCODEC]  = strdup ("MPEG 1 (libmpeg2new)");
        }

        break;
      case STATE_PICTURE:
        /* might skip */
        /* might set fbuf */
        if (info->current_picture->nb_fields == 1) {
          picture_structure = info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST ? VO_TOP_FIELD : VO_BOTTOM_FIELD;
        } else {
          picture_structure = VO_BOTH_FIELDS;
        }
          
        img = this->stream->video_out->get_frame (this->stream->video_out,
                                              info->sequence->picture_width,
                                              info->sequence->picture_height,
                                              this->aspect_ratio,  /* Aspect ratio */
                                              XINE_IMGFMT_YV12,
                                              picture_structure);
        this->frame_number++;
        img->top_field_first = info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST ? 1 : 0;
        img->repeat_first_field = (info->current_picture->nb_fields > 2) ? 1 : 0;
        img->duration=info->sequence->frame_period / 300;
        if( ((this->rff_pattern & 0xff) == 0xaa ||
             (this->rff_pattern & 0xff) == 0x55) ) {
          /* special case for ntsc 3:2 pulldown */
            img->duration += img->duration/4;
        } else {
          if( img->repeat_first_field ) {
            img->duration = (img->duration * info->current_picture->nb_fields) / 2; 
          }
        }

        if ((info->current_picture->flags & 7) == 1) {
          img->pts=buf_element->pts; /* If an I frame, use PTS */
        } else {
          img->pts=0;
        }

 
#ifdef LOG_FRAME_ALLOC_FREE
        printf ("libmpeg2:decode_data:get_frame %p (id=%d)\n", img,img->id);
#endif
        if (this->img_state[img->id] != 0) {
          printf ("libmpeg2:decode_data:get_frame id=%d BAD STATE:%d\n", img->id, this->img_state[img->id]);
          assert(0);
        }

        this->img_state[img->id] = 1;
        mpeg2_set_buf (this->mpeg2dec, img->base, img);
        break;
      case STATE_SLICE:
      case STATE_END:
        /* draw current picture */
        /* might free frame buffer */
        if (info->display_fbuf && info->display_fbuf->id) {
          img = (vo_frame_t *) info->display_fbuf->id;
          /* this should be used to detect any special rff pattern */
          this->rff_pattern = this->rff_pattern << 1;
          this->rff_pattern |= img->repeat_first_field;

#ifdef LOG_FRAME_ALLOC_FREE
          printf ("libmpeg2:decode_data:draw_frame %p, id=%d \n", info->display_fbuf, img->id);
#endif
          if (this->img_state[img->id] != 1) {
            printf ("libmpeg2:decode_data:draw_frame id=%d BAD STATE:%d\n", img->id, this->img_state[img->id]);
            assert(0);
          }
          if (this->img_state[img->id] == 1) {
            frame_skipping = img->draw (img, this->stream);
            /* FIXME: Handle skipping */
            this->img_state[img->id] = 2;
          }

        }
        if (info->discard_fbuf && !info->discard_fbuf->id) {
          printf ("libmpeg2:decode_data:BAD free_frame discard_fbuf=%p\n", info->discard_fbuf);
          assert(0);
        }
        if (info->discard_fbuf && info->discard_fbuf->id) {
          img = (vo_frame_t *) info->discard_fbuf->id;
#ifdef LOG_FRAME_ALLOC_FREE
          printf ("libmpeg2:decode_data:free_frame %p,id=%d\n", info->discard_fbuf, img->id);
#endif
          if (this->img_state[img->id] != 2) {
            printf ("libmpeg2:decode_data:free_frame id=%d BAD STATE:%d\n", img->id, this->img_state[img->id]);
            assert(0);
          }
          if (this->img_state[img->id] == 2) {
            img->free(img);
            this->img_state[img->id] = 0;
          }
        }
#ifdef LOG_FRAME_ALLOC_FREE
        mpeg2_video_print_bad_state(this->img_state);
#endif
        break;
      case STATE_GOP:
        break;
      default:
        break;
   }

 }

}

static void mpeg2_video_flush (video_decoder_t *this_gen) {
  mpeg2_video_decoder_t *this = (mpeg2_video_decoder_t *) this_gen;

#ifdef LOG
  printf ("libmpeg2: flush\n");
#endif

/*  mpeg2_flush (&this->mpeg2); */
}

static void mpeg2_video_reset (video_decoder_t *this_gen) {
  mpeg2_video_decoder_t *this = (mpeg2_video_decoder_t *) this_gen;

#ifdef LOG
  printf ("libmpeg2: reset\n");
#endif
/*  mpeg2_reset (&this->mpeg2dec); */
}

static void mpeg2_video_discontinuity (video_decoder_t *this_gen) {
  mpeg2_video_decoder_t *this = (mpeg2_video_decoder_t *) this_gen;

#ifdef LOG
  printf ("libmpeg2: dicontinuity\n");
#endif
/*  mpeg2_discontinuity (&this->mpeg2dec); */
}

static void mpeg2_video_dispose (video_decoder_t *this_gen) {

  mpeg2_video_decoder_t *this = (mpeg2_video_decoder_t *) this_gen;

#ifdef LOG
  printf ("libmpeg2: close\n");
#endif

  mpeg2_close (this->mpeg2dec);

  this->stream->video_out->close(this->stream->video_out, this->stream);

  free (this);
}

static video_decoder_t *open_plugin (video_decoder_class_t *class_gen, xine_stream_t *stream) {
  mpeg2_video_decoder_t *this ;
  int32_t n;

  this = (mpeg2_video_decoder_t *) malloc (sizeof (mpeg2_video_decoder_t));
  memset(this, 0, sizeof (mpeg2_video_decoder_t));

  this->video_decoder.decode_data         = mpeg2_video_decode_data;
  this->video_decoder.flush               = mpeg2_video_flush;
  this->video_decoder.reset               = mpeg2_video_reset;
  this->video_decoder.discontinuity       = mpeg2_video_discontinuity;
  this->video_decoder.dispose             = mpeg2_video_dispose;
  this->stream                            = stream;
  this->class                             = (mpeg2_class_t *) class_gen;
  this->frame_number=0;
  this->rff_pattern=0;

  this->mpeg2dec = mpeg2_init ();
  mpeg2_custom_fbuf (this->mpeg2dec, 1);  /* <- Force libmpeg2 to use xine frame buffers. */
  stream->video_out->open(stream->video_out, stream);
  this->force_aspect = 0;
  for(n=0;n<30;n++) this->img_state[n]=0;

  return &this->video_decoder;
}

/*
 * mpeg2 plugin class
 */

static char *get_identifier (video_decoder_class_t *this) {
  return "mpeg2new";
}

static char *get_description (video_decoder_class_t *this) {
  return "mpeg2 based video decoder plugin";
}

static void dispose_class (video_decoder_class_t *this) {
  free (this);
}

static void *init_plugin (xine_t *xine, void *data) {

  mpeg2_class_t *this;

  this = (mpeg2_class_t *) malloc (sizeof (mpeg2_class_t));

  this->decoder_class.open_plugin     = open_plugin;
  this->decoder_class.get_identifier  = get_identifier;
  this->decoder_class.get_description = get_description;
  this->decoder_class.dispose         = dispose_class;

  return this;
}
/*
 * exported plugin catalog entry
 */

static uint32_t supported_types[] = { BUF_VIDEO_MPEG, 0 };

static decoder_info_t dec_info_mpeg2 = {
  supported_types,     /* supported types */
  6                    /* priority        */
};

plugin_info_t xine_plugin_info[] = {
  /* type, API, "name", version, special_info, init_function */  
  { PLUGIN_VIDEO_DECODER, 14, "mpeg2new", XINE_VERSION_CODE, &dec_info_mpeg2, init_plugin },
  { PLUGIN_NONE, 0, "", 0, NULL, NULL }
};