| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "containers/core/containers_private.h" |
| 31 | #include "containers/core/containers_io_helpers.h" |
| 32 | #include "containers/core/containers_utils.h" |
| 33 | #include "containers/core/containers_logging.h" |
| 34 | |
| 35 | /****************************************************************************** |
| 36 | Supported extensions |
| 37 | ******************************************************************************/ |
| 38 | static const char *extensions[] = |
| 39 | { "mp3" , "aac" , "adts" , "ac3" , "ec3" , "amr" , "awb" , "evrc" , "dts" , |
| 40 | "m1v" , "m2v" , "mp4v" , "h263" , "263" , "h264" , "264" , "mvc" , |
| 41 | "bin" , 0 |
| 42 | }; |
| 43 | |
| 44 | /****************************************************************************** |
| 45 | Type definitions |
| 46 | ******************************************************************************/ |
| 47 | typedef struct VC_CONTAINER_MODULE_T |
| 48 | { |
| 49 | VC_CONTAINER_TRACK_T *track; |
| 50 | |
| 51 | } VC_CONTAINER_MODULE_T; |
| 52 | |
| 53 | /****************************************************************************** |
| 54 | Function prototypes |
| 55 | ******************************************************************************/ |
| 56 | VC_CONTAINER_STATUS_T binary_writer_open( VC_CONTAINER_T * ); |
| 57 | |
| 58 | /***************************************************************************** |
| 59 | Functions exported as part of the Container Module API |
| 60 | *****************************************************************************/ |
| 61 | static VC_CONTAINER_STATUS_T binary_writer_close( VC_CONTAINER_T *p_ctx ) |
| 62 | { |
| 63 | VC_CONTAINER_MODULE_T *module = p_ctx->priv->module; |
| 64 | for(; p_ctx->tracks_num > 0; p_ctx->tracks_num--) |
| 65 | vc_container_free_track(p_ctx, p_ctx->tracks[p_ctx->tracks_num-1]); |
| 66 | free(module); |
| 67 | return VC_CONTAINER_SUCCESS; |
| 68 | } |
| 69 | |
| 70 | /*****************************************************************************/ |
| 71 | static VC_CONTAINER_STATUS_T binary_writer_write( VC_CONTAINER_T *p_ctx, |
| 72 | VC_CONTAINER_PACKET_T *packet ) |
| 73 | { |
| 74 | WRITE_BYTES(p_ctx, packet->data, packet->size); |
| 75 | return STREAM_STATUS(p_ctx); |
| 76 | } |
| 77 | |
| 78 | /*****************************************************************************/ |
| 79 | static VC_CONTAINER_STATUS_T binary_writer_control( VC_CONTAINER_T *p_ctx, |
| 80 | VC_CONTAINER_CONTROL_T operation, va_list args ) |
| 81 | { |
| 82 | VC_CONTAINER_ES_FORMAT_T *format; |
| 83 | VC_CONTAINER_TRACK_T *track; |
| 84 | VC_CONTAINER_STATUS_T status; |
| 85 | |
| 86 | switch(operation) |
| 87 | { |
| 88 | case VC_CONTAINER_CONTROL_TRACK_ADD: |
| 89 | format = (VC_CONTAINER_ES_FORMAT_T *)va_arg( args, VC_CONTAINER_ES_FORMAT_T * ); |
| 90 | |
| 91 | /* Allocate and initialise track data */ |
| 92 | if(p_ctx->tracks_num >= 1) return VC_CONTAINER_ERROR_OUT_OF_RESOURCES; |
| 93 | p_ctx->tracks[p_ctx->tracks_num] = track = vc_container_allocate_track(p_ctx, 0); |
| 94 | if(!track) return VC_CONTAINER_ERROR_OUT_OF_MEMORY; |
| 95 | |
| 96 | if(format->extradata_size) |
| 97 | { |
| 98 | status = vc_container_track_allocate_extradata( p_ctx, track, format->extradata_size ); |
| 99 | if(status != VC_CONTAINER_SUCCESS) |
| 100 | { |
| 101 | vc_container_free_track(p_ctx, track); |
| 102 | return status; |
| 103 | } |
| 104 | WRITE_BYTES(p_ctx, format->extradata, format->extradata_size); |
| 105 | } |
| 106 | |
| 107 | vc_container_format_copy(track->format, format, format->extradata_size); |
| 108 | p_ctx->tracks_num++; |
| 109 | return VC_CONTAINER_SUCCESS; |
| 110 | |
| 111 | case VC_CONTAINER_CONTROL_TRACK_ADD_DONE: |
| 112 | return VC_CONTAINER_SUCCESS; |
| 113 | |
| 114 | default: return VC_CONTAINER_ERROR_UNSUPPORTED_OPERATION; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /*****************************************************************************/ |
| 119 | VC_CONTAINER_STATUS_T binary_writer_open( VC_CONTAINER_T *p_ctx ) |
| 120 | { |
| 121 | const char *extension = vc_uri_path_extension(p_ctx->priv->uri); |
| 122 | VC_CONTAINER_MODULE_T *module = 0; |
| 123 | VC_CONTAINER_STATUS_T status = VC_CONTAINER_ERROR_FORMAT_INVALID; |
| 124 | unsigned int i; |
| 125 | |
| 126 | /* Check if the user has specified a container */ |
| 127 | vc_uri_find_query(p_ctx->priv->uri, 0, "container" , &extension); |
| 128 | |
| 129 | /* Check we're the right writer for this */ |
| 130 | if(!extension) |
| 131 | return VC_CONTAINER_ERROR_FORMAT_NOT_SUPPORTED; |
| 132 | for(i = 0; extensions[i]; i++) |
| 133 | if(!strcasecmp(extension, extensions[i])) break; |
| 134 | if(!extensions[i]) |
| 135 | return VC_CONTAINER_ERROR_FORMAT_NOT_SUPPORTED; |
| 136 | |
| 137 | /* Allocate our context */ |
| 138 | module = malloc(sizeof(*module)); |
| 139 | if(!module) { status = VC_CONTAINER_ERROR_OUT_OF_MEMORY; goto error; } |
| 140 | memset(module, 0, sizeof(*module)); |
| 141 | p_ctx->priv->module = module; |
| 142 | p_ctx->tracks = &module->track; |
| 143 | |
| 144 | p_ctx->priv->pf_close = binary_writer_close; |
| 145 | p_ctx->priv->pf_write = binary_writer_write; |
| 146 | p_ctx->priv->pf_control = binary_writer_control; |
| 147 | return VC_CONTAINER_SUCCESS; |
| 148 | |
| 149 | error: |
| 150 | LOG_DEBUG(p_ctx, "binary: error opening stream (%i)" , status); |
| 151 | return status; |
| 152 | } |
| 153 | |
| 154 | /******************************************************************************** |
| 155 | Entrypoint function |
| 156 | ********************************************************************************/ |
| 157 | |
| 158 | #if !defined(ENABLE_CONTAINERS_STANDALONE) && defined(__HIGHC__) |
| 159 | # pragma weak writer_open binary_writer_open |
| 160 | #endif |
| 161 | |