| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2012, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: $ |
|---|
| 11 | * $brcm_Revision: $ |
|---|
| 12 | * $brcm_Date: $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: $ |
|---|
| 19 | * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | |
|---|
| 22 | #include "bsettop_stream_n_priv.h" |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | BDBG_MODULE(bstream); |
|---|
| 26 | |
|---|
| 27 | #define INVALID_PID 0xFFFF |
|---|
| 28 | |
|---|
| 29 | /* |
|---|
| 30 | * Summary: |
|---|
| 31 | * Private stream data structure to associate rave and context info with the stream. |
|---|
| 32 | */ |
|---|
| 33 | struct bstream |
|---|
| 34 | { |
|---|
| 35 | bool in_use; /* flag indicating the context is in use */ |
|---|
| 36 | int pid_ch; /* Main pid channel index */ |
|---|
| 37 | bband_t band; /* Band with possibly playback flag */ |
|---|
| 38 | bstream_mpeg mpeg; /* mpeg program pids */ |
|---|
| 39 | bstream_info_t stream_info; /* private stream parameters */ |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | static bstream_t bstream_p_find_stream(void); |
|---|
| 43 | |
|---|
| 44 | static struct bstream s_streams[BSETTOP_MAX_STREAMS]; |
|---|
| 45 | |
|---|
| 46 | static bstream_mpeg s_def_stream_mpeg = { {{INVALID_PID,0}}, {{INVALID_PID,0}}, INVALID_PID}; |
|---|
| 47 | static bool s_bstream_init = false; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | /* |
|---|
| 51 | * Summary |
|---|
| 52 | * Module initialization function |
|---|
| 53 | */ |
|---|
| 54 | void bstream_init(NEXUS_VideoDecoderHandle videoDecodeHandle) |
|---|
| 55 | { |
|---|
| 56 | int i; |
|---|
| 57 | if (s_bstream_init) |
|---|
| 58 | return; |
|---|
| 59 | |
|---|
| 60 | for (i=0; i<BSETTOP_MAX_STREAMS; ++i) |
|---|
| 61 | { |
|---|
| 62 | BKNI_Memset(&s_streams[i], 0, sizeof(struct bstream)); |
|---|
| 63 | s_streams[i].in_use = false; |
|---|
| 64 | s_streams[i].pid_ch = i; |
|---|
| 65 | s_streams[i].band = DEF_PARSER_BAND; |
|---|
| 66 | s_streams[i].mpeg.video[0].pid = s_streams[i].mpeg.audio[0].pid = s_streams[i].mpeg.pcr_pid = INVALID_PID; |
|---|
| 67 | s_streams[i].stream_info.videoDecoder = videoDecodeHandle; |
|---|
| 68 | } |
|---|
| 69 | s_bstream_init = true; |
|---|
| 70 | return; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /* |
|---|
| 74 | * Summary: |
|---|
| 75 | * Reset mpeg program structure to the default. |
|---|
| 76 | */ |
|---|
| 77 | void bstream_mpeg_init(bstream_mpeg *mpeg) |
|---|
| 78 | { |
|---|
| 79 | *mpeg = s_def_stream_mpeg; |
|---|
| 80 | return; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | bstream_t bstream_open(bband_t band, const bstream_mpeg *mpeg) |
|---|
| 84 | { |
|---|
| 85 | bstream_t stream; |
|---|
| 86 | NEXUS_StcChannelSettings stcSettings; |
|---|
| 87 | |
|---|
| 88 | BDBG_ASSERT(mpeg); |
|---|
| 89 | |
|---|
| 90 | stream = bstream_p_find_stream(); |
|---|
| 91 | if (!stream) { |
|---|
| 92 | BDBG_WRN(("%s no more stream available.", __FUNCTION__)); |
|---|
| 93 | goto ExitError; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | stream->band = band; |
|---|
| 97 | stream->mpeg = *mpeg; |
|---|
| 98 | stream->stream_info.pcrPidChannel = NULL; |
|---|
| 99 | |
|---|
| 100 | if (stream->mpeg.video[0].pid != 0) |
|---|
| 101 | { |
|---|
| 102 | stream->stream_info.videoPidChannel = NEXUS_PidChannel_Open(stream->band, stream->mpeg.video[0].pid, NULL); |
|---|
| 103 | if (!stream->stream_info.videoPidChannel) |
|---|
| 104 | { |
|---|
| 105 | BDBG_WRN(("NEXUS_PidChannel_Open video pid = 0x%04x", stream->mpeg.video[0].pid)); |
|---|
| 106 | goto ExitError; |
|---|
| 107 | } |
|---|
| 108 | if (stream->mpeg.pcr_pid == stream->mpeg.video[0].pid) { |
|---|
| 109 | stream->stream_info.pcrPidChannel = stream->stream_info.videoPidChannel; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | if (stream->mpeg.audio[0].pid != 0) |
|---|
| 113 | { |
|---|
| 114 | stream->stream_info.audioPidChannel = NEXUS_PidChannel_Open(stream->band, stream->mpeg.audio[0].pid, NULL); |
|---|
| 115 | if (!stream->stream_info.audioPidChannel) |
|---|
| 116 | { |
|---|
| 117 | BDBG_WRN(("NEXUS_PidChannel_Open audio pid = 0x%04x", stream->mpeg.audio[0].pid)); |
|---|
| 118 | goto ExitError; |
|---|
| 119 | } |
|---|
| 120 | if (stream->mpeg.pcr_pid == stream->mpeg.audio[0].pid) { |
|---|
| 121 | stream->stream_info.pcrPidChannel = stream->stream_info.audioPidChannel; |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if ((stream->mpeg.pcr_pid != 0) && (stream->stream_info.pcrPidChannel == NULL)) |
|---|
| 126 | { |
|---|
| 127 | stream->stream_info.pcrPidChannel = NEXUS_PidChannel_Open(stream->band, stream->mpeg.pcr_pid, NULL); |
|---|
| 128 | if (!stream->stream_info.pcrPidChannel) |
|---|
| 129 | { |
|---|
| 130 | BDBG_WRN(("NEXUS_PidChannel_Open pcr pid = 0x%04x", stream->mpeg.pcr_pid)); |
|---|
| 131 | goto ExitError; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | if (!stream->stream_info.pcrPidChannel) |
|---|
| 136 | stream->stream_info.pcrPidChannel = stream->stream_info.videoPidChannel; |
|---|
| 137 | |
|---|
| 138 | NEXUS_StcChannel_GetDefaultSettings(0, &stcSettings); |
|---|
| 139 | stcSettings.autoConfigTimebase = false; |
|---|
| 140 | stcSettings.timebase = NEXUS_Timebase_e0; |
|---|
| 141 | stcSettings.mode = NEXUS_StcChannelMode_ePcr; |
|---|
| 142 | stcSettings.modeSettings.pcr.pidChannel = stream->stream_info.pcrPidChannel; |
|---|
| 143 | stcSettings.modeSettings.pcr.offsetThreshold = 0; |
|---|
| 144 | stcSettings.stcIndex = 0; |
|---|
| 145 | stcSettings.modeSettings.Auto.behavior = NEXUS_StcChannelAutoModeBehavior_eFirstAvailable; |
|---|
| 146 | stcSettings.modeSettings.Auto.transportType = NEXUS_TransportType_eTs; |
|---|
| 147 | stream->stream_info.stcChannel = NEXUS_StcChannel_Open(stream->pid_ch, &stcSettings); |
|---|
| 148 | |
|---|
| 149 | if (!stream->stream_info.stcChannel) { |
|---|
| 150 | BDBG_WRN(("NEXUS_StcChannel_Open failed (%d)", stream->pid_ch)); |
|---|
| 151 | goto ExitError; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | if (stream->mpeg.video[0].pid) |
|---|
| 155 | { |
|---|
| 156 | NEXUS_VideoDecoder_GetDefaultStartSettings(&(stream->stream_info.decodeSettings)); |
|---|
| 157 | stream->stream_info.decodeSettings.codec = stream->mpeg.video[0].format; |
|---|
| 158 | stream->stream_info.decodeSettings.pidChannel = stream->stream_info.videoPidChannel; |
|---|
| 159 | stream->stream_info.decodeSettings.stcChannel = stream->stream_info.stcChannel; |
|---|
| 160 | stream->stream_info.decodeSettings.prerollRate = 1; |
|---|
| 161 | stream->stream_info.primerHandle = NEXUS_VideoDecoder_OpenPrimer(stream->stream_info.videoDecoder); |
|---|
| 162 | if (!stream->stream_info.primerHandle) { |
|---|
| 163 | BDBG_WRN(("NEXUS_VideoDecoder_OpenPrimer failed %d", stream->pid_ch)); |
|---|
| 164 | goto ExitError; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | BDBG_ASSERT(stream->stream_info.decodeSettings.stcChannel); |
|---|
| 168 | BDBG_ERR(("NEXUS_VideoDecoder_StartPrimer (0x%08x)", stream->mpeg.video[0].pid)); |
|---|
| 169 | NEXUS_VideoDecoder_StartPrimer(stream->stream_info.videoDecoder, |
|---|
| 170 | stream->stream_info.primerHandle, &(stream->stream_info.decodeSettings)); |
|---|
| 171 | } |
|---|
| 172 | stream->in_use = true; |
|---|
| 173 | |
|---|
| 174 | return stream; |
|---|
| 175 | |
|---|
| 176 | ExitError: |
|---|
| 177 | if (stream) |
|---|
| 178 | bstream_close(stream); |
|---|
| 179 | return NULL; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | void bstream_close(bstream_t stream) |
|---|
| 183 | { |
|---|
| 184 | BDBG_ASSERT(stream); |
|---|
| 185 | BDBG_ASSERT(stream->stream_info.videoDecoder); |
|---|
| 186 | |
|---|
| 187 | if (stream->stream_info.primerHandle) { |
|---|
| 188 | NEXUS_VideoDecoder_StopPrimer(stream->stream_info.videoDecoder, stream->stream_info.primerHandle); |
|---|
| 189 | NEXUS_VideoDecoder_ClosePrimer(stream->stream_info.videoDecoder, stream->stream_info.primerHandle); |
|---|
| 190 | stream->stream_info.primerHandle = NULL; |
|---|
| 191 | } |
|---|
| 192 | if (stream->stream_info.stcChannel) |
|---|
| 193 | { |
|---|
| 194 | NEXUS_StcChannel_Close(stream->stream_info.stcChannel); |
|---|
| 195 | stream->stream_info.stcChannel = NULL; |
|---|
| 196 | } |
|---|
| 197 | if ((stream->stream_info.pcrPidChannel) && |
|---|
| 198 | (stream->stream_info.pcrPidChannel != stream->stream_info.videoPidChannel) && |
|---|
| 199 | (stream->stream_info.pcrPidChannel != stream->stream_info.audioPidChannel)) |
|---|
| 200 | { |
|---|
| 201 | NEXUS_PidChannel_Close(stream->stream_info.pcrPidChannel); |
|---|
| 202 | stream->stream_info.pcrPidChannel = NULL; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | if (stream->stream_info.videoPidChannel) |
|---|
| 206 | { |
|---|
| 207 | NEXUS_PidChannel_Close(stream->stream_info.videoPidChannel); |
|---|
| 208 | stream->stream_info.videoPidChannel = NULL; |
|---|
| 209 | } |
|---|
| 210 | if (stream->stream_info.audioPidChannel) |
|---|
| 211 | { |
|---|
| 212 | NEXUS_PidChannel_Close(stream->stream_info.audioPidChannel); |
|---|
| 213 | stream->stream_info.audioPidChannel = NULL; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | stream->in_use = false; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | bresult bstream_get_status(bstream_t stream, bstream_status *status) |
|---|
| 220 | { |
|---|
| 221 | BDBG_ASSERT(stream); |
|---|
| 222 | BDBG_ASSERT(status); |
|---|
| 223 | |
|---|
| 224 | BKNI_Memset(status, 0, sizeof(bstream_status)); |
|---|
| 225 | status->mpeg = stream->mpeg; |
|---|
| 226 | status->band = stream->band; |
|---|
| 227 | |
|---|
| 228 | return b_ok; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | bstream_info_t *bstream_get_info(bstream_t stream) |
|---|
| 232 | { |
|---|
| 233 | return &(stream->stream_info); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | /* |
|---|
| 237 | * Summary: |
|---|
| 238 | * Find available stream. |
|---|
| 239 | */ |
|---|
| 240 | static bstream_t bstream_p_find_stream(void) |
|---|
| 241 | { |
|---|
| 242 | int i; |
|---|
| 243 | for (i=0; i<BSETTOP_MAX_STREAMS; ++i) |
|---|
| 244 | { |
|---|
| 245 | if (!s_streams[i].in_use) |
|---|
| 246 | return (bstream_t)&s_streams[i]; |
|---|
| 247 | } |
|---|
| 248 | return NULL; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | void bstream_update_mpeg(bstream_t stream, const bstream_mpeg *mpeg) |
|---|
| 252 | { |
|---|
| 253 | BDBG_ASSERT(stream); |
|---|
| 254 | BDBG_ASSERT(mpeg); |
|---|
| 255 | |
|---|
| 256 | stream->mpeg = *mpeg; |
|---|
| 257 | } |
|---|