Annotation of multiplexer/descref.c, revision 1.9

1.1       oskar       1: /*
                      2:  * ISO 13818 stream multiplexer
                      3:  * Copyright (C) 2001 Convergence Integrated Media GmbH Berlin
1.8       oskar       4:  * Author: Oskar Schirmer (oskar@scara.com)
1.9     ! oskar       5:  *
        !             6:  * This program is free software; you can redistribute it and/or modify
        !             7:  * it under the terms of the GNU General Public License as published by
        !             8:  * the Free Software Foundation; either version 2 of the License, or
        !             9:  * (at your option) any later version.
        !            10:  *
        !            11:  * This program is distributed in the hope that it will be useful,
        !            12:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            13:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        !            14:  * GNU General Public License for more details.
        !            15:  *
        !            16:  * You should have received a copy of the GNU General Public License
        !            17:  * along with this program; if not, write to the Free Software
        !            18:  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1.1       oskar      19:  */
                     20: 
                     21: /*
                     22:  * Module:  Descref
                     23:  * Purpose: Put descriptor data into map stream buffers.
                     24:  */
                     25: 
                     26: #include "global.h"
                     27: #include "error.h"
1.3       oskar      28: #include "splitts.h"
1.1       oskar      29: 
                     30: static mapreference mapref;
                     31: static int din;
                     32: 
1.3       oskar      33: /* Start descriptor processing into a map stream s.
                     34:  * The descriptor functions are to be used always in the
                     35:  * sequence "alloc, put...put, finish"
                     36:  * Postcondition: din==InputIndex, enough free space for PSI.
                     37:  */
1.1       oskar      38: void alloc_descriptor (stream_descr *s,
                     39:     int sourceid,
                     40:     int programnumber,
                     41:     byte version)
                     42: {
                     43:   if (s != NULL) {
                     44:     warn (LINF,"Alloc Descriptor",EDES,1,version,sourceid);
                     45:     mapref.sourceid = sourceid;
                     46:     mapref.programnumber = programnumber;
                     47:     mapref.version = version;
                     48:     memset (&mapref.elemdnew,0,sizeof(mapref.elemdnew));
                     49:     din = s->data.in;
                     50:     while (list_free (s->data) < 2*(sizeof(mapreference) + MAX_PSI_SIZE) + 1) {
                     51:       list_incr (s->ctrl.out,s->ctrl,1);
                     52:       s->data.out = s->ctrl.ptr[s->ctrl.out].index;
                     53:     }
                     54:     if (list_freeinend (s->data) < sizeof(mapreference) + MAX_PSI_SIZE) {
                     55:       din = 0;
                     56:     }
                     57:     s->ctrl.ptr[s->ctrl.in].index = din;
                     58:     s->ctrl.ptr[s->ctrl.in].length = sizeof(mapreference);
                     59:     din += sizeof(mapreference);
                     60:   }
                     61: }
                     62: 
1.3       oskar      63: /* Scan a descriptor and put it into the map stream s.
                     64:  * Source is raw data from file f.
                     65:  * Decrease infolen^ by number of bytes processed, or set to -1 on error.
                     66:  * Precondition: as thru alloc_descriptor
                     67:  * Return: index increased by number of bytes processed.
                     68:  */
1.1       oskar      69: int put_descriptor (file_descr *f,
                     70:   stream_descr *s,
                     71:   int index,
                     72:   int *infolen)
                     73: {
                     74:   byte t, l;
1.6       oskar      75:   t = f->data.ptr[index];
                     76: #if (NUMBER_DESCR < 0x100)
                     77:   if (t >= NUMBER_DESCR) {
1.1       oskar      78:     warn (LWAR,"Bad Descriptor Tag",EDES,2,1,t);
                     79:     *infolen = -1;
                     80:     return (index);
                     81:   }
1.6       oskar      82: #endif
1.1       oskar      83:   warn (LINF,"Put Descriptor",EDES,2,0,t);
                     84:   list_incr (index,f->data,1);
                     85:   l = f->data.ptr[index];
                     86:   if ((*infolen -= (l + 2)) >= 0) {
                     87:     if (s != NULL) {
                     88:       mapref.elemdnew[t] = &s->data.ptr[din];
                     89:       s->ctrl.ptr[s->ctrl.in].length += (l + 2);
                     90:       s->data.ptr[din++] = t;
                     91:       s->data.ptr[din++] = l;
                     92:       while (l > 0) {
                     93:         list_incr (index,f->data,1);
                     94:         s->data.ptr[din++] = f->data.ptr[index];
                     95:         l -= 1;
                     96:       }
                     97:       list_incr (index,f->data,1);
                     98:     } else {
                     99:       list_incr (index,f->data,l+1);
                    100:     }
                    101:   }
                    102:   return (index);
                    103: }
                    104: 
1.3       oskar     105: /* Scan a descriptor and put it into the map stream s.
                    106:  * Source is direct data from byte pointer d.
                    107:  * Decrease infolen^ by number of bytes processed, or set to -1 on error.
                    108:  * Precondition: as thru alloc_descriptor
                    109:  * Return: byte pointer d increased by number of bytes processed.
                    110:  */
1.1       oskar     111: byte *put_descriptor_s (byte *d,
                    112:   stream_descr *s,
                    113:   int *infolen)
                    114: {
                    115:   byte t, l;
1.6       oskar     116:   t = *d++;
                    117: #if (NUMBER_DESCR < 0x100)
                    118:   if (t >= NUMBER_DESCR) {
1.1       oskar     119:     warn (LWAR,"Bad Descriptor Tag",EDES,3,1,t);
                    120:     *infolen = -1;
                    121:     return (d);
                    122:   }
1.6       oskar     123: #endif
1.1       oskar     124:   warn (LINF,"Put Descriptor",EDES,3,0,t);
                    125:   l = *d++;
                    126:   if ((*infolen -= (l + 2)) >= 0) {
                    127:     if (s != NULL) {
                    128:       mapref.elemdnew[t] = &s->data.ptr[din];
                    129:       s->ctrl.ptr[s->ctrl.in].length += (l + 2);
                    130:       s->data.ptr[din++] = t;
                    131:       s->data.ptr[din++] = l;
                    132:       while (l > 0) {
                    133:         s->data.ptr[din++] = *d++;
                    134:         l -= 1;
                    135:       }
                    136:     } else {
                    137:       d += l;
                    138:     }
                    139:   }
                    140:   return (d);
                    141: }
                    142: 
1.3       oskar     143: /* Finish the collection of descriptors into the map stream s.
                    144:  */
1.1       oskar     145: void finish_descriptor (stream_descr *s)
                    146: {
                    147:   if (s != NULL) {
                    148:     warn (LINF,"Finish Descriptor",EDES,4,
                    149:         s->fdescr->sequence,s->ctrl.ptr[s->ctrl.in].length);
                    150:     memcpy (&s->data.ptr[s->ctrl.ptr[s->ctrl.in].index],&mapref,sizeof(mapref));
                    151:     s->data.in = din;
                    152:     warn (LDEB,"Sequence",EDES,4,1,s->fdescr->sequence);
                    153:     s->ctrl.ptr[s->ctrl.in].sequence = s->fdescr->sequence++;
                    154:     list_incr (s->ctrl.in,s->ctrl,1);
                    155:     if (s->ctrl.out == s->ctrl.in) {
                    156:       list_incr (s->ctrl.out,s->ctrl,1);
                    157:       s->data.out = s->ctrl.ptr[s->ctrl.out].index;
                    158:     }
                    159:   }
                    160: }
                    161: 
1.3       oskar     162: /* Save a set of descriptors map with a stream s,
                    163:  * including both the references and the raw data.
                    164:  */
1.1       oskar     165: static void save_mapreference (mapreference *map,
                    166:     byte *dscr,
                    167:     int size,
                    168:     stream_descr *s)
                    169: {
                    170:   int i;
                    171:   warn (LINF,"Save Mapref",EDES,5,0,size);
                    172:   s->version = map->version;
                    173:   /* ... = map->programnumber */
1.5       oskar     174:   memcpy (&s->autodescr->data[0],dscr,size);
1.6       oskar     175:   i = NUMBER_DESCR;
1.1       oskar     176:   while (--i >= 0) {
                    177:     if (map->elemdnew[i] == NULL) {
1.5       oskar     178:       s->autodescr->refx[i] = NULL;
1.1       oskar     179:     } else {
1.5       oskar     180:       s->autodescr->refx[i] =
                    181:         map->elemdnew[i] + ((&s->autodescr->data[0]) - dscr);
1.1       oskar     182:     }
                    183:   }
                    184: }
                    185: 
1.3       oskar     186: /* Take a set of descriptors from map stream m,
                    187:  * determine the right stream to put the descriptors into
                    188:  * (either the map stream itself, or a related data stream),
                    189:  * save the descriptors into that stream.
                    190:  */
1.1       oskar     191: void validate_mapref (stream_descr *m)
                    192: {
                    193:   stream_descr *s;
                    194:   int l;
                    195:   mapreference *pmapref;
                    196:   pmapref = (mapreference *)&m->data.ptr[m->data.out];
                    197:   if (m->sourceid == pmapref->sourceid) {
                    198:     s = m;
                    199:   } else {
                    200:     switch (m->fdescr->content) {
                    201:       case ct_program:
                    202:         s = m->fdescr->u.ps.stream[pmapref->sourceid];
                    203:         break;
                    204:       case ct_transport:
1.3       oskar     205:         s = ts_file_stream (m->fdescr,pmapref->sourceid);
1.1       oskar     206:         break;
                    207:       default:
                    208:         warn (LERR,"Mapref NULL",EDES,6,1,m->fdescr->content);
                    209:         s = NULL;
                    210:         break;
                    211:     }
                    212:   }
                    213:   if (s != NULL) {
                    214:     if (s->version != pmapref->version) {
                    215:       save_mapreference (pmapref,
                    216:           &m->data.ptr[m->data.out+sizeof(mapreference)],
                    217:           m->ctrl.ptr[m->ctrl.out].length-sizeof(mapreference),s);
1.4       oskar     218:       if (s->streamdata != sd_data) {
1.1       oskar     219:         warn (LDEB,"Mapref isamap",EDES,6,3,pmapref->sourceid);
                    220:         /* must do something, if input program is related to output prog */
                    221:       } else {
                    222:         s->u.d.mention = TRUE;
                    223:         l = s->u.d.progs;
                    224:         while (--l >= 0) {
1.7       oskar     225:           s->u.d.pdescr[l]->unchanged = TRUE;
                    226: /* check this: changed in which cases ? */
1.1       oskar     227:         }
                    228:       }
                    229:     }
                    230:   } else {
                    231:     warn (LDEB,"Mapref NULL",EDES,6,2,pmapref->sourceid);
                    232:   }
                    233:   list_incr (m->ctrl.out,m->ctrl,1);
                    234:   m->data.out = m->ctrl.ptr[m->ctrl.out].index;
1.5       oskar     235: }
                    236: 
                    237: /* Clear a descriptor struct
                    238:  */
                    239: void clear_descrdescr (descr_descr *dd)
                    240: {
                    241:   memset (dd,0,sizeof(descr_descr));
1.1       oskar     242: }
                    243: 

LinuxTV legacy CVS <linuxtv.org/cvs>