| 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 "mmalomx.h" |
| 29 | #include "mmalomx_roles.h" |
| 30 | #include "mmalomx_registry.h" |
| 31 | #include "mmalomx_logging.h" |
| 32 | |
| 33 | static const struct { |
| 34 | const char *name; |
| 35 | MMALOMX_ROLE_T role; |
| 36 | } mmalomx_roles[] = |
| 37 | { |
| 38 | {"video_decoder.h263" , MMALOMX_ROLE_VIDEO_DECODER_H263}, |
| 39 | {"video_decoder.mpeg4" , MMALOMX_ROLE_VIDEO_DECODER_MPEG4}, |
| 40 | {"video_decoder.avc" , MMALOMX_ROLE_VIDEO_DECODER_AVC}, |
| 41 | {"video_decoder.mpeg2" , MMALOMX_ROLE_VIDEO_DECODER_MPEG2}, |
| 42 | {"video_decoder.wmv" , MMALOMX_ROLE_VIDEO_DECODER_WMV}, |
| 43 | {"video_decoder.vpx" , MMALOMX_ROLE_VIDEO_DECODER_VPX}, |
| 44 | |
| 45 | {"video_encoder.h263" , MMALOMX_ROLE_VIDEO_ENCODER_H263}, |
| 46 | {"video_encoder.mpeg4" , MMALOMX_ROLE_VIDEO_ENCODER_MPEG4}, |
| 47 | {"video_encoder.avc" , MMALOMX_ROLE_VIDEO_ENCODER_AVC}, |
| 48 | |
| 49 | {"audio_decoder.aac" , MMALOMX_ROLE_AUDIO_DECODER_AAC}, |
| 50 | {"audio_decoder.mp1" , MMALOMX_ROLE_AUDIO_DECODER_MPGA_L1}, |
| 51 | {"audio_decoder.mp2" , MMALOMX_ROLE_AUDIO_DECODER_MPGA_L2}, |
| 52 | {"audio_decoder.mp3" , MMALOMX_ROLE_AUDIO_DECODER_MPGA_L3}, |
| 53 | {"audio_decoder.ddp" , MMALOMX_ROLE_AUDIO_DECODER_DDP}, |
| 54 | |
| 55 | {"AIV.play.101" , MMALOMX_ROLE_AIV_PLAY_101}, |
| 56 | {"play.avcddp" , MMALOMX_ROLE_AIV_PLAY_AVCDDP}, |
| 57 | |
| 58 | {0, 0} |
| 59 | }; |
| 60 | |
| 61 | const char *mmalomx_role_to_name(MMALOMX_ROLE_T role) |
| 62 | { |
| 63 | unsigned int i; |
| 64 | for (i = 0; mmalomx_roles[i].name; i++) |
| 65 | if (mmalomx_roles[i].role == role) |
| 66 | break; |
| 67 | return mmalomx_roles[i].name; |
| 68 | } |
| 69 | |
| 70 | MMALOMX_ROLE_T mmalomx_role_from_name(const char *name) |
| 71 | { |
| 72 | unsigned int i; |
| 73 | for (i = 0; mmalomx_roles[i].name; i++) |
| 74 | if (!strcmp(mmalomx_roles[i].name, name)) |
| 75 | break; |
| 76 | return mmalomx_roles[i].role; |
| 77 | } |
| 78 | |
| 79 | static void mmalomx_format_encoding_from_role(MMALOMX_ROLE_T role, |
| 80 | MMAL_FOURCC_T *encoding, MMAL_ES_TYPE_T *es_type, unsigned int *port) |
| 81 | { |
| 82 | switch (role) |
| 83 | { |
| 84 | case MMALOMX_ROLE_VIDEO_DECODER_MPEG4: |
| 85 | case MMALOMX_ROLE_VIDEO_ENCODER_MPEG4: |
| 86 | *encoding = MMAL_ENCODING_MP4V; |
| 87 | *es_type = MMAL_ES_TYPE_VIDEO; |
| 88 | break; |
| 89 | case MMALOMX_ROLE_VIDEO_DECODER_AVC: |
| 90 | case MMALOMX_ROLE_VIDEO_ENCODER_AVC: |
| 91 | *encoding = MMAL_ENCODING_H264; |
| 92 | *es_type = MMAL_ES_TYPE_VIDEO; |
| 93 | break; |
| 94 | case MMALOMX_ROLE_VIDEO_DECODER_MPEG2: |
| 95 | *encoding = MMAL_ENCODING_MP2V; |
| 96 | *es_type = MMAL_ES_TYPE_VIDEO; |
| 97 | break; |
| 98 | case MMALOMX_ROLE_VIDEO_DECODER_WMV: |
| 99 | *encoding = MMAL_ENCODING_WMV3; |
| 100 | *es_type = MMAL_ES_TYPE_VIDEO; |
| 101 | break; |
| 102 | case MMALOMX_ROLE_VIDEO_DECODER_VPX: |
| 103 | *encoding = MMAL_ENCODING_VP8; |
| 104 | *es_type = MMAL_ES_TYPE_VIDEO; |
| 105 | break; |
| 106 | case MMALOMX_ROLE_VIDEO_DECODER_H263: |
| 107 | case MMALOMX_ROLE_VIDEO_ENCODER_H263: |
| 108 | *encoding = MMAL_ENCODING_H263; |
| 109 | *es_type = MMAL_ES_TYPE_VIDEO; |
| 110 | break; |
| 111 | case MMALOMX_ROLE_AUDIO_DECODER_AAC: |
| 112 | *encoding = MMAL_ENCODING_MP4A; |
| 113 | *es_type = MMAL_ES_TYPE_AUDIO; |
| 114 | break; |
| 115 | case MMALOMX_ROLE_AUDIO_DECODER_MPGA_L1: |
| 116 | case MMALOMX_ROLE_AUDIO_DECODER_MPGA_L2: |
| 117 | case MMALOMX_ROLE_AUDIO_DECODER_MPGA_L3: |
| 118 | *encoding = MMAL_ENCODING_MPGA; |
| 119 | *es_type = MMAL_ES_TYPE_AUDIO; |
| 120 | break; |
| 121 | |
| 122 | case MMALOMX_ROLE_AUDIO_DECODER_DDP: |
| 123 | *encoding = MMAL_ENCODING_AC3; |
| 124 | *es_type = MMAL_ES_TYPE_AUDIO; |
| 125 | break; |
| 126 | |
| 127 | default: |
| 128 | *encoding = MMAL_ENCODING_UNKNOWN; |
| 129 | *es_type = MMAL_ES_TYPE_UNKNOWN; |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | switch (role) |
| 134 | { |
| 135 | case MMALOMX_ROLE_VIDEO_ENCODER_H263: |
| 136 | case MMALOMX_ROLE_VIDEO_ENCODER_MPEG4: |
| 137 | case MMALOMX_ROLE_VIDEO_ENCODER_AVC: |
| 138 | *port = 1; |
| 139 | break; |
| 140 | default: |
| 141 | *port = 0; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | OMX_ERRORTYPE mmalomx_role_set(MMALOMX_COMPONENT_T *component, const char *name) |
| 147 | { |
| 148 | const MMALOMX_ROLE_T role = mmalomx_role_from_name(name); |
| 149 | MMAL_FOURCC_T encoding = MMAL_ENCODING_UNKNOWN; |
| 150 | MMAL_ES_TYPE_T es_type = MMAL_ES_TYPE_UNKNOWN; |
| 151 | unsigned int port; |
| 152 | MMAL_ES_FORMAT_T *format; |
| 153 | |
| 154 | if (!role || !mmalomx_registry_component_supports_role(component->registry_id, role)) |
| 155 | return OMX_ErrorUnsupportedSetting; |
| 156 | |
| 157 | component->role = role; |
| 158 | |
| 159 | mmalomx_format_encoding_from_role(role, &encoding, &es_type, &port); |
| 160 | if (encoding == MMAL_ENCODING_UNKNOWN) |
| 161 | return OMX_ErrorNone; |
| 162 | |
| 163 | format = component->ports[port].mmal->format; |
| 164 | format->type = es_type; |
| 165 | format->encoding = encoding; |
| 166 | format->bitrate = 64000; |
| 167 | switch (es_type) |
| 168 | { |
| 169 | case MMAL_ES_TYPE_VIDEO: |
| 170 | format->es->video.width = 176; |
| 171 | format->es->video.height = 144; |
| 172 | format->es->video.frame_rate.num = 15; |
| 173 | format->es->video.frame_rate.den = 1; |
| 174 | break; |
| 175 | default: |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | switch (role) |
| 180 | { |
| 181 | case MMALOMX_ROLE_VIDEO_DECODER_H263: |
| 182 | case MMALOMX_ROLE_VIDEO_ENCODER_H263: |
| 183 | component->ports[port].format_param.h263.eProfile = OMX_VIDEO_H263ProfileBaseline; |
| 184 | component->ports[port].format_param.h263.eLevel = OMX_VIDEO_H263Level10; |
| 185 | component->ports[port].format_param.h263.bPLUSPTYPEAllowed = OMX_FALSE; |
| 186 | component->ports[port].format_param.h263.bForceRoundingTypeToZero = OMX_TRUE; |
| 187 | break; |
| 188 | case MMALOMX_ROLE_VIDEO_DECODER_MPEG4: |
| 189 | case MMALOMX_ROLE_VIDEO_ENCODER_MPEG4: |
| 190 | component->ports[port].format_param.mpeg4.eProfile = OMX_VIDEO_MPEG4ProfileSimple; |
| 191 | component->ports[port].format_param.mpeg4.eLevel = OMX_VIDEO_MPEG4Level1; |
| 192 | break; |
| 193 | case MMALOMX_ROLE_VIDEO_DECODER_AVC: |
| 194 | case MMALOMX_ROLE_VIDEO_ENCODER_AVC: |
| 195 | component->ports[port].format_param.avc.eProfile = OMX_VIDEO_AVCProfileBaseline; |
| 196 | component->ports[port].format_param.avc.eLevel = OMX_VIDEO_AVCLevel1; |
| 197 | break; |
| 198 | case MMALOMX_ROLE_VIDEO_DECODER_WMV: |
| 199 | component->ports[port].format_param.wmv.eFormat = OMX_VIDEO_WMVFormat9; |
| 200 | break; |
| 201 | default: |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | if (mmal_port_format_commit(component->ports[port].mmal) != MMAL_SUCCESS) |
| 206 | LOG_ERROR("failed to commit format to %s for role %s" , |
| 207 | component->ports[port].mmal->name, name); |
| 208 | |
| 209 | return OMX_ErrorNone; |
| 210 | } |
| 211 | |