Home | History | Annotate | Download | only in include
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 /************************************************************************/
     19 /*  file name       : media_fragment.h                                           */
     20 /*  file contents   : media fragment class & utilities       */
     21 /*  draw            : '96.10.29                                         */
     22 /*----------------------------------------------------------------------*/
     23 /*  amendment       :                                                   */
     24 /*      Copyright (C) 2002 PacketVideo                                   */
     25 /************************************************************************/
     26 
     27 #ifndef MEDIA_FRAGMENT_H
     28 #define MEDIA_FRAGMENT_H
     29 
     30 #include "oscl_mem.h"
     31 #include "oscl_media_data.h"
     32 
     33 class DefaultBufferMgr : public BufferMgr
     34 {
     35     public:
     36         DefaultBufferMgr()
     37         {
     38 
     39         }
     40         virtual ~DefaultBufferMgr()
     41         {
     42 
     43         }
     44         OsclAny BufferReleased(OsclAny* ptr, BufferState* state = NULL)
     45         {
     46             OSCL_DEFAULT_FREE(ptr);
     47             if (state)
     48                 OSCL_DELETE(state);
     49         }
     50 };
     51 
     52 
     53 /* the memory pool manager should allocate this.  The free function pointer iin the
     54 buffer_state should point to a routine that returns it to the pool */
     55 
     56 class MediaFragment
     57 {
     58     public:
     59         MediaFragment(): _state(NULL)
     60         {
     61             _fragment.ptr = NULL;
     62             _fragment.len = 0;
     63         }
     64         OSCL_IMPORT_REF MediaFragment(const MediaFragment& that, OsclAny* ptr = NULL, int32 len = 0);
     65 
     66         virtual ~MediaFragment()
     67         {
     68             if (_state)
     69                 _state->decrement_refcnt();
     70         }
     71         inline uint8* GetPtr()
     72         {
     73             return (uint8*)_fragment.ptr;
     74         }
     75         inline int32 GetLen()
     76         {
     77             return _fragment.len;
     78         }
     79 
     80         OsclAny Trim(uint8* ptr, int32 len)
     81         {
     82 //      if( (ptr >= _fragment.ptr && ptr < ((uint8*)_fragment.ptr+_fragment.len)) &&
     83 //          (((uint8*)ptr +len) <= ((uint8*)_fragment.ptr+_fragment.len)) ){
     84             _fragment.ptr = ptr;
     85             _fragment.len = len;
     86 //      }
     87         }
     88 
     89         virtual BufferFragment* GetFragment()
     90         {
     91             return &_fragment;
     92         }
     93         virtual BufferState* GetState()
     94         {
     95             return _state;
     96         }
     97 
     98     protected:
     99         BufferFragment _fragment;
    100         BufferState* _state;
    101     private:
    102         MediaFragment(BufferFragment& frag, BufferState* state):
    103                 _fragment(frag), _state(state)
    104         {
    105             if (state)
    106                 _state->increment_refcnt();
    107         }
    108 
    109         OsclAny Set(BufferFragment& frag, BufferState* state)
    110         {
    111             BufferState* old_state = _state;
    112             _state = state;
    113 
    114             if (_state)
    115             {
    116                 _state->increment_refcnt();
    117             }
    118             _fragment = frag;
    119             if (old_state)
    120             {
    121                 old_state->decrement_refcnt();
    122             }
    123         }
    124 
    125         OsclAny Set(OsclAny* ptr, int32 len, BufferState* state)
    126         {
    127             BufferState* old_state = _state;
    128             _state = state;
    129 
    130             if (_state)
    131             {
    132                 _state->increment_refcnt();
    133             }
    134             _fragment.len = len;
    135             _fragment.ptr = ptr;
    136 
    137             if (old_state)
    138             {
    139                 old_state->decrement_refcnt();
    140             }
    141         }
    142 
    143         friend class DefFragmentAllocator;
    144         friend class PoolFragmentAllocator;
    145         friend class MediaPacket;
    146 
    147         friend class AudioEncodeFragmentAllocator;
    148         friend class VideoEncodeFragmentAllocator;
    149 };
    150 
    151 typedef int32 FragmentType;
    152 const FragmentType FRAG_TYPE_DEFAULT = 0;
    153 
    154 /* Default media fragment and allocator. */
    155 class FragmentAllocator
    156 {
    157     public:
    158         virtual ~FragmentAllocator() {}
    159         OSCL_IMPORT_REF virtual MediaFragment* allocate_fragment(uint32 sz) = 0;
    160         OSCL_IMPORT_REF virtual MediaFragment* allocate_fragment(MediaFragment* fragment,
    161                 OsclAny* ptr = NULL, uint32 len = 0) = 0;
    162         OSCL_IMPORT_REF virtual int32 deallocate_fragment(MediaFragment* fragment) = 0;
    163 };
    164 
    165 class DefFragmentAllocator :  public FragmentAllocator
    166 {
    167     public:
    168         DefFragmentAllocator()
    169         {
    170 
    171         }
    172         ~DefFragmentAllocator()
    173         {
    174 
    175         }
    176         OSCL_IMPORT_REF virtual MediaFragment* allocate_fragment(uint32 sz);
    177         OSCL_IMPORT_REF virtual MediaFragment* allocate_fragment(MediaFragment* fragment,
    178                 OsclAny* ptr = NULL, uint32 len = 0);
    179         OSCL_IMPORT_REF virtual int32 deallocate_fragment(MediaFragment* fragment);
    180     private:
    181         DefaultBufferMgr buffer_state_listener;
    182 };
    183 
    184 
    185 /* Media fragment pool and allocator. */
    186 
    187 class BufferStateEntry
    188 {
    189     public:
    190         BufferStateEntry() : next_entry(NULL)
    191         {
    192             frag.ptr = NULL;
    193             frag.len = 0;
    194         };
    195         ~BufferStateEntry() {};
    196         BufferState buf_state;
    197         BufferFragment frag;
    198         MediaFragment media_frag;
    199         BufferStateEntry *next_entry;
    200 };
    201 
    202 class PoolFragmentAllocator;
    203 
    204 class BufferPoolMgr : public BufferMgr
    205 {
    206     public:
    207         BufferPoolMgr(PoolFragmentAllocator *pPool)
    208         {
    209             pBufferPool = pPool;
    210         }
    211         virtual ~BufferPoolMgr() {};
    212         OSCL_IMPORT_REF OsclAny BufferReleased(OsclAny* ptr, BufferState* state = NULL);
    213     private:
    214         PoolFragmentAllocator *pBufferPool;
    215 };
    216 
    217 
    218 class PoolFragmentAllocator :  public FragmentAllocator
    219 {
    220     public:
    221         OSCL_IMPORT_REF PoolFragmentAllocator(int32 max_frags, int32 max_frag_size);
    222         OSCL_IMPORT_REF ~PoolFragmentAllocator();
    223 
    224         OSCL_IMPORT_REF virtual MediaFragment* allocate_fragment(uint32 sz);
    225         OSCL_IMPORT_REF virtual MediaFragment* allocate_fragment(MediaFragment* fragment,
    226                 OsclAny* ptr = NULL, uint32 len = 0);
    227         OSCL_IMPORT_REF virtual int32 deallocate_fragment(MediaFragment* fragment);
    228 
    229         OsclAny free_buffer_to_pool(BufferStateEntry *entry);
    230     private:
    231 
    232         int32 num_frags;
    233         int32 frag_size;
    234         BufferPoolMgr *buffer_state_listener;
    235         BufferStateEntry *free_list;
    236         BufferStateEntry *entire_list;
    237         uint8 *buffer;
    238 };
    239 
    240 
    241 
    242 #endif /* MEDIA_FRAGMENT_H */
    243