dr_08.c 4 KB
Newer Older
1 2
/*****************************************************************************
 * dr_08.c
3
 * Copyright (C) 2001-2010 VideoLAN
4
 * $Id: dr_08.c,v 1.4 2003/07/25 20:20:40 fenrir Exp $
5 6 7
 *
 * Authors: Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
 *
8 9 10 11
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
17
 *
18 19 20
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 22 23 24 25 26 27 28 29 30
 *
 *****************************************************************************/


#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

31 32 33 34 35 36
#if defined(HAVE_INTTYPES_H)
#include <inttypes.h>
#elif defined(HAVE_STDINT_H)
#include <stdint.h>
#endif

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "../dvbpsi.h"
#include "../dvbpsi_private.h"
#include "../descriptor.h"

#include "dr_08.h"


/*****************************************************************************
 * dvbpsi_DecodeVWindowDr
 *****************************************************************************/
dvbpsi_vwindow_dr_t * dvbpsi_DecodeVWindowDr(dvbpsi_descriptor_t * p_descriptor)
{
  dvbpsi_vwindow_dr_t * p_decoded;

  /* Check the tag */
  if(p_descriptor->i_tag != 0x08)
  {
54
    dvbpsi_error(h_dvbpsi, "dr_08 decoder", "bad tag (0x%x)", p_descriptor->i_tag);
55 56 57 58 59 60 61 62 63
    return NULL;
  }

  /* Don't decode twice */
  if(p_descriptor->p_decoded)
    return p_descriptor->p_decoded;

  /* Allocate memory */
  p_decoded = (dvbpsi_vwindow_dr_t*)malloc(sizeof(dvbpsi_vwindow_dr_t));
64
  if(!p_decoded) return NULL;
65 66 67 68

  /* Decode data and check the length */
  if(p_descriptor->i_length != 4)
  {
69
    dvbpsi_error(h_dvbpsi, "dr_08 decoder", "bad length (%d)",
70 71
                     p_descriptor->i_length);
    free(p_decoded);
72
    return NULL;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  }

  p_decoded->i_horizontal_offset =   ((uint16_t)(p_descriptor->p_data[0]) << 6)
                                   | ((p_descriptor->p_data[1] & 0xfc) >> 2);
  p_decoded->i_vertical_offset =
                          ((uint16_t)(p_descriptor->p_data[1] & 0x03) << 12)
                        | ((uint16_t)(p_descriptor->p_data[2]) << 4)
                        | ((p_descriptor->p_data[3] & 0xf0) >> 4);
  p_decoded->i_window_priority = p_descriptor->p_data[3] & 0x0f;

  p_descriptor->p_decoded = (void*)p_decoded;

  return p_decoded;
}


/*****************************************************************************
 * dvbpsi_GenVWindowDr
 *****************************************************************************/
dvbpsi_descriptor_t * dvbpsi_GenVWindowDr(dvbpsi_vwindow_dr_t * p_decoded,
                                          int b_duplicate)
{
  /* Create the descriptor */
96
  dvbpsi_descriptor_t * p_descriptor = dvbpsi_NewDescriptor(0x08, 4, NULL);
97 98 99 100 101 102 103

  if(p_descriptor)
  {
    /* Encode data */
    p_descriptor->p_data[0] = p_decoded->i_horizontal_offset >> 6;
    p_descriptor->p_data[1] =   (((uint8_t)p_decoded->i_horizontal_offset) << 2)
                              | (p_decoded->i_vertical_offset >> 12);
104
    p_descriptor->p_data[2] = p_decoded->i_vertical_offset >> 4;
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    p_descriptor->p_data[3] =   (((uint8_t)p_decoded->i_vertical_offset) << 4)
                              | (p_decoded->i_window_priority & 0x0f);

    if(b_duplicate)
    {
      /* Duplicate decoded data */
      dvbpsi_vwindow_dr_t * p_dup_decoded =
                (dvbpsi_vwindow_dr_t*)malloc(sizeof(dvbpsi_vwindow_dr_t));
      if(p_dup_decoded)
        memcpy(p_dup_decoded, p_decoded, sizeof(dvbpsi_vwindow_dr_t));

      p_descriptor->p_decoded = (void*)p_dup_decoded;
    }
  }

  return p_descriptor;
}