| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2008, 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: bsettop_encode.c $ |
|---|
| 11 | * $brcm_Revision: 7 $ |
|---|
| 12 | * $brcm_Date: 9/11/08 12:38p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /BSEAV/api/src/nexus/bsettop_encode.c $ |
|---|
| 19 | * |
|---|
| 20 | * 7 9/11/08 12:38p shyam |
|---|
| 21 | * PR46778 : Add support for AAC audio encode from I2S |
|---|
| 22 | * |
|---|
| 23 | * 6 9/10/08 6:53p shyam |
|---|
| 24 | * PR46778 : Port brutus to B0 firmwre rel 2.2 |
|---|
| 25 | * |
|---|
| 26 | * 5 7/18/08 11:15a jrubio |
|---|
| 27 | * PR44849: fix TRANSCODE isue |
|---|
| 28 | * |
|---|
| 29 | * 4 7/17/08 4:36p shyam |
|---|
| 30 | * PR43426 : Add 7043 656 Encode support |
|---|
| 31 | * |
|---|
| 32 | * 3 12/20/07 10:28a erickson |
|---|
| 33 | * PR37590: fix warnings |
|---|
| 34 | * |
|---|
| 35 | * 2 10/16/07 12:35p erickson |
|---|
| 36 | * PR36068: brutus up over settop api/nexus |
|---|
| 37 | * |
|---|
| 38 | ***************************************************************************/ |
|---|
| 39 | #include "bsettop_impl.h" |
|---|
| 40 | |
|---|
| 41 | BDBG_MODULE(encode); |
|---|
| 42 | |
|---|
| 43 | #ifdef B_HAS_TRANSCODE |
|---|
| 44 | struct bencode g_encode[B_MAX_ENCODES] ; |
|---|
| 45 | #endif |
|---|
| 46 | |
|---|
| 47 | bencode_t bencode_open(bobject_t id) |
|---|
| 48 | { |
|---|
| 49 | #ifdef B_HAS_TRANSCODE |
|---|
| 50 | bencode_t encode ; |
|---|
| 51 | |
|---|
| 52 | unsigned index = B_ID_GET_INDEX(id); |
|---|
| 53 | if (index >= B_MAX_ENCODES) |
|---|
| 54 | { |
|---|
| 55 | BSETTOP_ERROR(berr_not_available); |
|---|
| 56 | return NULL; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | encode = &g_encode[index]; |
|---|
| 60 | |
|---|
| 61 | return encode ; |
|---|
| 62 | #else |
|---|
| 63 | BSTD_UNUSED(id); |
|---|
| 64 | BSETTOP_ERROR(berr_not_supported); |
|---|
| 65 | return NULL; |
|---|
| 66 | #endif |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | void bencode_close(bencode_t encode) |
|---|
| 70 | { |
|---|
| 71 | #ifdef B_HAS_TRANSCODE |
|---|
| 72 | BSTD_UNUSED(encode); |
|---|
| 73 | #else |
|---|
| 74 | BSTD_UNUSED(encode); |
|---|
| 75 | BSETTOP_ERROR(berr_not_supported); |
|---|
| 76 | #endif |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | void bencode_extended_settings_init(bencode_settings *settings, bencode_quality quality, bencode_t encode, bencode_vpp_mode vpp_mode) |
|---|
| 80 | { |
|---|
| 81 | #ifdef B_HAS_TRANSCODE |
|---|
| 82 | BSTD_UNUSED(quality) ; |
|---|
| 83 | BSTD_UNUSED(encode) ; |
|---|
| 84 | BSTD_UNUSED(vpp_mode); |
|---|
| 85 | |
|---|
| 86 | settings->video_pid = 0x21 ; |
|---|
| 87 | settings->video_width = 720 ; |
|---|
| 88 | settings->video_height = 480 ; |
|---|
| 89 | settings->video_bitrate = 30000000 ; |
|---|
| 90 | settings->video_format = bvideo_codec_mpeg2; |
|---|
| 91 | settings->framerate = bvideo_frame_rate_29_97 ; |
|---|
| 92 | settings->gop_structure = bencode_gop_structure_ip ; |
|---|
| 93 | settings->progressive = false ; |
|---|
| 94 | settings->transport_bitrate = 35000000 ; |
|---|
| 95 | |
|---|
| 96 | settings->audio_pid = 0x24 ; |
|---|
| 97 | settings->audio_bitrate = 100000 ; |
|---|
| 98 | settings->audio_type = baudio_format_aac; |
|---|
| 99 | |
|---|
| 100 | #else |
|---|
| 101 | BSTD_UNUSED(settings); |
|---|
| 102 | BSTD_UNUSED(quality); |
|---|
| 103 | BSTD_UNUSED(encode); |
|---|
| 104 | BSTD_UNUSED(vpp_mode); |
|---|
| 105 | BSETTOP_ERROR(berr_not_supported); |
|---|
| 106 | #endif |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void bencode_settings_init(bencode_settings *settings, bencode_quality quality, bencode_t encode) |
|---|
| 110 | { |
|---|
| 111 | #ifdef B_HAS_TRANSCODE |
|---|
| 112 | BSTD_UNUSED(quality) ; |
|---|
| 113 | BSTD_UNUSED(encode) ; |
|---|
| 114 | settings->video_pid = 0x21 ; |
|---|
| 115 | settings->video_width = 720 ; |
|---|
| 116 | settings->video_height = 480 ; |
|---|
| 117 | settings->video_bitrate = 30000000 ; |
|---|
| 118 | settings->video_format = bvideo_codec_mpeg2; |
|---|
| 119 | settings->framerate = bvideo_frame_rate_29_97 ; |
|---|
| 120 | settings->gop_structure = bencode_gop_structure_ip ; |
|---|
| 121 | settings->progressive = false ; |
|---|
| 122 | settings->transport_bitrate = 35000000 ; |
|---|
| 123 | |
|---|
| 124 | settings->audio_pid = 0x24 ; |
|---|
| 125 | settings->audio_bitrate = 100000 ; |
|---|
| 126 | settings->audio_type = baudio_format_aac; |
|---|
| 127 | |
|---|
| 128 | #else |
|---|
| 129 | BSTD_UNUSED(settings); |
|---|
| 130 | BSTD_UNUSED(quality); |
|---|
| 131 | BSTD_UNUSED(encode); |
|---|
| 132 | BSETTOP_ERROR(berr_not_supported); |
|---|
| 133 | #endif |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | #ifdef B_HAS_TRANSCODE |
|---|
| 137 | |
|---|
| 138 | static bresult bencode_p_start_video_encode( |
|---|
| 139 | bencode_t encode, /* handle returned by bencode_open */ |
|---|
| 140 | bstream_t stream /* source for the decode, either analog or digital */ |
|---|
| 141 | ) |
|---|
| 142 | { |
|---|
| 143 | NEXUS_Transcoder_OutputConfiguration portConfig ; |
|---|
| 144 | NEXUS_Transcoder_OutputChannel outputPort ; |
|---|
| 145 | NEXUS_Transcoder_OpenVideoEncodeSettings openSettings ; |
|---|
| 146 | NEXUS_VideoCodec outputVideoCodec ; |
|---|
| 147 | |
|---|
| 148 | /* Transcoder stuff */ |
|---|
| 149 | BKNI_Memset( &portConfig, 0 , sizeof(portConfig) ); |
|---|
| 150 | portConfig.transportBitrate = encode->settings.transport_bitrate ; |
|---|
| 151 | outputPort.outputChNum = 0; |
|---|
| 152 | outputPort.outputType = |
|---|
| 153 | NEXUS_Transcoder_OutputChannelType_eTxSerialOut ; |
|---|
| 154 | encode->hPortHandle = NEXUS_Transcoder_OpenOutputPort(outputPort, &portConfig); |
|---|
| 155 | BDBG_ERR(("Port handle = %p", encode->hPortHandle)); |
|---|
| 156 | if(NULL==encode->hPortHandle) |
|---|
| 157 | { |
|---|
| 158 | return BSETTOP_ERROR(berr_invalid_state) ; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | BKNI_Memset( &openSettings, 0 , sizeof(openSettings) ); |
|---|
| 162 | openSettings.bRealTime = true ; |
|---|
| 163 | openSettings.inputType = NEXUS_Transcoder_VideoInputChannelType_eITUBT656 ; |
|---|
| 164 | openSettings.inputChNum = stream->producer.linein->vdec7043DeviceNo ; |
|---|
| 165 | openSettings.videoformat = NEXUS_VideoFormat_eNtsc ; |
|---|
| 166 | openSettings.bFmtAutoDetect = true; |
|---|
| 167 | /* Video Encode/Output Settings */ |
|---|
| 168 | openSettings.outputVideoCodec = outputVideoCodec = |
|---|
| 169 | b_videocodec2nexus(encode->settings.video_format) ; |
|---|
| 170 | openSettings.outputStreamType = NEXUS_TransportType_eTs ; |
|---|
| 171 | openSettings.outputPort = encode->hPortHandle ; |
|---|
| 172 | if( NEXUS_VideoCodec_eH264 == outputVideoCodec ) |
|---|
| 173 | { |
|---|
| 174 | openSettings.codingSettings.avcCoding.h264Level = NEXUS_Transcoder_H264Level_eL4 ; |
|---|
| 175 | openSettings.codingSettings.avcCoding.h264Profile = NEXUS_Transcoder_H264Profile_eMain ; |
|---|
| 176 | } |
|---|
| 177 | else if( NEXUS_VideoCodec_eMpeg2 == outputVideoCodec ) |
|---|
| 178 | { |
|---|
| 179 | openSettings.codingSettings.mpegCoding.mpegLevel = NEXUS_Transcoder_MPEGLevel_eMain ; |
|---|
| 180 | openSettings.codingSettings.mpegCoding.mpegProfile = NEXUS_Transcoder_MPEGProfile_eMain ; |
|---|
| 181 | } |
|---|
| 182 | else if( NEXUS_VideoCodec_eMpeg4Part2 == outputVideoCodec ) |
|---|
| 183 | { |
|---|
| 184 | openSettings.codingSettings.mpeg4Coding.mpeg4Level = NEXUS_Transcoder_MPEG4Level_e1; |
|---|
| 185 | openSettings.codingSettings.mpeg4Coding.mpeg4Profile = NEXUS_Transcoder_MPEG4Profile_eSimple; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /** Transport Settings */ |
|---|
| 189 | openSettings.outputStreamId = encode->settings.video_pid ; |
|---|
| 190 | openSettings.esPeakRateControl = NEXUS_Transcoder_PeakRateControl_eConstantQp ; |
|---|
| 191 | openSettings.esTargetBitrate = encode->settings.video_bitrate; /* Bits per Second */ |
|---|
| 192 | |
|---|
| 193 | openSettings.transportBitrate = encode->settings.transport_bitrate; /* Bits per Second */ |
|---|
| 194 | |
|---|
| 195 | openSettings.outWidth = encode->settings.video_width ; |
|---|
| 196 | openSettings.outHeight = encode->settings.video_height ; |
|---|
| 197 | |
|---|
| 198 | openSettings.bProgressive = encode->settings.progressive; |
|---|
| 199 | |
|---|
| 200 | /** VBI Settings */ |
|---|
| 201 | openSettings.enableVbi = false ; |
|---|
| 202 | |
|---|
| 203 | encode->nexus_encoder = NEXUS_Transcoder_OpenVideoEncode(&openSettings) ; |
|---|
| 204 | BDBG_ERR(("Open encoder handle = %p", encode->nexus_encoder)); |
|---|
| 205 | if(NULL == encode->nexus_encoder) |
|---|
| 206 | { |
|---|
| 207 | return BSETTOP_ERROR(berr_invalid_state) ; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | NEXUS_Transcoder_Start(encode->nexus_encoder) ; |
|---|
| 211 | |
|---|
| 212 | return b_ok ; |
|---|
| 213 | |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | static bresult bencode_p_start_audio_encode( |
|---|
| 217 | bencode_t encode /* handle returned by bencode_open */ |
|---|
| 218 | ) |
|---|
| 219 | { |
|---|
| 220 | NEXUS_Error rc ; |
|---|
| 221 | NEXUS_Transcoder_OpenAudioEncodeSettings openSettings ; |
|---|
| 222 | |
|---|
| 223 | /* Transcoder stuff */ |
|---|
| 224 | BKNI_Memset( &openSettings, 0 , sizeof(openSettings) ); |
|---|
| 225 | openSettings.bRealTime = true ; |
|---|
| 226 | openSettings.inputType = NEXUS_Transcoder_AudioInputChannelType_eI2S ; |
|---|
| 227 | openSettings.inputChNum = 0 ; |
|---|
| 228 | openSettings.inputSamplingRate = 48000 ; |
|---|
| 229 | |
|---|
| 230 | /* Audio Encode/Output Settings */ |
|---|
| 231 | openSettings.audioCodec = b_audiocodec2nexus(encode->settings.audio_type) ; |
|---|
| 232 | openSettings.outSamplingRate = 48000 ; |
|---|
| 233 | openSettings.outputStreamType = NEXUS_TransportType_eTs ; |
|---|
| 234 | openSettings.outputPort = encode->hPortHandle ; |
|---|
| 235 | /* Transport Settings */ |
|---|
| 236 | openSettings.outputStreamId = encode->settings.audio_pid ; |
|---|
| 237 | openSettings.transportBitrate = encode->settings.audio_bitrate; /* Bits per Second */ |
|---|
| 238 | |
|---|
| 239 | encode->nexus_audEncoder = NEXUS_Transcoder_OpenAudioEncode(&openSettings) ; |
|---|
| 240 | rc = NEXUS_Transcoder_Start(encode->nexus_audEncoder); |
|---|
| 241 | if(rc) |
|---|
| 242 | return BSETTOP_ERROR(berr_invalid_state) ; |
|---|
| 243 | return b_ok ; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | #endif |
|---|
| 247 | bstream_t bencode_start(bencode_t encode, bstream_t stream, const bencode_settings *settings) |
|---|
| 248 | { |
|---|
| 249 | #ifdef B_HAS_TRANSCODE |
|---|
| 250 | bresult brc ; |
|---|
| 251 | bband_t band; |
|---|
| 252 | bstream_t dest ; |
|---|
| 253 | bstream_mpeg mpeg; |
|---|
| 254 | |
|---|
| 255 | encode->settings = *settings ; |
|---|
| 256 | brc = bencode_p_start_video_encode(encode, stream); |
|---|
| 257 | if(brc != b_ok) goto error ; |
|---|
| 258 | |
|---|
| 259 | brc = bencode_p_start_audio_encode(encode); |
|---|
| 260 | if(brc != b_ok) goto error ; |
|---|
| 261 | |
|---|
| 262 | /* Assume Transcoder is connected to IB2 for now */ |
|---|
| 263 | band = bstreamer_attach( B_ID(102), bstream_mpeg_type_ts); |
|---|
| 264 | if (!band) goto error; |
|---|
| 265 | |
|---|
| 266 | bstream_mpeg_init(&mpeg); |
|---|
| 267 | mpeg.video[0].pid = encode->settings.video_pid; |
|---|
| 268 | mpeg.video[0].format = encode->settings.video_format; |
|---|
| 269 | |
|---|
| 270 | mpeg.audio[0].pid = encode->settings.audio_pid; |
|---|
| 271 | mpeg.audio[0].format = encode->settings.audio_type; |
|---|
| 272 | mpeg.pcr_pid = encode->settings.video_pid; |
|---|
| 273 | |
|---|
| 274 | dest = bstream_open(band, &mpeg); |
|---|
| 275 | if (!dest) goto error; |
|---|
| 276 | |
|---|
| 277 | return dest ; |
|---|
| 278 | error : |
|---|
| 279 | return NULL ; |
|---|
| 280 | #else |
|---|
| 281 | BSTD_UNUSED(encode); |
|---|
| 282 | BSTD_UNUSED(stream); |
|---|
| 283 | BSTD_UNUSED(settings); |
|---|
| 284 | BSETTOP_ERROR(berr_not_supported); |
|---|
| 285 | return NULL; |
|---|
| 286 | #endif |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | void bencode_stop(bencode_t encode, bstream_t stream) |
|---|
| 290 | { |
|---|
| 291 | #ifdef B_HAS_TRANSCODE |
|---|
| 292 | BSTD_UNUSED(encode); |
|---|
| 293 | BSTD_UNUSED(stream); |
|---|
| 294 | |
|---|
| 295 | #else |
|---|
| 296 | BSTD_UNUSED(encode); |
|---|
| 297 | BSTD_UNUSED(stream); |
|---|
| 298 | BSETTOP_ERROR(berr_not_supported); |
|---|
| 299 | #endif |
|---|
| 300 | } |
|---|
| 301 | |
|---|