| 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 | |
| 28 | #include "mmal_types.h" |
| 29 | #include "mmal_format.h" |
| 30 | #include "util/mmal_util_rational.h" |
| 31 | |
| 32 | #define MMAL_ES_FORMAT_MAGIC MMAL_FOURCC('m','a','g','f') |
| 33 | #define 32 |
| 34 | #define (10*1024) |
| 35 | |
| 36 | typedef struct MMAL_ES_FORMAT_PRIVATE_T |
| 37 | { |
| 38 | MMAL_ES_FORMAT_T format; |
| 39 | MMAL_ES_SPECIFIC_FORMAT_T es; |
| 40 | |
| 41 | uint32_t magic; |
| 42 | |
| 43 | unsigned int ; |
| 44 | uint8_t *; |
| 45 | |
| 46 | uint8_t buffer[EXTRADATA_SIZE_DEFAULT]; |
| 47 | |
| 48 | } MMAL_ES_FORMAT_PRIVATE_T; |
| 49 | |
| 50 | /** Allocate a format structure */ |
| 51 | MMAL_ES_FORMAT_T *mmal_format_alloc(void) |
| 52 | { |
| 53 | MMAL_ES_FORMAT_PRIVATE_T *private; |
| 54 | |
| 55 | private = vcos_calloc(1, sizeof(*private), "mmal format" ); |
| 56 | if(!private) return 0; |
| 57 | memset(private, 0, sizeof(*private)); |
| 58 | |
| 59 | private->magic = MMAL_ES_FORMAT_MAGIC; |
| 60 | private->format.es = (void *)&private->es; |
| 61 | private->extradata_size = EXTRADATA_SIZE_DEFAULT; |
| 62 | |
| 63 | return &private->format; |
| 64 | } |
| 65 | |
| 66 | /** Free a format structure */ |
| 67 | void mmal_format_free(MMAL_ES_FORMAT_T *format) |
| 68 | { |
| 69 | MMAL_ES_FORMAT_PRIVATE_T *private = (MMAL_ES_FORMAT_PRIVATE_T *)format; |
| 70 | vcos_assert(private->magic == MMAL_ES_FORMAT_MAGIC); |
| 71 | if(private->extradata) vcos_free(private->extradata); |
| 72 | vcos_free(private); |
| 73 | } |
| 74 | |
| 75 | /** Copy a format structure */ |
| 76 | void mmal_format_copy(MMAL_ES_FORMAT_T *fmt_dst, MMAL_ES_FORMAT_T *fmt_src) |
| 77 | { |
| 78 | void *backup = fmt_dst->es; |
| 79 | *fmt_dst->es = *fmt_src->es; |
| 80 | *fmt_dst = *fmt_src; |
| 81 | fmt_dst->es = backup; |
| 82 | fmt_dst->extradata = 0; |
| 83 | fmt_dst->extradata_size = 0; |
| 84 | } |
| 85 | |
| 86 | /** Full copy of a format structure (including extradata) */ |
| 87 | MMAL_STATUS_T mmal_format_full_copy(MMAL_ES_FORMAT_T *fmt_dst, MMAL_ES_FORMAT_T *fmt_src) |
| 88 | { |
| 89 | mmal_format_copy(fmt_dst, fmt_src); |
| 90 | |
| 91 | if (fmt_src->extradata_size) |
| 92 | { |
| 93 | MMAL_STATUS_T status = mmal_format_extradata_alloc(fmt_dst, fmt_src->extradata_size); |
| 94 | if (status != MMAL_SUCCESS) |
| 95 | return status; |
| 96 | fmt_dst->extradata_size = fmt_src->extradata_size; |
| 97 | memcpy(fmt_dst->extradata, fmt_src->extradata, fmt_src->extradata_size); |
| 98 | } |
| 99 | return MMAL_SUCCESS; |
| 100 | } |
| 101 | |
| 102 | /** Compare 2 format structures */ |
| 103 | uint32_t mmal_format_compare(MMAL_ES_FORMAT_T *fmt1, MMAL_ES_FORMAT_T *fmt2) |
| 104 | { |
| 105 | MMAL_VIDEO_FORMAT_T *video1, *video2; |
| 106 | uint32_t result = 0; |
| 107 | |
| 108 | if (fmt1->type != fmt2->type) |
| 109 | return MMAL_ES_FORMAT_COMPARE_FLAG_TYPE; |
| 110 | |
| 111 | if (fmt1->encoding != fmt2->encoding) |
| 112 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_ENCODING; |
| 113 | if (fmt1->bitrate != fmt2->bitrate) |
| 114 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_BITRATE; |
| 115 | if (fmt1->flags != fmt2->flags) |
| 116 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_FLAGS; |
| 117 | if (fmt1->extradata_size != fmt2->extradata_size || |
| 118 | (fmt1->extradata_size && (!fmt1->extradata || !fmt2->extradata)) || |
| 119 | memcmp(fmt1->extradata, fmt2->extradata, fmt1->extradata_size)) |
| 120 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_EXTRADATA; |
| 121 | |
| 122 | /* Compare the ES specific information */ |
| 123 | switch (fmt1->type) |
| 124 | { |
| 125 | case MMAL_ES_TYPE_VIDEO: |
| 126 | video1 = &fmt1->es->video; |
| 127 | video2 = &fmt2->es->video; |
| 128 | if (video1->width != video2->width || video1->height != video2->height) |
| 129 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_VIDEO_RESOLUTION; |
| 130 | if (memcmp(&video1->crop, &video2->crop, sizeof(video1->crop))) |
| 131 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_VIDEO_CROPPING; |
| 132 | if (!mmal_rational_equal(video1->par, video2->par)) |
| 133 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_VIDEO_ASPECT_RATIO; |
| 134 | if (!mmal_rational_equal(video1->frame_rate, video2->frame_rate)) |
| 135 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_VIDEO_FRAME_RATE; |
| 136 | if (video1->color_space != video2->color_space) |
| 137 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_VIDEO_COLOR_SPACE; |
| 138 | /* coverity[overrun-buffer-arg] We're comparing the rest of the video format structure */ |
| 139 | if (memcmp(((char*)&video1->color_space) + sizeof(video1->color_space), |
| 140 | ((char*)&video2->color_space) + sizeof(video2->color_space), |
| 141 | sizeof(*video1) - offsetof(MMAL_VIDEO_FORMAT_T, color_space) - sizeof(video1->color_space))) |
| 142 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_ES_OTHER; |
| 143 | break; |
| 144 | case MMAL_ES_TYPE_AUDIO: |
| 145 | if (memcmp(fmt1->es, fmt2->es, sizeof(MMAL_AUDIO_FORMAT_T))) |
| 146 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_ES_OTHER; |
| 147 | break; |
| 148 | case MMAL_ES_TYPE_SUBPICTURE: |
| 149 | if (memcmp(fmt1->es, fmt2->es, sizeof(MMAL_SUBPICTURE_FORMAT_T))) |
| 150 | result |= MMAL_ES_FORMAT_COMPARE_FLAG_ES_OTHER; |
| 151 | break; |
| 152 | default: |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | /** */ |
| 160 | MMAL_STATUS_T (MMAL_ES_FORMAT_T *format, unsigned int size) |
| 161 | { |
| 162 | MMAL_ES_FORMAT_PRIVATE_T *private = (MMAL_ES_FORMAT_PRIVATE_T *)format; |
| 163 | |
| 164 | /* Sanity check the size requested */ |
| 165 | if(size > EXTRADATA_SIZE_MAX) |
| 166 | return MMAL_EINVAL; |
| 167 | |
| 168 | /* Allocate memory if needed */ |
| 169 | if(private->extradata_size < size) |
| 170 | { |
| 171 | if(private->extradata) vcos_free(private->extradata); |
| 172 | private->extradata = vcos_calloc(1, size, "mmal format extradata" ); |
| 173 | if(!private->extradata) |
| 174 | return MMAL_ENOMEM; |
| 175 | private->extradata_size = size; |
| 176 | } |
| 177 | |
| 178 | /* Set the fields in the actual format structure */ |
| 179 | if(private->extradata) private->format.extradata = private->extradata; |
| 180 | else private->format.extradata = private->buffer; |
| 181 | |
| 182 | return MMAL_SUCCESS; |
| 183 | } |
| 184 | |