Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "OMXCodec" |
| 19 | #include <utils/Log.h> |
| 20 | |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 21 | #include "include/AACDecoder.h" |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 22 | #include "include/AACEncoder.h" |
Andreas Huber | a30d400 | 2009-12-08 15:40:06 -0800 | [diff] [blame] | 23 | #include "include/AMRNBDecoder.h" |
Andreas Huber | d49b526dd | 2009-12-11 15:07:25 -0800 | [diff] [blame] | 24 | #include "include/AMRNBEncoder.h" |
Andreas Huber | a30d400 | 2009-12-08 15:40:06 -0800 | [diff] [blame] | 25 | #include "include/AMRWBDecoder.h" |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 26 | #include "include/AMRWBEncoder.h" |
Andreas Huber | 4a0ec3f | 2009-12-10 09:44:29 -0800 | [diff] [blame] | 27 | #include "include/AVCDecoder.h" |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 28 | #include "include/AVCEncoder.h" |
Andreas Huber | 520b2a7 | 2010-08-09 09:54:59 -0700 | [diff] [blame] | 29 | #include "include/G711Decoder.h" |
James Dong | 02f5b54 | 2009-12-15 16:26:55 -0800 | [diff] [blame] | 30 | #include "include/M4vH263Decoder.h" |
James Dong | 42ef0c7 | 2010-07-12 21:46:25 -0700 | [diff] [blame] | 31 | #include "include/M4vH263Encoder.h" |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 32 | #include "include/MP3Decoder.h" |
Andreas Huber | 388379f | 2010-05-07 10:35:13 -0700 | [diff] [blame] | 33 | #include "include/VorbisDecoder.h" |
Andreas Huber | 47ba30e | 2010-05-24 14:38:02 -0700 | [diff] [blame] | 34 | #include "include/VPXDecoder.h" |
Andreas Huber | 8c7ab03 | 2009-12-07 11:23:44 -0800 | [diff] [blame] | 35 | |
Andreas Huber | bd7b43b | 2009-10-13 10:22:55 -0700 | [diff] [blame] | 36 | #include "include/ESDS.h" |
| 37 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 38 | #include <binder/IServiceManager.h> |
| 39 | #include <binder/MemoryDealer.h> |
| 40 | #include <binder/ProcessState.h> |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 41 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 42 | #include <media/IMediaPlayerService.h> |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 43 | #include <media/stagefright/HardwareAPI.h> |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 44 | #include <media/stagefright/MediaBuffer.h> |
| 45 | #include <media/stagefright/MediaBufferGroup.h> |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 46 | #include <media/stagefright/MediaDefs.h> |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 47 | #include <media/stagefright/MediaExtractor.h> |
| 48 | #include <media/stagefright/MetaData.h> |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 49 | #include <media/stagefright/OMXCodec.h> |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 50 | #include <media/stagefright/Utils.h> |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 51 | #include <utils/Vector.h> |
| 52 | |
| 53 | #include <OMX_Audio.h> |
| 54 | #include <OMX_Component.h> |
| 55 | |
Andreas Huber | 8946ab2 | 2010-09-15 16:20:42 -0700 | [diff] [blame] | 56 | #include "include/ThreadedSource.h" |
Andreas Huber | f7e2e31 | 2010-11-15 09:01:13 -0800 | [diff] [blame] | 57 | #include "include/avc_utils.h" |
Andreas Huber | 8946ab2 | 2010-09-15 16:20:42 -0700 | [diff] [blame] | 58 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 59 | namespace android { |
| 60 | |
Andreas Huber | 8b432b1 | 2009-10-07 13:36:52 -0700 | [diff] [blame] | 61 | static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00; |
| 62 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 63 | struct CodecInfo { |
| 64 | const char *mime; |
| 65 | const char *codec; |
| 66 | }; |
| 67 | |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 68 | #define FACTORY_CREATE(name) \ |
| 69 | static sp<MediaSource> Make##name(const sp<MediaSource> &source) { \ |
| 70 | return new name(source); \ |
| 71 | } |
| 72 | |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 73 | #define FACTORY_CREATE_ENCODER(name) \ |
| 74 | static sp<MediaSource> Make##name(const sp<MediaSource> &source, const sp<MetaData> &meta) { \ |
| 75 | return new name(source, meta); \ |
| 76 | } |
| 77 | |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 78 | #define FACTORY_REF(name) { #name, Make##name }, |
| 79 | |
| 80 | FACTORY_CREATE(MP3Decoder) |
| 81 | FACTORY_CREATE(AMRNBDecoder) |
| 82 | FACTORY_CREATE(AMRWBDecoder) |
| 83 | FACTORY_CREATE(AACDecoder) |
| 84 | FACTORY_CREATE(AVCDecoder) |
Andreas Huber | 520b2a7 | 2010-08-09 09:54:59 -0700 | [diff] [blame] | 85 | FACTORY_CREATE(G711Decoder) |
James Dong | 02f5b54 | 2009-12-15 16:26:55 -0800 | [diff] [blame] | 86 | FACTORY_CREATE(M4vH263Decoder) |
Andreas Huber | 388379f | 2010-05-07 10:35:13 -0700 | [diff] [blame] | 87 | FACTORY_CREATE(VorbisDecoder) |
Andreas Huber | 47ba30e | 2010-05-24 14:38:02 -0700 | [diff] [blame] | 88 | FACTORY_CREATE(VPXDecoder) |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 89 | FACTORY_CREATE_ENCODER(AMRNBEncoder) |
| 90 | FACTORY_CREATE_ENCODER(AMRWBEncoder) |
| 91 | FACTORY_CREATE_ENCODER(AACEncoder) |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 92 | FACTORY_CREATE_ENCODER(AVCEncoder) |
James Dong | 42ef0c7 | 2010-07-12 21:46:25 -0700 | [diff] [blame] | 93 | FACTORY_CREATE_ENCODER(M4vH263Encoder) |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 94 | |
| 95 | static sp<MediaSource> InstantiateSoftwareEncoder( |
| 96 | const char *name, const sp<MediaSource> &source, |
| 97 | const sp<MetaData> &meta) { |
| 98 | struct FactoryInfo { |
| 99 | const char *name; |
| 100 | sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &, const sp<MetaData> &); |
| 101 | }; |
| 102 | |
| 103 | static const FactoryInfo kFactoryInfo[] = { |
| 104 | FACTORY_REF(AMRNBEncoder) |
| 105 | FACTORY_REF(AMRWBEncoder) |
| 106 | FACTORY_REF(AACEncoder) |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 107 | FACTORY_REF(AVCEncoder) |
James Dong | 42ef0c7 | 2010-07-12 21:46:25 -0700 | [diff] [blame] | 108 | FACTORY_REF(M4vH263Encoder) |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 109 | }; |
| 110 | for (size_t i = 0; |
| 111 | i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) { |
| 112 | if (!strcmp(name, kFactoryInfo[i].name)) { |
| 113 | return (*kFactoryInfo[i].CreateFunc)(source, meta); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return NULL; |
| 118 | } |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 119 | |
| 120 | static sp<MediaSource> InstantiateSoftwareCodec( |
| 121 | const char *name, const sp<MediaSource> &source) { |
| 122 | struct FactoryInfo { |
| 123 | const char *name; |
| 124 | sp<MediaSource> (*CreateFunc)(const sp<MediaSource> &); |
| 125 | }; |
| 126 | |
| 127 | static const FactoryInfo kFactoryInfo[] = { |
| 128 | FACTORY_REF(MP3Decoder) |
| 129 | FACTORY_REF(AMRNBDecoder) |
| 130 | FACTORY_REF(AMRWBDecoder) |
| 131 | FACTORY_REF(AACDecoder) |
| 132 | FACTORY_REF(AVCDecoder) |
Andreas Huber | 520b2a7 | 2010-08-09 09:54:59 -0700 | [diff] [blame] | 133 | FACTORY_REF(G711Decoder) |
James Dong | 02f5b54 | 2009-12-15 16:26:55 -0800 | [diff] [blame] | 134 | FACTORY_REF(M4vH263Decoder) |
Andreas Huber | 388379f | 2010-05-07 10:35:13 -0700 | [diff] [blame] | 135 | FACTORY_REF(VorbisDecoder) |
Andreas Huber | 47ba30e | 2010-05-24 14:38:02 -0700 | [diff] [blame] | 136 | FACTORY_REF(VPXDecoder) |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 137 | }; |
| 138 | for (size_t i = 0; |
| 139 | i < sizeof(kFactoryInfo) / sizeof(kFactoryInfo[0]); ++i) { |
| 140 | if (!strcmp(name, kFactoryInfo[i].name)) { |
Andreas Huber | 8946ab2 | 2010-09-15 16:20:42 -0700 | [diff] [blame] | 141 | if (!strcmp(name, "VPXDecoder")) { |
| 142 | return new ThreadedSource( |
| 143 | (*kFactoryInfo[i].CreateFunc)(source)); |
| 144 | } |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 145 | return (*kFactoryInfo[i].CreateFunc)(source); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | #undef FACTORY_REF |
| 153 | #undef FACTORY_CREATE |
| 154 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 155 | static const CodecInfo kDecoderInfo[] = { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 156 | { MEDIA_MIMETYPE_IMAGE_JPEG, "OMX.TI.JPEG.decode" }, |
Andreas Huber | d222c84 | 2010-08-26 14:29:34 -0700 | [diff] [blame] | 157 | // { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.Nvidia.mp3.decoder" }, |
James Dong | 374aee6 | 2010-04-26 10:23:30 -0700 | [diff] [blame] | 158 | // { MEDIA_MIMETYPE_AUDIO_MPEG, "OMX.TI.MP3.decode" }, |
Andreas Huber | acfbc80 | 2010-02-04 10:48:37 -0800 | [diff] [blame] | 159 | { MEDIA_MIMETYPE_AUDIO_MPEG, "MP3Decoder" }, |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 160 | // { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.decode" }, |
Andreas Huber | d222c84 | 2010-08-26 14:29:34 -0700 | [diff] [blame] | 161 | // { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amr.decoder" }, |
Andreas Huber | acfbc80 | 2010-02-04 10:48:37 -0800 | [diff] [blame] | 162 | { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBDecoder" }, |
Andreas Huber | d222c84 | 2010-08-26 14:29:34 -0700 | [diff] [blame] | 163 | // { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.Nvidia.amrwb.decoder" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 164 | { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.decode" }, |
Andreas Huber | acfbc80 | 2010-02-04 10:48:37 -0800 | [diff] [blame] | 165 | { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBDecoder" }, |
Andreas Huber | d222c84 | 2010-08-26 14:29:34 -0700 | [diff] [blame] | 166 | // { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.Nvidia.aac.decoder" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 167 | { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.decode" }, |
Andreas Huber | acfbc80 | 2010-02-04 10:48:37 -0800 | [diff] [blame] | 168 | { MEDIA_MIMETYPE_AUDIO_AAC, "AACDecoder" }, |
Andreas Huber | 520b2a7 | 2010-08-09 09:54:59 -0700 | [diff] [blame] | 169 | { MEDIA_MIMETYPE_AUDIO_G711_ALAW, "G711Decoder" }, |
| 170 | { MEDIA_MIMETYPE_AUDIO_G711_MLAW, "G711Decoder" }, |
Andreas Huber | 9f9ae60 | 2010-10-27 14:53:55 -0700 | [diff] [blame] | 171 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.Nvidia.mp4.decode" }, |
Andreas Huber | 8ef64c9 | 2010-06-29 09:14:00 -0700 | [diff] [blame] | 172 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.decoder.mpeg4" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 173 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.decoder.mpeg4" }, |
| 174 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.Decoder" }, |
Andreas Huber | 524e6f6 | 2010-09-16 11:23:09 -0700 | [diff] [blame] | 175 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Decoder" }, |
Andreas Huber | acfbc80 | 2010-02-04 10:48:37 -0800 | [diff] [blame] | 176 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Decoder" }, |
Andreas Huber | 9f9ae60 | 2010-10-27 14:53:55 -0700 | [diff] [blame] | 177 | { MEDIA_MIMETYPE_VIDEO_H263, "OMX.Nvidia.h263.decode" }, |
Andreas Huber | 8ef64c9 | 2010-06-29 09:14:00 -0700 | [diff] [blame] | 178 | { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.decoder.h263" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 179 | { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.decoder.h263" }, |
Andreas Huber | 524e6f6 | 2010-09-16 11:23:09 -0700 | [diff] [blame] | 180 | { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Decoder" }, |
Andreas Huber | acfbc80 | 2010-02-04 10:48:37 -0800 | [diff] [blame] | 181 | { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Decoder" }, |
pgudadhe | 6ad2c35 | 2010-07-26 15:04:33 -0700 | [diff] [blame] | 182 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.decode" }, |
Andreas Huber | 8ef64c9 | 2010-06-29 09:14:00 -0700 | [diff] [blame] | 183 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.decoder.avc" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 184 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.decoder.avc" }, |
| 185 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.Decoder" }, |
Andreas Huber | 524e6f6 | 2010-09-16 11:23:09 -0700 | [diff] [blame] | 186 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Decoder" }, |
Andreas Huber | acfbc80 | 2010-02-04 10:48:37 -0800 | [diff] [blame] | 187 | { MEDIA_MIMETYPE_VIDEO_AVC, "AVCDecoder" }, |
Andreas Huber | 388379f | 2010-05-07 10:35:13 -0700 | [diff] [blame] | 188 | { MEDIA_MIMETYPE_AUDIO_VORBIS, "VorbisDecoder" }, |
Andreas Huber | 47ba30e | 2010-05-24 14:38:02 -0700 | [diff] [blame] | 189 | { MEDIA_MIMETYPE_VIDEO_VPX, "VPXDecoder" }, |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static const CodecInfo kEncoderInfo[] = { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 193 | { MEDIA_MIMETYPE_AUDIO_AMR_NB, "OMX.TI.AMR.encode" }, |
Andreas Huber | acfbc80 | 2010-02-04 10:48:37 -0800 | [diff] [blame] | 194 | { MEDIA_MIMETYPE_AUDIO_AMR_NB, "AMRNBEncoder" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 195 | { MEDIA_MIMETYPE_AUDIO_AMR_WB, "OMX.TI.WBAMR.encode" }, |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 196 | { MEDIA_MIMETYPE_AUDIO_AMR_WB, "AMRWBEncoder" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 197 | { MEDIA_MIMETYPE_AUDIO_AAC, "OMX.TI.AAC.encode" }, |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 198 | { MEDIA_MIMETYPE_AUDIO_AAC, "AACEncoder" }, |
Andreas Huber | 8ef64c9 | 2010-06-29 09:14:00 -0700 | [diff] [blame] | 199 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.7x30.video.encoder.mpeg4" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 200 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.qcom.video.encoder.mpeg4" }, |
| 201 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.TI.Video.encoder" }, |
James Dong | 2e87f7b | 2010-09-23 17:46:34 -0700 | [diff] [blame] | 202 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.Nvidia.mp4.encoder" }, |
Andreas Huber | 524e6f6 | 2010-09-16 11:23:09 -0700 | [diff] [blame] | 203 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "OMX.SEC.MPEG4.Encoder" }, |
James Dong | 42ef0c7 | 2010-07-12 21:46:25 -0700 | [diff] [blame] | 204 | { MEDIA_MIMETYPE_VIDEO_MPEG4, "M4vH263Encoder" }, |
Andreas Huber | 8ef64c9 | 2010-06-29 09:14:00 -0700 | [diff] [blame] | 205 | { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.7x30.video.encoder.h263" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 206 | { MEDIA_MIMETYPE_VIDEO_H263, "OMX.qcom.video.encoder.h263" }, |
| 207 | { MEDIA_MIMETYPE_VIDEO_H263, "OMX.TI.Video.encoder" }, |
James Dong | 2e87f7b | 2010-09-23 17:46:34 -0700 | [diff] [blame] | 208 | { MEDIA_MIMETYPE_VIDEO_H263, "OMX.Nvidia.h263.encoder" }, |
Andreas Huber | 524e6f6 | 2010-09-16 11:23:09 -0700 | [diff] [blame] | 209 | { MEDIA_MIMETYPE_VIDEO_H263, "OMX.SEC.H263.Encoder" }, |
James Dong | 42ef0c7 | 2010-07-12 21:46:25 -0700 | [diff] [blame] | 210 | { MEDIA_MIMETYPE_VIDEO_H263, "M4vH263Encoder" }, |
Andreas Huber | 8ef64c9 | 2010-06-29 09:14:00 -0700 | [diff] [blame] | 211 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.7x30.video.encoder.avc" }, |
Andreas Huber | 71c27d9 | 2010-03-19 11:43:15 -0700 | [diff] [blame] | 212 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.qcom.video.encoder.avc" }, |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 213 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.TI.Video.encoder" }, |
pgudadhe | 9c30532 | 2010-07-26 13:59:29 -0700 | [diff] [blame] | 214 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.Nvidia.h264.encoder" }, |
Andreas Huber | 524e6f6 | 2010-09-16 11:23:09 -0700 | [diff] [blame] | 215 | { MEDIA_MIMETYPE_VIDEO_AVC, "OMX.SEC.AVC.Encoder" }, |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 216 | { MEDIA_MIMETYPE_VIDEO_AVC, "AVCEncoder" }, |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 219 | #undef OPTIONAL |
| 220 | |
Andreas Huber | e087373 | 2009-09-10 09:57:53 -0700 | [diff] [blame] | 221 | #define CODEC_LOGI(x, ...) LOGI("[%s] "x, mComponentName, ##__VA_ARGS__) |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 222 | #define CODEC_LOGV(x, ...) LOGV("[%s] "x, mComponentName, ##__VA_ARGS__) |
Andreas Huber | 42c444a | 2010-02-09 10:20:00 -0800 | [diff] [blame] | 223 | #define CODEC_LOGE(x, ...) LOGE("[%s] "x, mComponentName, ##__VA_ARGS__) |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 224 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 225 | struct OMXCodecObserver : public BnOMXObserver { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 226 | OMXCodecObserver() { |
| 227 | } |
| 228 | |
| 229 | void setCodec(const sp<OMXCodec> &target) { |
| 230 | mTarget = target; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // from IOMXObserver |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 234 | virtual void onMessage(const omx_message &msg) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 235 | sp<OMXCodec> codec = mTarget.promote(); |
| 236 | |
| 237 | if (codec.get() != NULL) { |
James Dong | 681e89c | 2011-01-10 08:55:02 -0800 | [diff] [blame] | 238 | Mutex::Autolock autoLock(codec->mLock); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 239 | codec->on_message(msg); |
James Dong | 681e89c | 2011-01-10 08:55:02 -0800 | [diff] [blame] | 240 | codec.clear(); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | protected: |
| 245 | virtual ~OMXCodecObserver() {} |
| 246 | |
| 247 | private: |
| 248 | wp<OMXCodec> mTarget; |
| 249 | |
| 250 | OMXCodecObserver(const OMXCodecObserver &); |
| 251 | OMXCodecObserver &operator=(const OMXCodecObserver &); |
| 252 | }; |
| 253 | |
| 254 | static const char *GetCodec(const CodecInfo *info, size_t numInfos, |
| 255 | const char *mime, int index) { |
| 256 | CHECK(index >= 0); |
| 257 | for(size_t i = 0; i < numInfos; ++i) { |
| 258 | if (!strcasecmp(mime, info[i].mime)) { |
| 259 | if (index == 0) { |
| 260 | return info[i].codec; |
| 261 | } |
| 262 | |
| 263 | --index; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return NULL; |
| 268 | } |
| 269 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 270 | template<class T> |
| 271 | static void InitOMXParams(T *params) { |
| 272 | params->nSize = sizeof(T); |
| 273 | params->nVersion.s.nVersionMajor = 1; |
| 274 | params->nVersion.s.nVersionMinor = 0; |
| 275 | params->nVersion.s.nRevision = 0; |
| 276 | params->nVersion.s.nStep = 0; |
| 277 | } |
| 278 | |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 279 | static bool IsSoftwareCodec(const char *componentName) { |
James Dong | 5592bcc | 2010-10-22 17:10:43 -0700 | [diff] [blame] | 280 | if (!strncmp("OMX.", componentName, 4)) { |
| 281 | return false; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 282 | } |
| 283 | |
James Dong | 5592bcc | 2010-10-22 17:10:43 -0700 | [diff] [blame] | 284 | return true; |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 287 | // A sort order in which non-OMX components are first, |
James Dong | 5592bcc | 2010-10-22 17:10:43 -0700 | [diff] [blame] | 288 | // followed by software codecs, and followed by all the others. |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 289 | static int CompareSoftwareCodecsFirst( |
| 290 | const String8 *elem1, const String8 *elem2) { |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 291 | bool isNotOMX1 = strncmp(elem1->string(), "OMX.", 4); |
| 292 | bool isNotOMX2 = strncmp(elem2->string(), "OMX.", 4); |
| 293 | |
| 294 | if (isNotOMX1) { |
| 295 | if (isNotOMX2) { return 0; } |
| 296 | return -1; |
| 297 | } |
| 298 | if (isNotOMX2) { |
| 299 | return 1; |
| 300 | } |
| 301 | |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 302 | bool isSoftwareCodec1 = IsSoftwareCodec(elem1->string()); |
| 303 | bool isSoftwareCodec2 = IsSoftwareCodec(elem2->string()); |
| 304 | |
| 305 | if (isSoftwareCodec1) { |
| 306 | if (isSoftwareCodec2) { return 0; } |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | if (isSoftwareCodec2) { |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | // static |
Andreas Huber | 1e19416 | 2010-10-06 16:43:57 -0700 | [diff] [blame] | 318 | uint32_t OMXCodec::getComponentQuirks( |
| 319 | const char *componentName, bool isEncoder) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 320 | uint32_t quirks = 0; |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 321 | |
Dima Zavin | 30ba6cb | 2010-08-23 11:10:03 -0700 | [diff] [blame] | 322 | if (!strcmp(componentName, "OMX.Nvidia.amr.decoder") || |
| 323 | !strcmp(componentName, "OMX.Nvidia.amrwb.decoder") || |
| 324 | !strcmp(componentName, "OMX.Nvidia.aac.decoder") || |
| 325 | !strcmp(componentName, "OMX.Nvidia.mp3.decoder")) { |
| 326 | quirks |= kDecoderLiesAboutNumberOfChannels; |
| 327 | } |
| 328 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 329 | if (!strcmp(componentName, "OMX.TI.MP3.decode")) { |
| 330 | quirks |= kNeedsFlushBeforeDisable; |
Andreas Huber | e331c7b | 2010-02-01 10:51:50 -0800 | [diff] [blame] | 331 | quirks |= kDecoderLiesAboutNumberOfChannels; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 332 | } |
| 333 | if (!strcmp(componentName, "OMX.TI.AAC.decode")) { |
| 334 | quirks |= kNeedsFlushBeforeDisable; |
Andreas Huber | 404cc41 | 2009-08-25 14:26:05 -0700 | [diff] [blame] | 335 | quirks |= kRequiresFlushCompleteEmulation; |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 336 | quirks |= kSupportsMultipleFramesPerInputBuffer; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 337 | } |
| 338 | if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) { |
| 339 | quirks |= kRequiresLoadedToIdleAfterAllocation; |
| 340 | quirks |= kRequiresAllocateBufferOnInputPorts; |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 341 | quirks |= kRequiresAllocateBufferOnOutputPorts; |
James Dong | 90862e2 | 2010-08-26 19:12:59 -0700 | [diff] [blame] | 342 | if (!strncmp(componentName, "OMX.qcom.video.encoder.avc", 26)) { |
| 343 | |
| 344 | // The AVC encoder advertises the size of output buffers |
| 345 | // based on the input video resolution and assumes |
| 346 | // the worst/least compression ratio is 0.5. It is found that |
| 347 | // sometimes, the output buffer size is larger than |
| 348 | // size advertised by the encoder. |
| 349 | quirks |= kRequiresLargerEncoderOutputBuffer; |
| 350 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 351 | } |
Andreas Huber | 8ef64c9 | 2010-06-29 09:14:00 -0700 | [diff] [blame] | 352 | if (!strncmp(componentName, "OMX.qcom.7x30.video.encoder.", 28)) { |
| 353 | } |
Andreas Huber | a7d0cf4 | 2009-09-04 07:48:51 -0700 | [diff] [blame] | 354 | if (!strncmp(componentName, "OMX.qcom.video.decoder.", 23)) { |
Andreas Huber | a7d0cf4 | 2009-09-04 07:48:51 -0700 | [diff] [blame] | 355 | quirks |= kRequiresAllocateBufferOnOutputPorts; |
Andreas Huber | 52733b8 | 2010-01-25 10:41:35 -0800 | [diff] [blame] | 356 | quirks |= kDefersOutputBufferAllocation; |
Andreas Huber | a7d0cf4 | 2009-09-04 07:48:51 -0700 | [diff] [blame] | 357 | } |
Andreas Huber | 8ef64c9 | 2010-06-29 09:14:00 -0700 | [diff] [blame] | 358 | if (!strncmp(componentName, "OMX.qcom.7x30.video.decoder.", 28)) { |
| 359 | quirks |= kRequiresAllocateBufferOnInputPorts; |
| 360 | quirks |= kRequiresAllocateBufferOnOutputPorts; |
| 361 | quirks |= kDefersOutputBufferAllocation; |
| 362 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 363 | |
Andreas Huber | 2dc64d8 | 2009-09-11 12:58:53 -0700 | [diff] [blame] | 364 | if (!strncmp(componentName, "OMX.TI.", 7)) { |
| 365 | // Apparently I must not use OMX_UseBuffer on either input or |
| 366 | // output ports on any of the TI components or quote: |
| 367 | // "(I) may have unexpected problem (sic) which can be timing related |
| 368 | // and hard to reproduce." |
| 369 | |
| 370 | quirks |= kRequiresAllocateBufferOnInputPorts; |
| 371 | quirks |= kRequiresAllocateBufferOnOutputPorts; |
James Dong | dca66e1 | 2010-06-14 11:14:38 -0700 | [diff] [blame] | 372 | if (!strncmp(componentName, "OMX.TI.Video.encoder", 20)) { |
James Dong | 4f501f0 | 2010-06-07 14:41:41 -0700 | [diff] [blame] | 373 | quirks |= kAvoidMemcopyInputRecordingFrames; |
| 374 | } |
Andreas Huber | 2dc64d8 | 2009-09-11 12:58:53 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Andreas Huber | b8de957 | 2010-02-22 14:58:45 -0800 | [diff] [blame] | 377 | if (!strcmp(componentName, "OMX.TI.Video.Decoder")) { |
| 378 | quirks |= kInputBufferSizesAreBogus; |
| 379 | } |
| 380 | |
Andreas Huber | 1e19416 | 2010-10-06 16:43:57 -0700 | [diff] [blame] | 381 | if (!strncmp(componentName, "OMX.SEC.", 8) && !isEncoder) { |
| 382 | // These output buffers contain no video data, just some |
| 383 | // opaque information that allows the overlay to display their |
| 384 | // contents. |
| 385 | quirks |= kOutputBuffersAreUnreadable; |
| 386 | } |
| 387 | |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 388 | return quirks; |
| 389 | } |
| 390 | |
| 391 | // static |
| 392 | void OMXCodec::findMatchingCodecs( |
| 393 | const char *mime, |
| 394 | bool createEncoder, const char *matchComponentName, |
| 395 | uint32_t flags, |
| 396 | Vector<String8> *matchingCodecs) { |
| 397 | matchingCodecs->clear(); |
| 398 | |
| 399 | for (int index = 0;; ++index) { |
| 400 | const char *componentName; |
| 401 | |
| 402 | if (createEncoder) { |
| 403 | componentName = GetCodec( |
| 404 | kEncoderInfo, |
| 405 | sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]), |
| 406 | mime, index); |
| 407 | } else { |
| 408 | componentName = GetCodec( |
| 409 | kDecoderInfo, |
| 410 | sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]), |
| 411 | mime, index); |
| 412 | } |
| 413 | |
| 414 | if (!componentName) { |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | // If a specific codec is requested, skip the non-matching ones. |
| 419 | if (matchComponentName && strcmp(componentName, matchComponentName)) { |
| 420 | continue; |
| 421 | } |
| 422 | |
James Dong | 170a929 | 2010-10-22 17:28:15 -0700 | [diff] [blame] | 423 | // When requesting software-only codecs, only push software codecs |
| 424 | // When requesting hardware-only codecs, only push hardware codecs |
| 425 | // When there is request neither for software-only nor for |
| 426 | // hardware-only codecs, push all codecs |
| 427 | if (((flags & kSoftwareCodecsOnly) && IsSoftwareCodec(componentName)) || |
| 428 | ((flags & kHardwareCodecsOnly) && !IsSoftwareCodec(componentName)) || |
| 429 | (!(flags & (kSoftwareCodecsOnly | kHardwareCodecsOnly)))) { |
| 430 | |
| 431 | matchingCodecs->push(String8(componentName)); |
| 432 | } |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | if (flags & kPreferSoftwareCodecs) { |
| 436 | matchingCodecs->sort(CompareSoftwareCodecsFirst); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // static |
Andreas Huber | 91eb035 | 2009-12-07 09:43:00 -0800 | [diff] [blame] | 441 | sp<MediaSource> OMXCodec::Create( |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 442 | const sp<IOMX> &omx, |
| 443 | const sp<MetaData> &meta, bool createEncoder, |
| 444 | const sp<MediaSource> &source, |
| 445 | const char *matchComponentName, |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 446 | uint32_t flags, |
| 447 | const sp<ANativeWindow> &nativeWindow) { |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 448 | const char *mime; |
| 449 | bool success = meta->findCString(kKeyMIMEType, &mime); |
| 450 | CHECK(success); |
| 451 | |
| 452 | Vector<String8> matchingCodecs; |
| 453 | findMatchingCodecs( |
| 454 | mime, createEncoder, matchComponentName, flags, &matchingCodecs); |
| 455 | |
| 456 | if (matchingCodecs.isEmpty()) { |
| 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | sp<OMXCodecObserver> observer = new OMXCodecObserver; |
| 461 | IOMX::node_id node = 0; |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 462 | |
| 463 | const char *componentName; |
| 464 | for (size_t i = 0; i < matchingCodecs.size(); ++i) { |
| 465 | componentName = matchingCodecs[i].string(); |
| 466 | |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 467 | sp<MediaSource> softwareCodec = createEncoder? |
| 468 | InstantiateSoftwareEncoder(componentName, source, meta): |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 469 | InstantiateSoftwareCodec(componentName, source); |
| 470 | |
| 471 | if (softwareCodec != NULL) { |
| 472 | LOGV("Successfully allocated software codec '%s'", componentName); |
| 473 | |
| 474 | return softwareCodec; |
| 475 | } |
Andreas Huber | fb1c2f8 | 2009-12-15 13:25:11 -0800 | [diff] [blame] | 476 | |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 477 | LOGV("Attempting to allocate OMX node '%s'", componentName); |
| 478 | |
Andreas Huber | 5a40e39 | 2010-10-18 09:57:42 -0700 | [diff] [blame] | 479 | uint32_t quirks = getComponentQuirks(componentName, createEncoder); |
| 480 | |
| 481 | if (!createEncoder |
| 482 | && (quirks & kOutputBuffersAreUnreadable) |
| 483 | && (flags & kClientNeedsFramebuffer)) { |
| 484 | if (strncmp(componentName, "OMX.SEC.", 8)) { |
| 485 | // For OMX.SEC.* decoders we can enable a special mode that |
| 486 | // gives the client access to the framebuffer contents. |
| 487 | |
| 488 | LOGW("Component '%s' does not give the client access to " |
| 489 | "the framebuffer contents. Skipping.", |
| 490 | componentName); |
| 491 | |
| 492 | continue; |
| 493 | } |
| 494 | } |
| 495 | |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 496 | status_t err = omx->allocateNode(componentName, observer, &node); |
| 497 | if (err == OK) { |
| 498 | LOGV("Successfully allocated OMX node '%s'", componentName); |
| 499 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 500 | sp<OMXCodec> codec = new OMXCodec( |
Andreas Huber | 5a40e39 | 2010-10-18 09:57:42 -0700 | [diff] [blame] | 501 | omx, node, quirks, |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 502 | createEncoder, mime, componentName, |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 503 | source, nativeWindow); |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 504 | |
| 505 | observer->setCodec(codec); |
| 506 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 507 | err = codec->configureCodec(meta, flags); |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 508 | |
| 509 | if (err == OK) { |
| 510 | return codec; |
| 511 | } |
| 512 | |
| 513 | LOGV("Failed to configure codec '%s'", componentName); |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 517 | return NULL; |
| 518 | } |
Andreas Huber | e13526a | 2009-10-22 10:43:34 -0700 | [diff] [blame] | 519 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 520 | status_t OMXCodec::configureCodec(const sp<MetaData> &meta, uint32_t flags) { |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 521 | mIsMetaDataStoredInVideoBuffers = false; |
| 522 | if (flags & kStoreMetaDataInVideoBuffers) { |
| 523 | mIsMetaDataStoredInVideoBuffers = true; |
| 524 | } |
James Dong | 5f3ab06 | 2011-01-25 16:31:28 -0800 | [diff] [blame] | 525 | |
| 526 | mOnlySubmitOneBufferAtOneTime = false; |
| 527 | if (flags & kOnlySubmitOneInputBufferAtOneTime) { |
| 528 | mOnlySubmitOneBufferAtOneTime = true; |
| 529 | } |
| 530 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 531 | if (!(flags & kIgnoreCodecSpecificData)) { |
| 532 | uint32_t type; |
| 533 | const void *data; |
| 534 | size_t size; |
| 535 | if (meta->findData(kKeyESDS, &type, &data, &size)) { |
| 536 | ESDS esds((const char *)data, size); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 537 | CHECK_EQ(esds.InitCheck(), (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 538 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 539 | const void *codec_specific_data; |
| 540 | size_t codec_specific_data_size; |
| 541 | esds.getCodecSpecificInfo( |
| 542 | &codec_specific_data, &codec_specific_data_size); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 543 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 544 | addCodecSpecificData( |
| 545 | codec_specific_data, codec_specific_data_size); |
| 546 | } else if (meta->findData(kKeyAVCC, &type, &data, &size)) { |
| 547 | // Parse the AVCDecoderConfigurationRecord |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 548 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 549 | const uint8_t *ptr = (const uint8_t *)data; |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 550 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 551 | CHECK(size >= 7); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 552 | CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1 |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 553 | uint8_t profile = ptr[1]; |
| 554 | uint8_t level = ptr[3]; |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 555 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 556 | // There is decodable content out there that fails the following |
| 557 | // assertion, let's be lenient for now... |
| 558 | // CHECK((ptr[4] >> 2) == 0x3f); // reserved |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 559 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 560 | size_t lengthSize = 1 + (ptr[4] & 3); |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 561 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 562 | // commented out check below as H264_QVGA_500_NO_AUDIO.3gp |
| 563 | // violates it... |
| 564 | // CHECK((ptr[5] >> 5) == 7); // reserved |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 565 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 566 | size_t numSeqParameterSets = ptr[5] & 31; |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 567 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 568 | ptr += 6; |
| 569 | size -= 6; |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 570 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 571 | for (size_t i = 0; i < numSeqParameterSets; ++i) { |
| 572 | CHECK(size >= 2); |
| 573 | size_t length = U16_AT(ptr); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 574 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 575 | ptr += 2; |
| 576 | size -= 2; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 577 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 578 | CHECK(size >= length); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 579 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 580 | addCodecSpecificData(ptr, length); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 581 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 582 | ptr += length; |
| 583 | size -= length; |
| 584 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 585 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 586 | CHECK(size >= 1); |
| 587 | size_t numPictureParameterSets = *ptr; |
| 588 | ++ptr; |
| 589 | --size; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 590 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 591 | for (size_t i = 0; i < numPictureParameterSets; ++i) { |
| 592 | CHECK(size >= 2); |
| 593 | size_t length = U16_AT(ptr); |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 594 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 595 | ptr += 2; |
| 596 | size -= 2; |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 597 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 598 | CHECK(size >= length); |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 599 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 600 | addCodecSpecificData(ptr, length); |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 601 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 602 | ptr += length; |
| 603 | size -= length; |
| 604 | } |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 605 | |
Andreas Huber | 6ac2cb2 | 2010-11-18 14:06:07 -0800 | [diff] [blame] | 606 | CODEC_LOGI( |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 607 | "AVC profile = %d (%s), level = %d", |
| 608 | (int)profile, AVCProfileToString(profile), level); |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 609 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 610 | if (!strcmp(mComponentName, "OMX.TI.Video.Decoder") |
| 611 | && (profile != kAVCProfileBaseline || level > 30)) { |
| 612 | // This stream exceeds the decoder's capabilities. The decoder |
| 613 | // does not handle this gracefully and would clobber the heap |
| 614 | // and wreak havoc instead... |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 615 | |
Andreas Huber | 4c19bf9 | 2010-09-08 14:32:20 -0700 | [diff] [blame] | 616 | LOGE("Profile and/or level exceed the decoder's capabilities."); |
| 617 | return ERROR_UNSUPPORTED; |
| 618 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 622 | int32_t bitRate = 0; |
| 623 | if (mIsEncoder) { |
| 624 | CHECK(meta->findInt32(kKeyBitRate, &bitRate)); |
| 625 | } |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 626 | if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mMIME)) { |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 627 | setAMRFormat(false /* isWAMR */, bitRate); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 628 | } |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 629 | if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mMIME)) { |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 630 | setAMRFormat(true /* isWAMR */, bitRate); |
Andreas Huber | ee606e6 | 2009-09-08 10:19:21 -0700 | [diff] [blame] | 631 | } |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 632 | if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mMIME)) { |
Andreas Huber | 43ad6eaf | 2009-09-01 16:02:43 -0700 | [diff] [blame] | 633 | int32_t numChannels, sampleRate; |
| 634 | CHECK(meta->findInt32(kKeyChannelCount, &numChannels)); |
| 635 | CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); |
| 636 | |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 637 | setAACFormat(numChannels, sampleRate, bitRate); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 638 | } |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 639 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 640 | if (!strncasecmp(mMIME, "video/", 6)) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 641 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 642 | if (mIsEncoder) { |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 643 | setVideoInputFormat(mMIME, meta); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 644 | } else { |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 645 | int32_t width, height; |
| 646 | bool success = meta->findInt32(kKeyWidth, &width); |
| 647 | success = success && meta->findInt32(kKeyHeight, &height); |
| 648 | CHECK(success); |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 649 | status_t err = setVideoOutputFormat( |
| 650 | mMIME, width, height); |
| 651 | |
| 652 | if (err != OK) { |
| 653 | return err; |
| 654 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 657 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 658 | if (!strcasecmp(mMIME, MEDIA_MIMETYPE_IMAGE_JPEG) |
| 659 | && !strcmp(mComponentName, "OMX.TI.JPEG.decode")) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 660 | OMX_COLOR_FORMATTYPE format = |
| 661 | OMX_COLOR_Format32bitARGB8888; |
| 662 | // OMX_COLOR_FormatYUV420PackedPlanar; |
| 663 | // OMX_COLOR_FormatCbYCrY; |
| 664 | // OMX_COLOR_FormatYUV411Planar; |
| 665 | |
| 666 | int32_t width, height; |
| 667 | bool success = meta->findInt32(kKeyWidth, &width); |
| 668 | success = success && meta->findInt32(kKeyHeight, &height); |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 669 | |
| 670 | int32_t compressedSize; |
| 671 | success = success && meta->findInt32( |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 672 | kKeyMaxInputSize, &compressedSize); |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 673 | |
| 674 | CHECK(success); |
| 675 | CHECK(compressedSize > 0); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 676 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 677 | setImageOutputFormat(format, width, height); |
| 678 | setJPEGInputFormat(width, height, (OMX_U32)compressedSize); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 681 | int32_t maxInputSize; |
Andreas Huber | 1bceff9 | 2009-11-23 14:03:32 -0800 | [diff] [blame] | 682 | if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) { |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 683 | setMinBufferSize(kPortIndexInput, (OMX_U32)maxInputSize); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 686 | if (!strcmp(mComponentName, "OMX.TI.AMR.encode") |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 687 | || !strcmp(mComponentName, "OMX.TI.WBAMR.encode") |
| 688 | || !strcmp(mComponentName, "OMX.TI.AAC.encode")) { |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 689 | setMinBufferSize(kPortIndexOutput, 8192); // XXX |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 692 | initOutputFormat(meta); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 693 | |
Andreas Huber | 5a40e39 | 2010-10-18 09:57:42 -0700 | [diff] [blame] | 694 | if ((flags & kClientNeedsFramebuffer) |
| 695 | && !strncmp(mComponentName, "OMX.SEC.", 8)) { |
| 696 | OMX_INDEXTYPE index; |
| 697 | |
| 698 | status_t err = |
| 699 | mOMX->getExtensionIndex( |
| 700 | mNode, |
| 701 | "OMX.SEC.index.ThumbnailMode", |
| 702 | &index); |
| 703 | |
| 704 | if (err != OK) { |
| 705 | return err; |
| 706 | } |
| 707 | |
| 708 | OMX_BOOL enable = OMX_TRUE; |
| 709 | err = mOMX->setConfig(mNode, index, &enable, sizeof(enable)); |
| 710 | |
| 711 | if (err != OK) { |
| 712 | CODEC_LOGE("setConfig('OMX.SEC.index.ThumbnailMode') " |
| 713 | "returned error 0x%08x", err); |
| 714 | |
| 715 | return err; |
| 716 | } |
| 717 | |
| 718 | mQuirks &= ~kOutputBuffersAreUnreadable; |
| 719 | } |
| 720 | |
Jamie Gennis | dbfb32e | 2010-10-20 15:53:59 -0700 | [diff] [blame] | 721 | if (mNativeWindow != NULL |
| 722 | && !mIsEncoder |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 723 | && !strncasecmp(mMIME, "video/", 6) |
| 724 | && !strncmp(mComponentName, "OMX.", 4)) { |
| 725 | status_t err = initNativeWindow(); |
| 726 | if (err != OK) { |
| 727 | return err; |
| 728 | } |
| 729 | } |
| 730 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 731 | return OK; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 734 | void OMXCodec::setMinBufferSize(OMX_U32 portIndex, OMX_U32 size) { |
| 735 | OMX_PARAM_PORTDEFINITIONTYPE def; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 736 | InitOMXParams(&def); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 737 | def.nPortIndex = portIndex; |
| 738 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 739 | status_t err = mOMX->getParameter( |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 740 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 741 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 742 | |
Andreas Huber | b8de957 | 2010-02-22 14:58:45 -0800 | [diff] [blame] | 743 | if ((portIndex == kPortIndexInput && (mQuirks & kInputBufferSizesAreBogus)) |
| 744 | || (def.nBufferSize < size)) { |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 745 | def.nBufferSize = size; |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 748 | err = mOMX->setParameter( |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 749 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 750 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | 1bceff9 | 2009-11-23 14:03:32 -0800 | [diff] [blame] | 751 | |
| 752 | err = mOMX->getParameter( |
| 753 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 754 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | 1bceff9 | 2009-11-23 14:03:32 -0800 | [diff] [blame] | 755 | |
| 756 | // Make sure the setting actually stuck. |
Andreas Huber | b8de957 | 2010-02-22 14:58:45 -0800 | [diff] [blame] | 757 | if (portIndex == kPortIndexInput |
| 758 | && (mQuirks & kInputBufferSizesAreBogus)) { |
| 759 | CHECK_EQ(def.nBufferSize, size); |
| 760 | } else { |
| 761 | CHECK(def.nBufferSize >= size); |
| 762 | } |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 765 | status_t OMXCodec::setVideoPortFormatType( |
| 766 | OMX_U32 portIndex, |
| 767 | OMX_VIDEO_CODINGTYPE compressionFormat, |
| 768 | OMX_COLOR_FORMATTYPE colorFormat) { |
| 769 | OMX_VIDEO_PARAM_PORTFORMATTYPE format; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 770 | InitOMXParams(&format); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 771 | format.nPortIndex = portIndex; |
| 772 | format.nIndex = 0; |
| 773 | bool found = false; |
| 774 | |
| 775 | OMX_U32 index = 0; |
| 776 | for (;;) { |
| 777 | format.nIndex = index; |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 778 | status_t err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 779 | mNode, OMX_IndexParamVideoPortFormat, |
| 780 | &format, sizeof(format)); |
| 781 | |
| 782 | if (err != OK) { |
| 783 | return err; |
| 784 | } |
| 785 | |
| 786 | // The following assertion is violated by TI's video decoder. |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 787 | // CHECK_EQ(format.nIndex, index); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 788 | |
| 789 | #if 1 |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 790 | CODEC_LOGV("portIndex: %ld, index: %ld, eCompressionFormat=%d eColorFormat=%d", |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 791 | portIndex, |
| 792 | index, format.eCompressionFormat, format.eColorFormat); |
| 793 | #endif |
| 794 | |
| 795 | if (!strcmp("OMX.TI.Video.encoder", mComponentName)) { |
| 796 | if (portIndex == kPortIndexInput |
| 797 | && colorFormat == format.eColorFormat) { |
| 798 | // eCompressionFormat does not seem right. |
| 799 | found = true; |
| 800 | break; |
| 801 | } |
| 802 | if (portIndex == kPortIndexOutput |
| 803 | && compressionFormat == format.eCompressionFormat) { |
| 804 | // eColorFormat does not seem right. |
| 805 | found = true; |
| 806 | break; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | if (format.eCompressionFormat == compressionFormat |
| 811 | && format.eColorFormat == colorFormat) { |
| 812 | found = true; |
| 813 | break; |
| 814 | } |
| 815 | |
| 816 | ++index; |
| 817 | } |
| 818 | |
| 819 | if (!found) { |
| 820 | return UNKNOWN_ERROR; |
| 821 | } |
| 822 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 823 | CODEC_LOGV("found a match."); |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 824 | status_t err = mOMX->setParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 825 | mNode, OMX_IndexParamVideoPortFormat, |
| 826 | &format, sizeof(format)); |
| 827 | |
| 828 | return err; |
| 829 | } |
| 830 | |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 831 | static size_t getFrameSize( |
| 832 | OMX_COLOR_FORMATTYPE colorFormat, int32_t width, int32_t height) { |
| 833 | switch (colorFormat) { |
| 834 | case OMX_COLOR_FormatYCbYCr: |
| 835 | case OMX_COLOR_FormatCbYCrY: |
| 836 | return width * height * 2; |
| 837 | |
Andreas Huber | 71c27d9 | 2010-03-19 11:43:15 -0700 | [diff] [blame] | 838 | case OMX_COLOR_FormatYUV420Planar: |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 839 | case OMX_COLOR_FormatYUV420SemiPlanar: |
| 840 | return (width * height * 3) / 2; |
| 841 | |
| 842 | default: |
| 843 | CHECK(!"Should not be here. Unsupported color format."); |
| 844 | break; |
| 845 | } |
| 846 | } |
| 847 | |
James Dong | afd97e8 | 2010-08-03 17:19:23 -0700 | [diff] [blame] | 848 | status_t OMXCodec::findTargetColorFormat( |
| 849 | const sp<MetaData>& meta, OMX_COLOR_FORMATTYPE *colorFormat) { |
| 850 | LOGV("findTargetColorFormat"); |
| 851 | CHECK(mIsEncoder); |
| 852 | |
| 853 | *colorFormat = OMX_COLOR_FormatYUV420SemiPlanar; |
| 854 | int32_t targetColorFormat; |
| 855 | if (meta->findInt32(kKeyColorFormat, &targetColorFormat)) { |
| 856 | *colorFormat = (OMX_COLOR_FORMATTYPE) targetColorFormat; |
| 857 | } else { |
| 858 | if (!strcasecmp("OMX.TI.Video.encoder", mComponentName)) { |
| 859 | *colorFormat = OMX_COLOR_FormatYCbYCr; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | // Check whether the target color format is supported. |
| 864 | return isColorFormatSupported(*colorFormat, kPortIndexInput); |
| 865 | } |
| 866 | |
| 867 | status_t OMXCodec::isColorFormatSupported( |
| 868 | OMX_COLOR_FORMATTYPE colorFormat, int portIndex) { |
| 869 | LOGV("isColorFormatSupported: %d", static_cast<int>(colorFormat)); |
| 870 | |
| 871 | // Enumerate all the color formats supported by |
| 872 | // the omx component to see whether the given |
| 873 | // color format is supported. |
| 874 | OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat; |
| 875 | InitOMXParams(&portFormat); |
| 876 | portFormat.nPortIndex = portIndex; |
| 877 | OMX_U32 index = 0; |
| 878 | portFormat.nIndex = index; |
| 879 | while (true) { |
| 880 | if (OMX_ErrorNone != mOMX->getParameter( |
| 881 | mNode, OMX_IndexParamVideoPortFormat, |
| 882 | &portFormat, sizeof(portFormat))) { |
James Dong | b5024da | 2010-09-13 16:30:51 -0700 | [diff] [blame] | 883 | break; |
James Dong | afd97e8 | 2010-08-03 17:19:23 -0700 | [diff] [blame] | 884 | } |
| 885 | // Make sure that omx component does not overwrite |
| 886 | // the incremented index (bug 2897413). |
| 887 | CHECK_EQ(index, portFormat.nIndex); |
| 888 | if ((portFormat.eColorFormat == colorFormat)) { |
| 889 | LOGV("Found supported color format: %d", portFormat.eColorFormat); |
| 890 | return OK; // colorFormat is supported! |
| 891 | } |
| 892 | ++index; |
| 893 | portFormat.nIndex = index; |
| 894 | |
| 895 | // OMX Spec defines less than 50 color formats |
| 896 | // 1000 is more than enough for us to tell whether the omx |
| 897 | // component in question is buggy or not. |
| 898 | if (index >= 1000) { |
| 899 | LOGE("More than %ld color formats are supported???", index); |
| 900 | break; |
| 901 | } |
| 902 | } |
James Dong | b5024da | 2010-09-13 16:30:51 -0700 | [diff] [blame] | 903 | |
| 904 | LOGE("color format %d is not supported", colorFormat); |
James Dong | afd97e8 | 2010-08-03 17:19:23 -0700 | [diff] [blame] | 905 | return UNKNOWN_ERROR; |
| 906 | } |
| 907 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 908 | void OMXCodec::setVideoInputFormat( |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 909 | const char *mime, const sp<MetaData>& meta) { |
| 910 | |
| 911 | int32_t width, height, frameRate, bitRate, stride, sliceHeight; |
| 912 | bool success = meta->findInt32(kKeyWidth, &width); |
| 913 | success = success && meta->findInt32(kKeyHeight, &height); |
James Dong | aac193c | 2010-11-10 20:43:53 -0800 | [diff] [blame] | 914 | success = success && meta->findInt32(kKeyFrameRate, &frameRate); |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 915 | success = success && meta->findInt32(kKeyBitRate, &bitRate); |
| 916 | success = success && meta->findInt32(kKeyStride, &stride); |
| 917 | success = success && meta->findInt32(kKeySliceHeight, &sliceHeight); |
| 918 | CHECK(success); |
| 919 | CHECK(stride != 0); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 920 | |
| 921 | OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused; |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 922 | if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 923 | compressionFormat = OMX_VIDEO_CodingAVC; |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 924 | } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 925 | compressionFormat = OMX_VIDEO_CodingMPEG4; |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 926 | } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 927 | compressionFormat = OMX_VIDEO_CodingH263; |
| 928 | } else { |
| 929 | LOGE("Not a supported video mime type: %s", mime); |
| 930 | CHECK(!"Should not be here. Not a supported video mime type."); |
| 931 | } |
| 932 | |
James Dong | afd97e8 | 2010-08-03 17:19:23 -0700 | [diff] [blame] | 933 | OMX_COLOR_FORMATTYPE colorFormat; |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 934 | CHECK_EQ((status_t)OK, findTargetColorFormat(meta, &colorFormat)); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 935 | |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 936 | status_t err; |
| 937 | OMX_PARAM_PORTDEFINITIONTYPE def; |
| 938 | OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; |
| 939 | |
| 940 | //////////////////////// Input port ///////////////////////// |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 941 | CHECK_EQ(setVideoPortFormatType( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 942 | kPortIndexInput, OMX_VIDEO_CodingUnused, |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 943 | colorFormat), (status_t)OK); |
James Dong | 4f501f0 | 2010-06-07 14:41:41 -0700 | [diff] [blame] | 944 | |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 945 | InitOMXParams(&def); |
| 946 | def.nPortIndex = kPortIndexInput; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 947 | |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 948 | err = mOMX->getParameter( |
| 949 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 950 | CHECK_EQ(err, (status_t)OK); |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 951 | |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 952 | def.nBufferSize = getFrameSize(colorFormat, |
| 953 | stride > 0? stride: -stride, sliceHeight); |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 954 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 955 | CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo); |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 956 | |
| 957 | video_def->nFrameWidth = width; |
| 958 | video_def->nFrameHeight = height; |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 959 | video_def->nStride = stride; |
| 960 | video_def->nSliceHeight = sliceHeight; |
James Dong | 4f501f0 | 2010-06-07 14:41:41 -0700 | [diff] [blame] | 961 | video_def->xFramerate = (frameRate << 16); // Q16 format |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 962 | video_def->eCompressionFormat = OMX_VIDEO_CodingUnused; |
| 963 | video_def->eColorFormat = colorFormat; |
| 964 | |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 965 | err = mOMX->setParameter( |
| 966 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 967 | CHECK_EQ(err, (status_t)OK); |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 968 | |
| 969 | //////////////////////// Output port ///////////////////////// |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 970 | CHECK_EQ(setVideoPortFormatType( |
| 971 | kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused), |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 972 | (status_t)OK); |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 973 | InitOMXParams(&def); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 974 | def.nPortIndex = kPortIndexOutput; |
| 975 | |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 976 | err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 977 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| 978 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 979 | CHECK_EQ(err, (status_t)OK); |
| 980 | CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 981 | |
| 982 | video_def->nFrameWidth = width; |
| 983 | video_def->nFrameHeight = height; |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 984 | video_def->xFramerate = 0; // No need for output port |
James Dong | 4f501f0 | 2010-06-07 14:41:41 -0700 | [diff] [blame] | 985 | video_def->nBitrate = bitRate; // Q16 format |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 986 | video_def->eCompressionFormat = compressionFormat; |
| 987 | video_def->eColorFormat = OMX_COLOR_FormatUnused; |
James Dong | 90862e2 | 2010-08-26 19:12:59 -0700 | [diff] [blame] | 988 | if (mQuirks & kRequiresLargerEncoderOutputBuffer) { |
| 989 | // Increases the output buffer size |
| 990 | def.nBufferSize = ((def.nBufferSize * 3) >> 1); |
| 991 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 992 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 993 | err = mOMX->setParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 994 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 995 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 996 | |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 997 | /////////////////// Codec-specific //////////////////////// |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 998 | switch (compressionFormat) { |
| 999 | case OMX_VIDEO_CodingMPEG4: |
| 1000 | { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1001 | CHECK_EQ(setupMPEG4EncoderParameters(meta), (status_t)OK); |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1002 | break; |
| 1003 | } |
| 1004 | |
| 1005 | case OMX_VIDEO_CodingH263: |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1006 | CHECK_EQ(setupH263EncoderParameters(meta), (status_t)OK); |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1007 | break; |
| 1008 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1009 | case OMX_VIDEO_CodingAVC: |
| 1010 | { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1011 | CHECK_EQ(setupAVCEncoderParameters(meta), (status_t)OK); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1012 | break; |
| 1013 | } |
| 1014 | |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1015 | default: |
| 1016 | CHECK(!"Support for this compressionFormat to be implemented."); |
| 1017 | break; |
| 1018 | } |
| 1019 | } |
| 1020 | |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 1021 | static OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) { |
| 1022 | if (iFramesInterval < 0) { |
| 1023 | return 0xFFFFFFFF; |
| 1024 | } else if (iFramesInterval == 0) { |
| 1025 | return 0; |
| 1026 | } |
| 1027 | OMX_U32 ret = frameRate * iFramesInterval; |
| 1028 | CHECK(ret > 1); |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1032 | status_t OMXCodec::setupErrorCorrectionParameters() { |
| 1033 | OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType; |
| 1034 | InitOMXParams(&errorCorrectionType); |
| 1035 | errorCorrectionType.nPortIndex = kPortIndexOutput; |
| 1036 | |
| 1037 | status_t err = mOMX->getParameter( |
| 1038 | mNode, OMX_IndexParamVideoErrorCorrection, |
| 1039 | &errorCorrectionType, sizeof(errorCorrectionType)); |
James Dong | 903fc22 | 2010-09-22 17:37:42 -0700 | [diff] [blame] | 1040 | if (err != OK) { |
| 1041 | LOGW("Error correction param query is not supported"); |
| 1042 | return OK; // Optional feature. Ignore this failure |
| 1043 | } |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1044 | |
| 1045 | errorCorrectionType.bEnableHEC = OMX_FALSE; |
| 1046 | errorCorrectionType.bEnableResync = OMX_TRUE; |
| 1047 | errorCorrectionType.nResynchMarkerSpacing = 256; |
| 1048 | errorCorrectionType.bEnableDataPartitioning = OMX_FALSE; |
| 1049 | errorCorrectionType.bEnableRVLC = OMX_FALSE; |
| 1050 | |
| 1051 | err = mOMX->setParameter( |
| 1052 | mNode, OMX_IndexParamVideoErrorCorrection, |
| 1053 | &errorCorrectionType, sizeof(errorCorrectionType)); |
James Dong | 903fc22 | 2010-09-22 17:37:42 -0700 | [diff] [blame] | 1054 | if (err != OK) { |
| 1055 | LOGW("Error correction param configuration is not supported"); |
| 1056 | } |
| 1057 | |
| 1058 | // Optional feature. Ignore the failure. |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1059 | return OK; |
| 1060 | } |
| 1061 | |
| 1062 | status_t OMXCodec::setupBitRate(int32_t bitRate) { |
| 1063 | OMX_VIDEO_PARAM_BITRATETYPE bitrateType; |
| 1064 | InitOMXParams(&bitrateType); |
| 1065 | bitrateType.nPortIndex = kPortIndexOutput; |
| 1066 | |
| 1067 | status_t err = mOMX->getParameter( |
| 1068 | mNode, OMX_IndexParamVideoBitrate, |
| 1069 | &bitrateType, sizeof(bitrateType)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1070 | CHECK_EQ(err, (status_t)OK); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1071 | |
| 1072 | bitrateType.eControlRate = OMX_Video_ControlRateVariable; |
| 1073 | bitrateType.nTargetBitrate = bitRate; |
| 1074 | |
| 1075 | err = mOMX->setParameter( |
| 1076 | mNode, OMX_IndexParamVideoBitrate, |
| 1077 | &bitrateType, sizeof(bitrateType)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1078 | CHECK_EQ(err, (status_t)OK); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1079 | return OK; |
| 1080 | } |
| 1081 | |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1082 | status_t OMXCodec::getVideoProfileLevel( |
| 1083 | const sp<MetaData>& meta, |
| 1084 | const CodecProfileLevel& defaultProfileLevel, |
| 1085 | CodecProfileLevel &profileLevel) { |
| 1086 | CODEC_LOGV("Default profile: %ld, level %ld", |
| 1087 | defaultProfileLevel.mProfile, defaultProfileLevel.mLevel); |
| 1088 | |
| 1089 | // Are the default profile and level overwriten? |
| 1090 | int32_t profile, level; |
| 1091 | if (!meta->findInt32(kKeyVideoProfile, &profile)) { |
| 1092 | profile = defaultProfileLevel.mProfile; |
| 1093 | } |
| 1094 | if (!meta->findInt32(kKeyVideoLevel, &level)) { |
| 1095 | level = defaultProfileLevel.mLevel; |
| 1096 | } |
| 1097 | CODEC_LOGV("Target profile: %d, level: %d", profile, level); |
| 1098 | |
| 1099 | // Are the target profile and level supported by the encoder? |
| 1100 | OMX_VIDEO_PARAM_PROFILELEVELTYPE param; |
| 1101 | InitOMXParams(¶m); |
| 1102 | param.nPortIndex = kPortIndexOutput; |
| 1103 | for (param.nProfileIndex = 0;; ++param.nProfileIndex) { |
| 1104 | status_t err = mOMX->getParameter( |
| 1105 | mNode, OMX_IndexParamVideoProfileLevelQuerySupported, |
| 1106 | ¶m, sizeof(param)); |
| 1107 | |
James Dong | dfb8991 | 2010-09-15 21:07:52 -0700 | [diff] [blame] | 1108 | if (err != OK) break; |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1109 | |
| 1110 | int32_t supportedProfile = static_cast<int32_t>(param.eProfile); |
| 1111 | int32_t supportedLevel = static_cast<int32_t>(param.eLevel); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1112 | CODEC_LOGV("Supported profile: %d, level %d", |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1113 | supportedProfile, supportedLevel); |
| 1114 | |
| 1115 | if (profile == supportedProfile && |
James Dong | dfb8991 | 2010-09-15 21:07:52 -0700 | [diff] [blame] | 1116 | level <= supportedLevel) { |
| 1117 | // We can further check whether the level is a valid |
| 1118 | // value; but we will leave that to the omx encoder component |
| 1119 | // via OMX_SetParameter call. |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1120 | profileLevel.mProfile = profile; |
| 1121 | profileLevel.mLevel = level; |
| 1122 | return OK; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | CODEC_LOGE("Target profile (%d) and level (%d) is not supported", |
| 1127 | profile, level); |
| 1128 | return BAD_VALUE; |
| 1129 | } |
| 1130 | |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1131 | status_t OMXCodec::setupH263EncoderParameters(const sp<MetaData>& meta) { |
| 1132 | int32_t iFramesInterval, frameRate, bitRate; |
| 1133 | bool success = meta->findInt32(kKeyBitRate, &bitRate); |
James Dong | aac193c | 2010-11-10 20:43:53 -0800 | [diff] [blame] | 1134 | success = success && meta->findInt32(kKeyFrameRate, &frameRate); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1135 | success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); |
| 1136 | CHECK(success); |
| 1137 | OMX_VIDEO_PARAM_H263TYPE h263type; |
| 1138 | InitOMXParams(&h263type); |
| 1139 | h263type.nPortIndex = kPortIndexOutput; |
| 1140 | |
| 1141 | status_t err = mOMX->getParameter( |
| 1142 | mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1143 | CHECK_EQ(err, (status_t)OK); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1144 | |
| 1145 | h263type.nAllowedPictureTypes = |
| 1146 | OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; |
| 1147 | |
| 1148 | h263type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); |
| 1149 | if (h263type.nPFrames == 0) { |
| 1150 | h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; |
| 1151 | } |
| 1152 | h263type.nBFrames = 0; |
| 1153 | |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1154 | // Check profile and level parameters |
| 1155 | CodecProfileLevel defaultProfileLevel, profileLevel; |
James Dong | 1e0e166 | 2010-09-22 17:42:09 -0700 | [diff] [blame] | 1156 | defaultProfileLevel.mProfile = h263type.eProfile; |
| 1157 | defaultProfileLevel.mLevel = h263type.eLevel; |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1158 | err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel); |
| 1159 | if (err != OK) return err; |
| 1160 | h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profileLevel.mProfile); |
| 1161 | h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(profileLevel.mLevel); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1162 | |
| 1163 | h263type.bPLUSPTYPEAllowed = OMX_FALSE; |
| 1164 | h263type.bForceRoundingTypeToZero = OMX_FALSE; |
| 1165 | h263type.nPictureHeaderRepetition = 0; |
| 1166 | h263type.nGOBHeaderInterval = 0; |
| 1167 | |
| 1168 | err = mOMX->setParameter( |
| 1169 | mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1170 | CHECK_EQ(err, (status_t)OK); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1171 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1172 | CHECK_EQ(setupBitRate(bitRate), (status_t)OK); |
| 1173 | CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1174 | |
| 1175 | return OK; |
| 1176 | } |
| 1177 | |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 1178 | status_t OMXCodec::setupMPEG4EncoderParameters(const sp<MetaData>& meta) { |
| 1179 | int32_t iFramesInterval, frameRate, bitRate; |
| 1180 | bool success = meta->findInt32(kKeyBitRate, &bitRate); |
James Dong | aac193c | 2010-11-10 20:43:53 -0800 | [diff] [blame] | 1181 | success = success && meta->findInt32(kKeyFrameRate, &frameRate); |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 1182 | success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); |
| 1183 | CHECK(success); |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1184 | OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type; |
| 1185 | InitOMXParams(&mpeg4type); |
| 1186 | mpeg4type.nPortIndex = kPortIndexOutput; |
| 1187 | |
| 1188 | status_t err = mOMX->getParameter( |
| 1189 | mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1190 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1191 | |
| 1192 | mpeg4type.nSliceHeaderSpacing = 0; |
| 1193 | mpeg4type.bSVH = OMX_FALSE; |
| 1194 | mpeg4type.bGov = OMX_FALSE; |
| 1195 | |
| 1196 | mpeg4type.nAllowedPictureTypes = |
| 1197 | OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; |
| 1198 | |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 1199 | mpeg4type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); |
| 1200 | if (mpeg4type.nPFrames == 0) { |
| 1201 | mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; |
| 1202 | } |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1203 | mpeg4type.nBFrames = 0; |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1204 | mpeg4type.nIDCVLCThreshold = 0; |
| 1205 | mpeg4type.bACPred = OMX_TRUE; |
| 1206 | mpeg4type.nMaxPacketSize = 256; |
| 1207 | mpeg4type.nTimeIncRes = 1000; |
| 1208 | mpeg4type.nHeaderExtension = 0; |
| 1209 | mpeg4type.bReversibleVLC = OMX_FALSE; |
| 1210 | |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1211 | // Check profile and level parameters |
| 1212 | CodecProfileLevel defaultProfileLevel, profileLevel; |
James Dong | 1e0e166 | 2010-09-22 17:42:09 -0700 | [diff] [blame] | 1213 | defaultProfileLevel.mProfile = mpeg4type.eProfile; |
| 1214 | defaultProfileLevel.mLevel = mpeg4type.eLevel; |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1215 | err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel); |
| 1216 | if (err != OK) return err; |
| 1217 | mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profileLevel.mProfile); |
| 1218 | mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(profileLevel.mLevel); |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1219 | |
| 1220 | err = mOMX->setParameter( |
| 1221 | mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1222 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1223 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1224 | CHECK_EQ(setupBitRate(bitRate), (status_t)OK); |
| 1225 | CHECK_EQ(setupErrorCorrectionParameters(), (status_t)OK); |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1226 | |
| 1227 | return OK; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 1230 | status_t OMXCodec::setupAVCEncoderParameters(const sp<MetaData>& meta) { |
| 1231 | int32_t iFramesInterval, frameRate, bitRate; |
| 1232 | bool success = meta->findInt32(kKeyBitRate, &bitRate); |
James Dong | aac193c | 2010-11-10 20:43:53 -0800 | [diff] [blame] | 1233 | success = success && meta->findInt32(kKeyFrameRate, &frameRate); |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 1234 | success = success && meta->findInt32(kKeyIFramesInterval, &iFramesInterval); |
| 1235 | CHECK(success); |
| 1236 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1237 | OMX_VIDEO_PARAM_AVCTYPE h264type; |
| 1238 | InitOMXParams(&h264type); |
| 1239 | h264type.nPortIndex = kPortIndexOutput; |
| 1240 | |
| 1241 | status_t err = mOMX->getParameter( |
| 1242 | mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1243 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1244 | |
| 1245 | h264type.nAllowedPictureTypes = |
| 1246 | OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP; |
| 1247 | |
| 1248 | h264type.nSliceHeaderSpacing = 0; |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 1249 | h264type.nBFrames = 0; // No B frames support yet |
| 1250 | h264type.nPFrames = setPFramesSpacing(iFramesInterval, frameRate); |
| 1251 | if (h264type.nPFrames == 0) { |
| 1252 | h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI; |
| 1253 | } |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1254 | |
| 1255 | // Check profile and level parameters |
| 1256 | CodecProfileLevel defaultProfileLevel, profileLevel; |
| 1257 | defaultProfileLevel.mProfile = h264type.eProfile; |
| 1258 | defaultProfileLevel.mLevel = h264type.eLevel; |
| 1259 | err = getVideoProfileLevel(meta, defaultProfileLevel, profileLevel); |
| 1260 | if (err != OK) return err; |
| 1261 | h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profileLevel.mProfile); |
| 1262 | h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(profileLevel.mLevel); |
| 1263 | |
| 1264 | if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) { |
| 1265 | h264type.bUseHadamard = OMX_TRUE; |
| 1266 | h264type.nRefFrames = 1; |
| 1267 | h264type.nRefIdx10ActiveMinus1 = 0; |
| 1268 | h264type.nRefIdx11ActiveMinus1 = 0; |
| 1269 | h264type.bEntropyCodingCABAC = OMX_FALSE; |
| 1270 | h264type.bWeightedPPrediction = OMX_FALSE; |
| 1271 | h264type.bconstIpred = OMX_FALSE; |
| 1272 | h264type.bDirect8x8Inference = OMX_FALSE; |
| 1273 | h264type.bDirectSpatialTemporal = OMX_FALSE; |
| 1274 | h264type.nCabacInitIdc = 0; |
| 1275 | } |
| 1276 | |
| 1277 | if (h264type.nBFrames != 0) { |
| 1278 | h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB; |
| 1279 | } |
| 1280 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1281 | h264type.bEnableUEP = OMX_FALSE; |
| 1282 | h264type.bEnableFMO = OMX_FALSE; |
| 1283 | h264type.bEnableASO = OMX_FALSE; |
| 1284 | h264type.bEnableRS = OMX_FALSE; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1285 | h264type.bFrameMBsOnly = OMX_TRUE; |
| 1286 | h264type.bMBAFF = OMX_FALSE; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1287 | h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable; |
| 1288 | |
pgudadhe | 9c30532 | 2010-07-26 13:59:29 -0700 | [diff] [blame] | 1289 | if (!strcasecmp("OMX.Nvidia.h264.encoder", mComponentName)) { |
| 1290 | h264type.eLevel = OMX_VIDEO_AVCLevelMax; |
| 1291 | } |
| 1292 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1293 | err = mOMX->setParameter( |
| 1294 | mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1295 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1296 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1297 | CHECK_EQ(setupBitRate(bitRate), (status_t)OK); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1298 | |
| 1299 | return OK; |
| 1300 | } |
| 1301 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 1302 | status_t OMXCodec::setVideoOutputFormat( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1303 | const char *mime, OMX_U32 width, OMX_U32 height) { |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 1304 | CODEC_LOGV("setVideoOutputFormat width=%ld, height=%ld", width, height); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1305 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1306 | OMX_VIDEO_CODINGTYPE compressionFormat = OMX_VIDEO_CodingUnused; |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1307 | if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1308 | compressionFormat = OMX_VIDEO_CodingAVC; |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1309 | } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1310 | compressionFormat = OMX_VIDEO_CodingMPEG4; |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1311 | } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1312 | compressionFormat = OMX_VIDEO_CodingH263; |
| 1313 | } else { |
| 1314 | LOGE("Not a supported video mime type: %s", mime); |
| 1315 | CHECK(!"Should not be here. Not a supported video mime type."); |
| 1316 | } |
| 1317 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 1318 | status_t err = setVideoPortFormatType( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1319 | kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused); |
| 1320 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 1321 | if (err != OK) { |
| 1322 | return err; |
| 1323 | } |
| 1324 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1325 | #if 1 |
| 1326 | { |
| 1327 | OMX_VIDEO_PARAM_PORTFORMATTYPE format; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1328 | InitOMXParams(&format); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1329 | format.nPortIndex = kPortIndexOutput; |
| 1330 | format.nIndex = 0; |
| 1331 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1332 | status_t err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1333 | mNode, OMX_IndexParamVideoPortFormat, |
| 1334 | &format, sizeof(format)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1335 | CHECK_EQ(err, (status_t)OK); |
| 1336 | CHECK_EQ((int)format.eCompressionFormat, (int)OMX_VIDEO_CodingUnused); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1337 | |
| 1338 | static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00; |
| 1339 | |
| 1340 | CHECK(format.eColorFormat == OMX_COLOR_FormatYUV420Planar |
| 1341 | || format.eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar |
| 1342 | || format.eColorFormat == OMX_COLOR_FormatCbYCrY |
| 1343 | || format.eColorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar); |
| 1344 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1345 | err = mOMX->setParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1346 | mNode, OMX_IndexParamVideoPortFormat, |
| 1347 | &format, sizeof(format)); |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 1348 | |
| 1349 | if (err != OK) { |
| 1350 | return err; |
| 1351 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1352 | } |
| 1353 | #endif |
| 1354 | |
| 1355 | OMX_PARAM_PORTDEFINITIONTYPE def; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1356 | InitOMXParams(&def); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1357 | def.nPortIndex = kPortIndexInput; |
| 1358 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1359 | OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; |
| 1360 | |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 1361 | err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1362 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| 1363 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1364 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1365 | |
| 1366 | #if 1 |
| 1367 | // XXX Need a (much) better heuristic to compute input buffer sizes. |
| 1368 | const size_t X = 64 * 1024; |
| 1369 | if (def.nBufferSize < X) { |
| 1370 | def.nBufferSize = X; |
| 1371 | } |
| 1372 | #endif |
| 1373 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1374 | CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1375 | |
| 1376 | video_def->nFrameWidth = width; |
| 1377 | video_def->nFrameHeight = height; |
| 1378 | |
Andreas Huber | b482ce8 | 2009-10-29 12:02:48 -0700 | [diff] [blame] | 1379 | video_def->eCompressionFormat = compressionFormat; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1380 | video_def->eColorFormat = OMX_COLOR_FormatUnused; |
| 1381 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1382 | err = mOMX->setParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1383 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 1384 | |
| 1385 | if (err != OK) { |
| 1386 | return err; |
| 1387 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1388 | |
| 1389 | //////////////////////////////////////////////////////////////////////////// |
| 1390 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1391 | InitOMXParams(&def); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1392 | def.nPortIndex = kPortIndexOutput; |
| 1393 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1394 | err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1395 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1396 | CHECK_EQ(err, (status_t)OK); |
| 1397 | CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1398 | |
| 1399 | #if 0 |
| 1400 | def.nBufferSize = |
| 1401 | (((width + 15) & -16) * ((height + 15) & -16) * 3) / 2; // YUV420 |
| 1402 | #endif |
| 1403 | |
| 1404 | video_def->nFrameWidth = width; |
| 1405 | video_def->nFrameHeight = height; |
| 1406 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1407 | err = mOMX->setParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1408 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 2a09c7e | 2010-03-16 11:44:07 -0700 | [diff] [blame] | 1409 | |
| 1410 | return err; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1413 | OMXCodec::OMXCodec( |
| 1414 | const sp<IOMX> &omx, IOMX::node_id node, uint32_t quirks, |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 1415 | bool isEncoder, |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1416 | const char *mime, |
| 1417 | const char *componentName, |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1418 | const sp<MediaSource> &source, |
| 1419 | const sp<ANativeWindow> &nativeWindow) |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1420 | : mOMX(omx), |
Andreas Huber | f1fe064 | 2010-01-15 15:28:19 -0800 | [diff] [blame] | 1421 | mOMXLivesLocally(omx->livesLocally(getpid())), |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1422 | mNode(node), |
| 1423 | mQuirks(quirks), |
| 1424 | mIsEncoder(isEncoder), |
| 1425 | mMIME(strdup(mime)), |
| 1426 | mComponentName(strdup(componentName)), |
| 1427 | mSource(source), |
| 1428 | mCodecSpecificDataIndex(0), |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1429 | mState(LOADED), |
Andreas Huber | 42978e5 | 2009-08-27 10:08:39 -0700 | [diff] [blame] | 1430 | mInitialBufferSubmit(true), |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1431 | mSignalledEOS(false), |
| 1432 | mNoMoreOutputData(false), |
Andreas Huber | cfd5557 | 2009-10-09 14:11:28 -0700 | [diff] [blame] | 1433 | mOutputPortSettingsHaveChanged(false), |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 1434 | mSeekTimeUs(-1), |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 1435 | mSeekMode(ReadOptions::SEEK_CLOSEST_SYNC), |
| 1436 | mTargetTimeUs(-1), |
Andreas Huber | b928983 | 2011-02-08 13:10:25 -0800 | [diff] [blame] | 1437 | mOutputPortSettingsChangedPending(false), |
Andreas Huber | 1f24b30 | 2010-06-10 11:12:39 -0700 | [diff] [blame] | 1438 | mLeftOverBuffer(NULL), |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1439 | mPaused(false), |
| 1440 | mNativeWindow(nativeWindow) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1441 | mPortStatus[kPortIndexInput] = ENABLED; |
| 1442 | mPortStatus[kPortIndexOutput] = ENABLED; |
| 1443 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1444 | setComponentRole(); |
| 1445 | } |
| 1446 | |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1447 | // static |
| 1448 | void OMXCodec::setComponentRole( |
| 1449 | const sp<IOMX> &omx, IOMX::node_id node, bool isEncoder, |
| 1450 | const char *mime) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1451 | struct MimeToRole { |
| 1452 | const char *mime; |
| 1453 | const char *decoderRole; |
| 1454 | const char *encoderRole; |
| 1455 | }; |
| 1456 | |
| 1457 | static const MimeToRole kMimeToRole[] = { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1458 | { MEDIA_MIMETYPE_AUDIO_MPEG, |
| 1459 | "audio_decoder.mp3", "audio_encoder.mp3" }, |
| 1460 | { MEDIA_MIMETYPE_AUDIO_AMR_NB, |
| 1461 | "audio_decoder.amrnb", "audio_encoder.amrnb" }, |
| 1462 | { MEDIA_MIMETYPE_AUDIO_AMR_WB, |
| 1463 | "audio_decoder.amrwb", "audio_encoder.amrwb" }, |
| 1464 | { MEDIA_MIMETYPE_AUDIO_AAC, |
| 1465 | "audio_decoder.aac", "audio_encoder.aac" }, |
| 1466 | { MEDIA_MIMETYPE_VIDEO_AVC, |
| 1467 | "video_decoder.avc", "video_encoder.avc" }, |
| 1468 | { MEDIA_MIMETYPE_VIDEO_MPEG4, |
| 1469 | "video_decoder.mpeg4", "video_encoder.mpeg4" }, |
| 1470 | { MEDIA_MIMETYPE_VIDEO_H263, |
| 1471 | "video_decoder.h263", "video_encoder.h263" }, |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1472 | }; |
| 1473 | |
| 1474 | static const size_t kNumMimeToRole = |
| 1475 | sizeof(kMimeToRole) / sizeof(kMimeToRole[0]); |
| 1476 | |
| 1477 | size_t i; |
| 1478 | for (i = 0; i < kNumMimeToRole; ++i) { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1479 | if (!strcasecmp(mime, kMimeToRole[i].mime)) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1480 | break; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | if (i == kNumMimeToRole) { |
| 1485 | return; |
| 1486 | } |
| 1487 | |
| 1488 | const char *role = |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1489 | isEncoder ? kMimeToRole[i].encoderRole |
| 1490 | : kMimeToRole[i].decoderRole; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1491 | |
| 1492 | if (role != NULL) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1493 | OMX_PARAM_COMPONENTROLETYPE roleParams; |
| 1494 | InitOMXParams(&roleParams); |
| 1495 | |
| 1496 | strncpy((char *)roleParams.cRole, |
| 1497 | role, OMX_MAX_STRINGNAME_SIZE - 1); |
| 1498 | |
| 1499 | roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0'; |
| 1500 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1501 | status_t err = omx->setParameter( |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1502 | node, OMX_IndexParamStandardComponentRole, |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1503 | &roleParams, sizeof(roleParams)); |
| 1504 | |
| 1505 | if (err != OK) { |
| 1506 | LOGW("Failed to set standard component role '%s'.", role); |
| 1507 | } |
| 1508 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 1511 | void OMXCodec::setComponentRole() { |
| 1512 | setComponentRole(mOMX, mNode, mIsEncoder, mMIME); |
| 1513 | } |
| 1514 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1515 | OMXCodec::~OMXCodec() { |
Andreas Huber | f98197a | 2010-09-17 11:49:39 -0700 | [diff] [blame] | 1516 | mSource.clear(); |
| 1517 | |
Andreas Huber | eec06d3 | 2011-01-06 10:26:36 -0800 | [diff] [blame] | 1518 | CHECK(mState == LOADED || mState == ERROR || mState == LOADED_TO_IDLE); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1519 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1520 | status_t err = mOMX->freeNode(mNode); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1521 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1522 | |
| 1523 | mNode = NULL; |
| 1524 | setState(DEAD); |
| 1525 | |
| 1526 | clearCodecSpecificData(); |
| 1527 | |
| 1528 | free(mComponentName); |
| 1529 | mComponentName = NULL; |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 1530 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1531 | free(mMIME); |
| 1532 | mMIME = NULL; |
| 1533 | } |
| 1534 | |
| 1535 | status_t OMXCodec::init() { |
Andreas Huber | 42978e5 | 2009-08-27 10:08:39 -0700 | [diff] [blame] | 1536 | // mLock is held. |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1537 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1538 | CHECK_EQ((int)mState, (int)LOADED); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1539 | |
| 1540 | status_t err; |
| 1541 | if (!(mQuirks & kRequiresLoadedToIdleAfterAllocation)) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1542 | err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1543 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1544 | setState(LOADED_TO_IDLE); |
| 1545 | } |
| 1546 | |
| 1547 | err = allocateBuffers(); |
Jamie Gennis | fd8b75a | 2010-12-17 15:07:02 -0800 | [diff] [blame] | 1548 | if (err != (status_t)OK) { |
| 1549 | return err; |
| 1550 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1551 | |
| 1552 | if (mQuirks & kRequiresLoadedToIdleAfterAllocation) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1553 | err = mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1554 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1555 | |
| 1556 | setState(LOADED_TO_IDLE); |
| 1557 | } |
| 1558 | |
| 1559 | while (mState != EXECUTING && mState != ERROR) { |
| 1560 | mAsyncCompletion.wait(mLock); |
| 1561 | } |
| 1562 | |
| 1563 | return mState == ERROR ? UNKNOWN_ERROR : OK; |
| 1564 | } |
| 1565 | |
| 1566 | // static |
| 1567 | bool OMXCodec::isIntermediateState(State state) { |
| 1568 | return state == LOADED_TO_IDLE |
| 1569 | || state == IDLE_TO_EXECUTING |
| 1570 | || state == EXECUTING_TO_IDLE |
| 1571 | || state == IDLE_TO_LOADED |
| 1572 | || state == RECONFIGURING; |
| 1573 | } |
| 1574 | |
| 1575 | status_t OMXCodec::allocateBuffers() { |
| 1576 | status_t err = allocateBuffersOnPort(kPortIndexInput); |
| 1577 | |
| 1578 | if (err != OK) { |
| 1579 | return err; |
| 1580 | } |
| 1581 | |
| 1582 | return allocateBuffersOnPort(kPortIndexOutput); |
| 1583 | } |
| 1584 | |
| 1585 | status_t OMXCodec::allocateBuffersOnPort(OMX_U32 portIndex) { |
Jamie Gennis | dbfb32e | 2010-10-20 15:53:59 -0700 | [diff] [blame] | 1586 | if (mNativeWindow != NULL && portIndex == kPortIndexOutput) { |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1587 | return allocateOutputBuffersFromNativeWindow(); |
| 1588 | } |
| 1589 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1590 | OMX_PARAM_PORTDEFINITIONTYPE def; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1591 | InitOMXParams(&def); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1592 | def.nPortIndex = portIndex; |
| 1593 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1594 | status_t err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1595 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| 1596 | |
| 1597 | if (err != OK) { |
| 1598 | return err; |
| 1599 | } |
| 1600 | |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 1601 | if (mIsMetaDataStoredInVideoBuffers && portIndex == kPortIndexInput) { |
| 1602 | err = mOMX->storeMetaDataInBuffers(mNode, kPortIndexInput, OMX_TRUE); |
| 1603 | if (err != OK) { |
| 1604 | LOGE("Storing meta data in video buffers is not supported"); |
| 1605 | return err; |
| 1606 | } |
| 1607 | } |
| 1608 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1609 | CODEC_LOGI("allocating %lu buffers of size %lu on %s port", |
| 1610 | def.nBufferCountActual, def.nBufferSize, |
| 1611 | portIndex == kPortIndexInput ? "input" : "output"); |
| 1612 | |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 1613 | size_t totalSize = def.nBufferCountActual * def.nBufferSize; |
Mathias Agopian | 6faf789 | 2010-01-25 19:00:00 -0800 | [diff] [blame] | 1614 | mDealer[portIndex] = new MemoryDealer(totalSize, "OMXCodec"); |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 1615 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1616 | for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) { |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 1617 | sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1618 | CHECK(mem.get() != NULL); |
| 1619 | |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 1620 | BufferInfo info; |
| 1621 | info.mData = NULL; |
| 1622 | info.mSize = def.nBufferSize; |
| 1623 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1624 | IOMX::buffer_id buffer; |
| 1625 | if (portIndex == kPortIndexInput |
| 1626 | && (mQuirks & kRequiresAllocateBufferOnInputPorts)) { |
Andreas Huber | f1fe064 | 2010-01-15 15:28:19 -0800 | [diff] [blame] | 1627 | if (mOMXLivesLocally) { |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 1628 | mem.clear(); |
| 1629 | |
Andreas Huber | f1fe064 | 2010-01-15 15:28:19 -0800 | [diff] [blame] | 1630 | err = mOMX->allocateBuffer( |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 1631 | mNode, portIndex, def.nBufferSize, &buffer, |
| 1632 | &info.mData); |
Andreas Huber | f1fe064 | 2010-01-15 15:28:19 -0800 | [diff] [blame] | 1633 | } else { |
| 1634 | err = mOMX->allocateBufferWithBackup( |
| 1635 | mNode, portIndex, mem, &buffer); |
| 1636 | } |
Andreas Huber | 446f44f | 2009-08-25 17:23:44 -0700 | [diff] [blame] | 1637 | } else if (portIndex == kPortIndexOutput |
| 1638 | && (mQuirks & kRequiresAllocateBufferOnOutputPorts)) { |
Andreas Huber | f1fe064 | 2010-01-15 15:28:19 -0800 | [diff] [blame] | 1639 | if (mOMXLivesLocally) { |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 1640 | mem.clear(); |
| 1641 | |
Andreas Huber | f1fe064 | 2010-01-15 15:28:19 -0800 | [diff] [blame] | 1642 | err = mOMX->allocateBuffer( |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 1643 | mNode, portIndex, def.nBufferSize, &buffer, |
| 1644 | &info.mData); |
Andreas Huber | f1fe064 | 2010-01-15 15:28:19 -0800 | [diff] [blame] | 1645 | } else { |
| 1646 | err = mOMX->allocateBufferWithBackup( |
| 1647 | mNode, portIndex, mem, &buffer); |
| 1648 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1649 | } else { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 1650 | err = mOMX->useBuffer(mNode, portIndex, mem, &buffer); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | if (err != OK) { |
| 1654 | LOGE("allocate_buffer_with_backup failed"); |
| 1655 | return err; |
| 1656 | } |
| 1657 | |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 1658 | if (mem != NULL) { |
| 1659 | info.mData = mem->pointer(); |
| 1660 | } |
| 1661 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1662 | info.mBuffer = buffer; |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 1663 | info.mStatus = OWNED_BY_US; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1664 | info.mMem = mem; |
| 1665 | info.mMediaBuffer = NULL; |
| 1666 | |
| 1667 | if (portIndex == kPortIndexOutput) { |
Andreas Huber | 52733b8 | 2010-01-25 10:41:35 -0800 | [diff] [blame] | 1668 | if (!(mOMXLivesLocally |
| 1669 | && (mQuirks & kRequiresAllocateBufferOnOutputPorts) |
| 1670 | && (mQuirks & kDefersOutputBufferAllocation))) { |
| 1671 | // If the node does not fill in the buffer ptr at this time, |
| 1672 | // we will defer creating the MediaBuffer until receiving |
| 1673 | // the first FILL_BUFFER_DONE notification instead. |
| 1674 | info.mMediaBuffer = new MediaBuffer(info.mData, info.mSize); |
| 1675 | info.mMediaBuffer->setObserver(this); |
| 1676 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | mPortBuffers[portIndex].push(info); |
| 1680 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1681 | CODEC_LOGV("allocated buffer %p on %s port", buffer, |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1682 | portIndex == kPortIndexInput ? "input" : "output"); |
| 1683 | } |
| 1684 | |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 1685 | // dumpPortStatus(portIndex); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1686 | |
| 1687 | return OK; |
| 1688 | } |
| 1689 | |
Andreas Huber | 5e9dc94 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 1690 | status_t OMXCodec::applyRotation() { |
| 1691 | sp<MetaData> meta = mSource->getFormat(); |
| 1692 | |
| 1693 | int32_t rotationDegrees; |
| 1694 | if (!meta->findInt32(kKeyRotation, &rotationDegrees)) { |
| 1695 | rotationDegrees = 0; |
| 1696 | } |
| 1697 | |
| 1698 | uint32_t transform; |
| 1699 | switch (rotationDegrees) { |
| 1700 | case 0: transform = 0; break; |
| 1701 | case 90: transform = HAL_TRANSFORM_ROT_90; break; |
| 1702 | case 180: transform = HAL_TRANSFORM_ROT_180; break; |
| 1703 | case 270: transform = HAL_TRANSFORM_ROT_270; break; |
| 1704 | default: transform = 0; break; |
| 1705 | } |
| 1706 | |
| 1707 | status_t err = OK; |
| 1708 | |
| 1709 | if (transform) { |
| 1710 | err = native_window_set_buffers_transform( |
| 1711 | mNativeWindow.get(), transform); |
| 1712 | } |
| 1713 | |
| 1714 | return err; |
| 1715 | } |
| 1716 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1717 | status_t OMXCodec::allocateOutputBuffersFromNativeWindow() { |
| 1718 | // Get the number of buffers needed. |
| 1719 | OMX_PARAM_PORTDEFINITIONTYPE def; |
| 1720 | InitOMXParams(&def); |
| 1721 | def.nPortIndex = kPortIndexOutput; |
| 1722 | |
| 1723 | status_t err = mOMX->getParameter( |
| 1724 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| 1725 | if (err != OK) { |
| 1726 | return err; |
| 1727 | } |
| 1728 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1729 | err = native_window_set_buffers_geometry( |
| 1730 | mNativeWindow.get(), |
| 1731 | def.format.video.nFrameWidth, |
| 1732 | def.format.video.nFrameHeight, |
Jamie Gennis | 044ace6 | 2010-10-29 15:19:29 -0700 | [diff] [blame] | 1733 | def.format.video.eColorFormat); |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1734 | |
| 1735 | if (err != 0) { |
| 1736 | LOGE("native_window_set_buffers_geometry failed: %s (%d)", |
| 1737 | strerror(-err), -err); |
| 1738 | return err; |
| 1739 | } |
| 1740 | |
| 1741 | // Increase the buffer count by one to allow for the ANativeWindow to hold |
| 1742 | // on to one of the buffers. |
| 1743 | def.nBufferCountActual++; |
| 1744 | err = mOMX->setParameter( |
| 1745 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
| 1746 | if (err != OK) { |
| 1747 | return err; |
| 1748 | } |
| 1749 | |
Andreas Huber | 5e9dc94 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 1750 | err = applyRotation(); |
| 1751 | if (err != OK) { |
| 1752 | return err; |
| 1753 | } |
| 1754 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1755 | // Set up the native window. |
| 1756 | // XXX TODO: Get the gralloc usage flags from the OMX plugin! |
| 1757 | err = native_window_set_usage( |
Jamie Gennis | b04c1503 | 2010-11-17 18:57:13 -0800 | [diff] [blame] | 1758 | mNativeWindow.get(), GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP); |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1759 | if (err != 0) { |
| 1760 | LOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err); |
| 1761 | return err; |
| 1762 | } |
| 1763 | |
| 1764 | err = native_window_set_buffer_count( |
| 1765 | mNativeWindow.get(), def.nBufferCountActual); |
| 1766 | if (err != 0) { |
| 1767 | LOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err), |
| 1768 | -err); |
| 1769 | return err; |
| 1770 | } |
| 1771 | |
| 1772 | // XXX TODO: Do something so the ANativeWindow knows that we'll need to get |
| 1773 | // the same set of buffers. |
| 1774 | |
| 1775 | CODEC_LOGI("allocating %lu buffers from a native window of size %lu on " |
| 1776 | "output port", def.nBufferCountActual, def.nBufferSize); |
| 1777 | |
| 1778 | // Dequeue buffers and send them to OMX |
| 1779 | OMX_U32 i; |
| 1780 | for (i = 0; i < def.nBufferCountActual; i++) { |
| 1781 | android_native_buffer_t* buf; |
| 1782 | err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf); |
| 1783 | if (err != 0) { |
| 1784 | LOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err); |
| 1785 | break; |
| 1786 | } |
| 1787 | |
| 1788 | sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false)); |
| 1789 | IOMX::buffer_id bufferId; |
| 1790 | err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer, |
| 1791 | &bufferId); |
| 1792 | if (err != 0) { |
| 1793 | break; |
| 1794 | } |
| 1795 | |
| 1796 | CODEC_LOGV("registered graphic buffer with ID %p (pointer = %p)", |
| 1797 | bufferId, graphicBuffer.get()); |
| 1798 | |
| 1799 | BufferInfo info; |
| 1800 | info.mData = NULL; |
| 1801 | info.mSize = def.nBufferSize; |
| 1802 | info.mBuffer = bufferId; |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 1803 | info.mStatus = OWNED_BY_US; |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1804 | info.mMem = NULL; |
| 1805 | info.mMediaBuffer = new MediaBuffer(graphicBuffer); |
| 1806 | info.mMediaBuffer->setObserver(this); |
| 1807 | |
| 1808 | mPortBuffers[kPortIndexOutput].push(info); |
| 1809 | } |
| 1810 | |
| 1811 | OMX_U32 cancelStart; |
| 1812 | OMX_U32 cancelEnd; |
| 1813 | |
| 1814 | if (err != 0) { |
| 1815 | // If an error occurred while dequeuing we need to cancel any buffers |
| 1816 | // that were dequeued. |
| 1817 | cancelStart = 0; |
| 1818 | cancelEnd = i; |
| 1819 | } else { |
| 1820 | // Return the last two buffers to the native window. |
| 1821 | // XXX TODO: The number of buffers the native window owns should probably be |
| 1822 | // queried from it when we put the native window in fixed buffer pool mode |
| 1823 | // (which needs to be implemented). Currently it's hard-coded to 2. |
| 1824 | cancelStart = def.nBufferCountActual - 2; |
| 1825 | cancelEnd = def.nBufferCountActual; |
| 1826 | } |
| 1827 | |
| 1828 | for (OMX_U32 i = cancelStart; i < cancelEnd; i++) { |
| 1829 | BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(i); |
| 1830 | cancelBufferToNativeWindow(info); |
| 1831 | } |
| 1832 | |
| 1833 | return err; |
| 1834 | } |
| 1835 | |
| 1836 | status_t OMXCodec::cancelBufferToNativeWindow(BufferInfo *info) { |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 1837 | CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US); |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1838 | CODEC_LOGV("Calling cancelBuffer on buffer %p", info->mBuffer); |
| 1839 | int err = mNativeWindow->cancelBuffer( |
| 1840 | mNativeWindow.get(), info->mMediaBuffer->graphicBuffer().get()); |
| 1841 | if (err != 0) { |
| 1842 | CODEC_LOGE("cancelBuffer failed w/ error 0x%08x", err); |
| 1843 | |
| 1844 | setState(ERROR); |
| 1845 | return err; |
| 1846 | } |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 1847 | info->mStatus = OWNED_BY_NATIVE_WINDOW; |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1848 | return OK; |
| 1849 | } |
| 1850 | |
| 1851 | OMXCodec::BufferInfo* OMXCodec::dequeueBufferFromNativeWindow() { |
| 1852 | // Dequeue the next buffer from the native window. |
| 1853 | android_native_buffer_t* buf; |
| 1854 | int err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf); |
| 1855 | if (err != 0) { |
| 1856 | CODEC_LOGE("dequeueBuffer failed w/ error 0x%08x", err); |
| 1857 | |
| 1858 | setState(ERROR); |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
| 1862 | // Determine which buffer we just dequeued. |
| 1863 | Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; |
| 1864 | BufferInfo *bufInfo = 0; |
| 1865 | for (size_t i = 0; i < buffers->size(); i++) { |
| 1866 | sp<GraphicBuffer> graphicBuffer = buffers->itemAt(i). |
| 1867 | mMediaBuffer->graphicBuffer(); |
| 1868 | if (graphicBuffer->handle == buf->handle) { |
| 1869 | bufInfo = &buffers->editItemAt(i); |
| 1870 | break; |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | if (bufInfo == 0) { |
| 1875 | CODEC_LOGE("dequeued unrecognized buffer: %p", buf); |
| 1876 | |
| 1877 | setState(ERROR); |
| 1878 | return 0; |
| 1879 | } |
| 1880 | |
| 1881 | // The native window no longer owns the buffer. |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 1882 | CHECK_EQ((int)bufInfo->mStatus, (int)OWNED_BY_NATIVE_WINDOW); |
| 1883 | bufInfo->mStatus = OWNED_BY_US; |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 1884 | |
| 1885 | return bufInfo; |
| 1886 | } |
| 1887 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1888 | void OMXCodec::on_message(const omx_message &msg) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1889 | switch (msg.type) { |
| 1890 | case omx_message::EVENT: |
| 1891 | { |
| 1892 | onEvent( |
| 1893 | msg.u.event_data.event, msg.u.event_data.data1, |
| 1894 | msg.u.event_data.data2); |
| 1895 | |
| 1896 | break; |
| 1897 | } |
| 1898 | |
| 1899 | case omx_message::EMPTY_BUFFER_DONE: |
| 1900 | { |
| 1901 | IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer; |
| 1902 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1903 | CODEC_LOGV("EMPTY_BUFFER_DONE(buffer: %p)", buffer); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1904 | |
| 1905 | Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; |
| 1906 | size_t i = 0; |
| 1907 | while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) { |
| 1908 | ++i; |
| 1909 | } |
| 1910 | |
| 1911 | CHECK(i < buffers->size()); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 1912 | if ((*buffers)[i].mStatus != OWNED_BY_COMPONENT) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1913 | LOGW("We already own input buffer %p, yet received " |
| 1914 | "an EMPTY_BUFFER_DONE.", buffer); |
| 1915 | } |
| 1916 | |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 1917 | BufferInfo* info = &buffers->editItemAt(i); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 1918 | info->mStatus = OWNED_BY_US; |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 1919 | |
| 1920 | // Buffer could not be released until empty buffer done is called. |
| 1921 | if (info->mMediaBuffer != NULL) { |
James Dong | 31b9375f | 2010-11-10 21:11:41 -0800 | [diff] [blame] | 1922 | if (mIsEncoder && |
| 1923 | (mQuirks & kAvoidMemcopyInputRecordingFrames)) { |
| 1924 | // If zero-copy mode is enabled this will send the |
| 1925 | // input buffer back to the upstream source. |
| 1926 | restorePatchedDataPointer(info); |
| 1927 | } |
| 1928 | |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 1929 | info->mMediaBuffer->release(); |
| 1930 | info->mMediaBuffer = NULL; |
| 1931 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1932 | |
| 1933 | if (mPortStatus[kPortIndexInput] == DISABLING) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1934 | CODEC_LOGV("Port is disabled, freeing buffer %p", buffer); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1935 | |
Jamie Gennis | f0c5c1e | 2010-11-01 16:04:31 -0700 | [diff] [blame] | 1936 | status_t err = freeBuffer(kPortIndexInput, i); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1937 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | 4a9375e | 2010-02-09 11:54:33 -0800 | [diff] [blame] | 1938 | } else if (mState != ERROR |
| 1939 | && mPortStatus[kPortIndexInput] != SHUTTING_DOWN) { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1940 | CHECK_EQ((int)mPortStatus[kPortIndexInput], (int)ENABLED); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1941 | drainInputBuffer(&buffers->editItemAt(i)); |
| 1942 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1943 | break; |
| 1944 | } |
| 1945 | |
| 1946 | case omx_message::FILL_BUFFER_DONE: |
| 1947 | { |
| 1948 | IOMX::buffer_id buffer = msg.u.extended_buffer_data.buffer; |
| 1949 | OMX_U32 flags = msg.u.extended_buffer_data.flags; |
| 1950 | |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 1951 | CODEC_LOGV("FILL_BUFFER_DONE(buffer: %p, size: %ld, flags: 0x%08lx, timestamp: %lld us (%.2f secs))", |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1952 | buffer, |
| 1953 | msg.u.extended_buffer_data.range_length, |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 1954 | flags, |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1955 | msg.u.extended_buffer_data.timestamp, |
| 1956 | msg.u.extended_buffer_data.timestamp / 1E6); |
| 1957 | |
| 1958 | Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; |
| 1959 | size_t i = 0; |
| 1960 | while (i < buffers->size() && (*buffers)[i].mBuffer != buffer) { |
| 1961 | ++i; |
| 1962 | } |
| 1963 | |
| 1964 | CHECK(i < buffers->size()); |
| 1965 | BufferInfo *info = &buffers->editItemAt(i); |
| 1966 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 1967 | if (info->mStatus != OWNED_BY_COMPONENT) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1968 | LOGW("We already own output buffer %p, yet received " |
| 1969 | "a FILL_BUFFER_DONE.", buffer); |
| 1970 | } |
| 1971 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 1972 | info->mStatus = OWNED_BY_US; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1973 | |
| 1974 | if (mPortStatus[kPortIndexOutput] == DISABLING) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1975 | CODEC_LOGV("Port is disabled, freeing buffer %p", buffer); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1976 | |
Jamie Gennis | f0c5c1e | 2010-11-01 16:04:31 -0700 | [diff] [blame] | 1977 | status_t err = freeBuffer(kPortIndexOutput, i); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1978 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1979 | |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 1980 | #if 0 |
Andreas Huber | d779589 | 2009-08-26 10:33:47 -0700 | [diff] [blame] | 1981 | } else if (mPortStatus[kPortIndexOutput] == ENABLED |
| 1982 | && (flags & OMX_BUFFERFLAG_EOS)) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 1983 | CODEC_LOGV("No more output data."); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1984 | mNoMoreOutputData = true; |
| 1985 | mBufferFilled.signal(); |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 1986 | #endif |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 1987 | } else if (mPortStatus[kPortIndexOutput] != SHUTTING_DOWN) { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 1988 | CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED); |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 1989 | |
Andreas Huber | 52733b8 | 2010-01-25 10:41:35 -0800 | [diff] [blame] | 1990 | if (info->mMediaBuffer == NULL) { |
| 1991 | CHECK(mOMXLivesLocally); |
| 1992 | CHECK(mQuirks & kRequiresAllocateBufferOnOutputPorts); |
| 1993 | CHECK(mQuirks & kDefersOutputBufferAllocation); |
| 1994 | |
| 1995 | // The qcom video decoders on Nexus don't actually allocate |
| 1996 | // output buffer memory on a call to OMX_AllocateBuffer |
| 1997 | // the "pBuffer" member of the OMX_BUFFERHEADERTYPE |
| 1998 | // structure is only filled in later. |
| 1999 | |
| 2000 | info->mMediaBuffer = new MediaBuffer( |
| 2001 | msg.u.extended_buffer_data.data_ptr, |
| 2002 | info->mSize); |
| 2003 | info->mMediaBuffer->setObserver(this); |
| 2004 | } |
| 2005 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2006 | MediaBuffer *buffer = info->mMediaBuffer; |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 2007 | bool isGraphicBuffer = buffer->graphicBuffer() != NULL; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2008 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 2009 | if (!isGraphicBuffer |
| 2010 | && msg.u.extended_buffer_data.range_offset |
Andreas Huber | f88f844 | 2010-08-10 11:18:36 -0700 | [diff] [blame] | 2011 | + msg.u.extended_buffer_data.range_length |
| 2012 | > buffer->size()) { |
| 2013 | CODEC_LOGE( |
| 2014 | "Codec lied about its buffer size requirements, " |
| 2015 | "sending a buffer larger than the originally " |
| 2016 | "advertised size in FILL_BUFFER_DONE!"); |
| 2017 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2018 | buffer->set_range( |
| 2019 | msg.u.extended_buffer_data.range_offset, |
| 2020 | msg.u.extended_buffer_data.range_length); |
| 2021 | |
| 2022 | buffer->meta_data()->clear(); |
| 2023 | |
Andreas Huber | fa8de75 | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 2024 | buffer->meta_data()->setInt64( |
| 2025 | kKeyTime, msg.u.extended_buffer_data.timestamp); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2026 | |
| 2027 | if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_SYNCFRAME) { |
| 2028 | buffer->meta_data()->setInt32(kKeyIsSyncFrame, true); |
| 2029 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 2030 | if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_CODECCONFIG) { |
| 2031 | buffer->meta_data()->setInt32(kKeyIsCodecConfig, true); |
| 2032 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2033 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 2034 | if (isGraphicBuffer || mQuirks & kOutputBuffersAreUnreadable) { |
Andreas Huber | 1e19416 | 2010-10-06 16:43:57 -0700 | [diff] [blame] | 2035 | buffer->meta_data()->setInt32(kKeyIsUnreadable, true); |
| 2036 | } |
| 2037 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2038 | buffer->meta_data()->setPointer( |
| 2039 | kKeyPlatformPrivate, |
| 2040 | msg.u.extended_buffer_data.platform_private); |
| 2041 | |
| 2042 | buffer->meta_data()->setPointer( |
| 2043 | kKeyBufferID, |
| 2044 | msg.u.extended_buffer_data.buffer); |
| 2045 | |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 2046 | if (msg.u.extended_buffer_data.flags & OMX_BUFFERFLAG_EOS) { |
| 2047 | CODEC_LOGV("No more output data."); |
| 2048 | mNoMoreOutputData = true; |
| 2049 | } |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2050 | |
| 2051 | if (mTargetTimeUs >= 0) { |
| 2052 | CHECK(msg.u.extended_buffer_data.timestamp <= mTargetTimeUs); |
| 2053 | |
| 2054 | if (msg.u.extended_buffer_data.timestamp < mTargetTimeUs) { |
| 2055 | CODEC_LOGV( |
| 2056 | "skipping output buffer at timestamp %lld us", |
| 2057 | msg.u.extended_buffer_data.timestamp); |
| 2058 | |
| 2059 | fillOutputBuffer(info); |
| 2060 | break; |
| 2061 | } |
| 2062 | |
| 2063 | CODEC_LOGV( |
| 2064 | "returning output buffer at target timestamp " |
| 2065 | "%lld us", |
| 2066 | msg.u.extended_buffer_data.timestamp); |
| 2067 | |
| 2068 | mTargetTimeUs = -1; |
| 2069 | } |
| 2070 | |
| 2071 | mFilledBuffers.push_back(i); |
| 2072 | mBufferFilled.signal(); |
James Dong | fc8b7c9 | 2010-12-07 14:37:27 -0800 | [diff] [blame] | 2073 | if (mIsEncoder) { |
| 2074 | sched_yield(); |
| 2075 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | break; |
| 2079 | } |
| 2080 | |
| 2081 | default: |
| 2082 | { |
| 2083 | CHECK(!"should not be here."); |
| 2084 | break; |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | |
Andreas Huber | b167860 | 2009-10-19 13:06:40 -0700 | [diff] [blame] | 2089 | // Has the format changed in any way that the client would have to be aware of? |
| 2090 | static bool formatHasNotablyChanged( |
| 2091 | const sp<MetaData> &from, const sp<MetaData> &to) { |
| 2092 | if (from.get() == NULL && to.get() == NULL) { |
| 2093 | return false; |
| 2094 | } |
| 2095 | |
Andreas Huber | f68c168 | 2009-10-21 14:01:30 -0700 | [diff] [blame] | 2096 | if ((from.get() == NULL && to.get() != NULL) |
| 2097 | || (from.get() != NULL && to.get() == NULL)) { |
Andreas Huber | b167860 | 2009-10-19 13:06:40 -0700 | [diff] [blame] | 2098 | return true; |
| 2099 | } |
| 2100 | |
| 2101 | const char *mime_from, *mime_to; |
| 2102 | CHECK(from->findCString(kKeyMIMEType, &mime_from)); |
| 2103 | CHECK(to->findCString(kKeyMIMEType, &mime_to)); |
| 2104 | |
| 2105 | if (strcasecmp(mime_from, mime_to)) { |
| 2106 | return true; |
| 2107 | } |
| 2108 | |
| 2109 | if (!strcasecmp(mime_from, MEDIA_MIMETYPE_VIDEO_RAW)) { |
| 2110 | int32_t colorFormat_from, colorFormat_to; |
| 2111 | CHECK(from->findInt32(kKeyColorFormat, &colorFormat_from)); |
| 2112 | CHECK(to->findInt32(kKeyColorFormat, &colorFormat_to)); |
| 2113 | |
| 2114 | if (colorFormat_from != colorFormat_to) { |
| 2115 | return true; |
| 2116 | } |
| 2117 | |
| 2118 | int32_t width_from, width_to; |
| 2119 | CHECK(from->findInt32(kKeyWidth, &width_from)); |
| 2120 | CHECK(to->findInt32(kKeyWidth, &width_to)); |
| 2121 | |
| 2122 | if (width_from != width_to) { |
| 2123 | return true; |
| 2124 | } |
| 2125 | |
| 2126 | int32_t height_from, height_to; |
| 2127 | CHECK(from->findInt32(kKeyHeight, &height_from)); |
| 2128 | CHECK(to->findInt32(kKeyHeight, &height_to)); |
| 2129 | |
| 2130 | if (height_from != height_to) { |
| 2131 | return true; |
| 2132 | } |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2133 | |
| 2134 | int32_t left_from, top_from, right_from, bottom_from; |
| 2135 | CHECK(from->findRect( |
| 2136 | kKeyCropRect, |
| 2137 | &left_from, &top_from, &right_from, &bottom_from)); |
| 2138 | |
| 2139 | int32_t left_to, top_to, right_to, bottom_to; |
| 2140 | CHECK(to->findRect( |
| 2141 | kKeyCropRect, |
| 2142 | &left_to, &top_to, &right_to, &bottom_to)); |
| 2143 | |
| 2144 | if (left_to != left_from || top_to != top_from |
| 2145 | || right_to != right_from || bottom_to != bottom_from) { |
| 2146 | return true; |
| 2147 | } |
Andreas Huber | b167860 | 2009-10-19 13:06:40 -0700 | [diff] [blame] | 2148 | } else if (!strcasecmp(mime_from, MEDIA_MIMETYPE_AUDIO_RAW)) { |
| 2149 | int32_t numChannels_from, numChannels_to; |
| 2150 | CHECK(from->findInt32(kKeyChannelCount, &numChannels_from)); |
| 2151 | CHECK(to->findInt32(kKeyChannelCount, &numChannels_to)); |
| 2152 | |
| 2153 | if (numChannels_from != numChannels_to) { |
| 2154 | return true; |
| 2155 | } |
| 2156 | |
| 2157 | int32_t sampleRate_from, sampleRate_to; |
| 2158 | CHECK(from->findInt32(kKeySampleRate, &sampleRate_from)); |
| 2159 | CHECK(to->findInt32(kKeySampleRate, &sampleRate_to)); |
| 2160 | |
| 2161 | if (sampleRate_from != sampleRate_to) { |
| 2162 | return true; |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | return false; |
| 2167 | } |
| 2168 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2169 | void OMXCodec::onEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) { |
| 2170 | switch (event) { |
| 2171 | case OMX_EventCmdComplete: |
| 2172 | { |
| 2173 | onCmdComplete((OMX_COMMANDTYPE)data1, data2); |
| 2174 | break; |
| 2175 | } |
| 2176 | |
| 2177 | case OMX_EventError: |
| 2178 | { |
| 2179 | CODEC_LOGE("ERROR(0x%08lx, %ld)", data1, data2); |
| 2180 | |
| 2181 | setState(ERROR); |
| 2182 | break; |
| 2183 | } |
| 2184 | |
| 2185 | case OMX_EventPortSettingsChanged: |
| 2186 | { |
| 2187 | CODEC_LOGV("OMX_EventPortSettingsChanged(port=%ld, data2=0x%08lx)", |
| 2188 | data1, data2); |
| 2189 | |
| 2190 | if (data2 == 0 || data2 == OMX_IndexParamPortDefinition) { |
| 2191 | onPortSettingsChanged(data1); |
| 2192 | } else if (data1 == kPortIndexOutput |
| 2193 | && data2 == OMX_IndexConfigCommonOutputCrop) { |
| 2194 | |
| 2195 | sp<MetaData> oldOutputFormat = mOutputFormat; |
| 2196 | initOutputFormat(mSource->getFormat()); |
| 2197 | |
| 2198 | if (formatHasNotablyChanged(oldOutputFormat, mOutputFormat)) { |
| 2199 | mOutputPortSettingsHaveChanged = true; |
| 2200 | |
| 2201 | if (mNativeWindow != NULL) { |
| 2202 | int32_t left, top, right, bottom; |
| 2203 | CHECK(mOutputFormat->findRect( |
| 2204 | kKeyCropRect, |
| 2205 | &left, &top, &right, &bottom)); |
| 2206 | |
| 2207 | android_native_rect_t crop; |
| 2208 | crop.left = left; |
| 2209 | crop.top = top; |
| 2210 | crop.right = right; |
| 2211 | crop.bottom = bottom; |
| 2212 | |
Andreas Huber | 00d6c72 | 2011-01-26 12:02:35 -0800 | [diff] [blame] | 2213 | // We'll ignore any errors here, if the surface is |
| 2214 | // already invalid, we'll know soon enough. |
| 2215 | native_window_set_crop(mNativeWindow.get(), &crop); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | break; |
| 2220 | } |
| 2221 | |
| 2222 | #if 0 |
| 2223 | case OMX_EventBufferFlag: |
| 2224 | { |
| 2225 | CODEC_LOGV("EVENT_BUFFER_FLAG(%ld)", data1); |
| 2226 | |
| 2227 | if (data1 == kPortIndexOutput) { |
| 2228 | mNoMoreOutputData = true; |
| 2229 | } |
| 2230 | break; |
| 2231 | } |
| 2232 | #endif |
| 2233 | |
| 2234 | default: |
| 2235 | { |
| 2236 | CODEC_LOGV("EVENT(%d, %ld, %ld)", event, data1, data2); |
| 2237 | break; |
| 2238 | } |
| 2239 | } |
| 2240 | } |
| 2241 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2242 | void OMXCodec::onCmdComplete(OMX_COMMANDTYPE cmd, OMX_U32 data) { |
| 2243 | switch (cmd) { |
| 2244 | case OMX_CommandStateSet: |
| 2245 | { |
| 2246 | onStateChange((OMX_STATETYPE)data); |
| 2247 | break; |
| 2248 | } |
| 2249 | |
| 2250 | case OMX_CommandPortDisable: |
| 2251 | { |
| 2252 | OMX_U32 portIndex = data; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2253 | CODEC_LOGV("PORT_DISABLED(%ld)", portIndex); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2254 | |
| 2255 | CHECK(mState == EXECUTING || mState == RECONFIGURING); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2256 | CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLING); |
| 2257 | CHECK_EQ(mPortBuffers[portIndex].size(), 0u); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2258 | |
| 2259 | mPortStatus[portIndex] = DISABLED; |
| 2260 | |
| 2261 | if (mState == RECONFIGURING) { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2262 | CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2263 | |
Andreas Huber | b167860 | 2009-10-19 13:06:40 -0700 | [diff] [blame] | 2264 | sp<MetaData> oldOutputFormat = mOutputFormat; |
Andreas Huber | cfd5557 | 2009-10-09 14:11:28 -0700 | [diff] [blame] | 2265 | initOutputFormat(mSource->getFormat()); |
Andreas Huber | b167860 | 2009-10-19 13:06:40 -0700 | [diff] [blame] | 2266 | |
| 2267 | // Don't notify clients if the output port settings change |
| 2268 | // wasn't of importance to them, i.e. it may be that just the |
| 2269 | // number of buffers has changed and nothing else. |
| 2270 | mOutputPortSettingsHaveChanged = |
| 2271 | formatHasNotablyChanged(oldOutputFormat, mOutputFormat); |
Andreas Huber | cfd5557 | 2009-10-09 14:11:28 -0700 | [diff] [blame] | 2272 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2273 | enablePortAsync(portIndex); |
| 2274 | |
| 2275 | status_t err = allocateBuffersOnPort(portIndex); |
Andreas Huber | ba1b1679 | 2011-01-19 09:20:58 -0800 | [diff] [blame] | 2276 | |
| 2277 | if (err != OK) { |
| 2278 | CODEC_LOGE("allocateBuffersOnPort failed (err = %d)", err); |
| 2279 | setState(ERROR); |
| 2280 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2281 | } |
| 2282 | break; |
| 2283 | } |
| 2284 | |
| 2285 | case OMX_CommandPortEnable: |
| 2286 | { |
| 2287 | OMX_U32 portIndex = data; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2288 | CODEC_LOGV("PORT_ENABLED(%ld)", portIndex); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2289 | |
| 2290 | CHECK(mState == EXECUTING || mState == RECONFIGURING); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2291 | CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLING); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2292 | |
| 2293 | mPortStatus[portIndex] = ENABLED; |
| 2294 | |
| 2295 | if (mState == RECONFIGURING) { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2296 | CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2297 | |
| 2298 | setState(EXECUTING); |
| 2299 | |
| 2300 | fillOutputBuffers(); |
| 2301 | } |
| 2302 | break; |
| 2303 | } |
| 2304 | |
| 2305 | case OMX_CommandFlush: |
| 2306 | { |
| 2307 | OMX_U32 portIndex = data; |
| 2308 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2309 | CODEC_LOGV("FLUSH_DONE(%ld)", portIndex); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2310 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2311 | CHECK_EQ((int)mPortStatus[portIndex], (int)SHUTTING_DOWN); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2312 | mPortStatus[portIndex] = ENABLED; |
| 2313 | |
| 2314 | CHECK_EQ(countBuffersWeOwn(mPortBuffers[portIndex]), |
| 2315 | mPortBuffers[portIndex].size()); |
| 2316 | |
| 2317 | if (mState == RECONFIGURING) { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2318 | CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2319 | |
| 2320 | disablePortAsync(portIndex); |
Andreas Huber | 127fcdc | 2009-08-26 16:27:02 -0700 | [diff] [blame] | 2321 | } else if (mState == EXECUTING_TO_IDLE) { |
| 2322 | if (mPortStatus[kPortIndexInput] == ENABLED |
| 2323 | && mPortStatus[kPortIndexOutput] == ENABLED) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2324 | CODEC_LOGV("Finished flushing both ports, now completing " |
Andreas Huber | 127fcdc | 2009-08-26 16:27:02 -0700 | [diff] [blame] | 2325 | "transition from EXECUTING to IDLE."); |
| 2326 | |
| 2327 | mPortStatus[kPortIndexInput] = SHUTTING_DOWN; |
| 2328 | mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; |
| 2329 | |
| 2330 | status_t err = |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2331 | mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2332 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | 127fcdc | 2009-08-26 16:27:02 -0700 | [diff] [blame] | 2333 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2334 | } else { |
| 2335 | // We're flushing both ports in preparation for seeking. |
| 2336 | |
| 2337 | if (mPortStatus[kPortIndexInput] == ENABLED |
| 2338 | && mPortStatus[kPortIndexOutput] == ENABLED) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2339 | CODEC_LOGV("Finished flushing both ports, now continuing from" |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2340 | " seek-time."); |
| 2341 | |
Andreas Huber | 1f24b30 | 2010-06-10 11:12:39 -0700 | [diff] [blame] | 2342 | // We implicitly resume pulling on our upstream source. |
| 2343 | mPaused = false; |
| 2344 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2345 | drainInputBuffers(); |
| 2346 | fillOutputBuffers(); |
| 2347 | } |
Andreas Huber | b928983 | 2011-02-08 13:10:25 -0800 | [diff] [blame] | 2348 | |
| 2349 | if (mOutputPortSettingsChangedPending) { |
| 2350 | CODEC_LOGV( |
| 2351 | "Honoring deferred output port settings change."); |
| 2352 | |
| 2353 | mOutputPortSettingsChangedPending = false; |
| 2354 | onPortSettingsChanged(kPortIndexOutput); |
| 2355 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | break; |
| 2359 | } |
| 2360 | |
| 2361 | default: |
| 2362 | { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2363 | CODEC_LOGV("CMD_COMPLETE(%d, %ld)", cmd, data); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2364 | break; |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | void OMXCodec::onStateChange(OMX_STATETYPE newState) { |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 2370 | CODEC_LOGV("onStateChange %d", newState); |
| 2371 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2372 | switch (newState) { |
| 2373 | case OMX_StateIdle: |
| 2374 | { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2375 | CODEC_LOGV("Now Idle."); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2376 | if (mState == LOADED_TO_IDLE) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2377 | status_t err = mOMX->sendCommand( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2378 | mNode, OMX_CommandStateSet, OMX_StateExecuting); |
| 2379 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2380 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2381 | |
| 2382 | setState(IDLE_TO_EXECUTING); |
| 2383 | } else { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2384 | CHECK_EQ((int)mState, (int)EXECUTING_TO_IDLE); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2385 | |
| 2386 | CHECK_EQ( |
| 2387 | countBuffersWeOwn(mPortBuffers[kPortIndexInput]), |
| 2388 | mPortBuffers[kPortIndexInput].size()); |
| 2389 | |
| 2390 | CHECK_EQ( |
| 2391 | countBuffersWeOwn(mPortBuffers[kPortIndexOutput]), |
| 2392 | mPortBuffers[kPortIndexOutput].size()); |
| 2393 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2394 | status_t err = mOMX->sendCommand( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2395 | mNode, OMX_CommandStateSet, OMX_StateLoaded); |
| 2396 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2397 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2398 | |
| 2399 | err = freeBuffersOnPort(kPortIndexInput); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2400 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2401 | |
| 2402 | err = freeBuffersOnPort(kPortIndexOutput); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2403 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2404 | |
| 2405 | mPortStatus[kPortIndexInput] = ENABLED; |
| 2406 | mPortStatus[kPortIndexOutput] = ENABLED; |
| 2407 | |
| 2408 | setState(IDLE_TO_LOADED); |
| 2409 | } |
| 2410 | break; |
| 2411 | } |
| 2412 | |
| 2413 | case OMX_StateExecuting: |
| 2414 | { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2415 | CHECK_EQ((int)mState, (int)IDLE_TO_EXECUTING); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2416 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2417 | CODEC_LOGV("Now Executing."); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2418 | |
Andreas Huber | b928983 | 2011-02-08 13:10:25 -0800 | [diff] [blame] | 2419 | mOutputPortSettingsChangedPending = false; |
| 2420 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2421 | setState(EXECUTING); |
| 2422 | |
Andreas Huber | 42978e5 | 2009-08-27 10:08:39 -0700 | [diff] [blame] | 2423 | // Buffers will be submitted to the component in the first |
| 2424 | // call to OMXCodec::read as mInitialBufferSubmit is true at |
| 2425 | // this point. This ensures that this on_message call returns, |
| 2426 | // releases the lock and ::init can notice the state change and |
| 2427 | // itself return. |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2428 | break; |
| 2429 | } |
| 2430 | |
| 2431 | case OMX_StateLoaded: |
| 2432 | { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2433 | CHECK_EQ((int)mState, (int)IDLE_TO_LOADED); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2434 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2435 | CODEC_LOGV("Now Loaded."); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2436 | |
| 2437 | setState(LOADED); |
| 2438 | break; |
| 2439 | } |
| 2440 | |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 2441 | case OMX_StateInvalid: |
| 2442 | { |
| 2443 | setState(ERROR); |
| 2444 | break; |
| 2445 | } |
| 2446 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2447 | default: |
| 2448 | { |
| 2449 | CHECK(!"should not be here."); |
| 2450 | break; |
| 2451 | } |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | // static |
| 2456 | size_t OMXCodec::countBuffersWeOwn(const Vector<BufferInfo> &buffers) { |
| 2457 | size_t n = 0; |
| 2458 | for (size_t i = 0; i < buffers.size(); ++i) { |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2459 | if (buffers[i].mStatus != OWNED_BY_COMPONENT) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2460 | ++n; |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | return n; |
| 2465 | } |
| 2466 | |
| 2467 | status_t OMXCodec::freeBuffersOnPort( |
| 2468 | OMX_U32 portIndex, bool onlyThoseWeOwn) { |
| 2469 | Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; |
| 2470 | |
| 2471 | status_t stickyErr = OK; |
| 2472 | |
| 2473 | for (size_t i = buffers->size(); i-- > 0;) { |
| 2474 | BufferInfo *info = &buffers->editItemAt(i); |
| 2475 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2476 | if (onlyThoseWeOwn && info->mStatus == OWNED_BY_COMPONENT) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2477 | continue; |
| 2478 | } |
| 2479 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2480 | CHECK(info->mStatus == OWNED_BY_US |
| 2481 | || info->mStatus == OWNED_BY_NATIVE_WINDOW); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2482 | |
Andreas Huber | 9202285 | 2009-09-14 15:24:14 -0700 | [diff] [blame] | 2483 | CODEC_LOGV("freeing buffer %p on port %ld", info->mBuffer, portIndex); |
| 2484 | |
Jamie Gennis | f0c5c1e | 2010-11-01 16:04:31 -0700 | [diff] [blame] | 2485 | status_t err = freeBuffer(portIndex, i); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2486 | |
| 2487 | if (err != OK) { |
| 2488 | stickyErr = err; |
| 2489 | } |
| 2490 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | CHECK(onlyThoseWeOwn || buffers->isEmpty()); |
| 2494 | |
| 2495 | return stickyErr; |
| 2496 | } |
| 2497 | |
Jamie Gennis | f0c5c1e | 2010-11-01 16:04:31 -0700 | [diff] [blame] | 2498 | status_t OMXCodec::freeBuffer(OMX_U32 portIndex, size_t bufIndex) { |
| 2499 | Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; |
| 2500 | |
| 2501 | BufferInfo *info = &buffers->editItemAt(bufIndex); |
| 2502 | |
| 2503 | status_t err = mOMX->freeBuffer(mNode, portIndex, info->mBuffer); |
| 2504 | |
| 2505 | if (err == OK && info->mMediaBuffer != NULL) { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2506 | CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); |
Jamie Gennis | f0c5c1e | 2010-11-01 16:04:31 -0700 | [diff] [blame] | 2507 | info->mMediaBuffer->setObserver(NULL); |
| 2508 | |
| 2509 | // Make sure nobody but us owns this buffer at this point. |
| 2510 | CHECK_EQ(info->mMediaBuffer->refcount(), 0); |
| 2511 | |
| 2512 | // Cancel the buffer if it belongs to an ANativeWindow. |
| 2513 | sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer(); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2514 | if (info->mStatus == OWNED_BY_US && graphicBuffer != 0) { |
Jamie Gennis | f0c5c1e | 2010-11-01 16:04:31 -0700 | [diff] [blame] | 2515 | err = cancelBufferToNativeWindow(info); |
| 2516 | } |
| 2517 | |
| 2518 | info->mMediaBuffer->release(); |
James Dong | 31b9375f | 2010-11-10 21:11:41 -0800 | [diff] [blame] | 2519 | info->mMediaBuffer = NULL; |
Jamie Gennis | f0c5c1e | 2010-11-01 16:04:31 -0700 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | if (err == OK) { |
| 2523 | buffers->removeAt(bufIndex); |
| 2524 | } |
| 2525 | |
| 2526 | return err; |
| 2527 | } |
| 2528 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2529 | void OMXCodec::onPortSettingsChanged(OMX_U32 portIndex) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2530 | CODEC_LOGV("PORT_SETTINGS_CHANGED(%ld)", portIndex); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2531 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2532 | CHECK_EQ((int)mState, (int)EXECUTING); |
| 2533 | CHECK_EQ(portIndex, (OMX_U32)kPortIndexOutput); |
Andreas Huber | b928983 | 2011-02-08 13:10:25 -0800 | [diff] [blame] | 2534 | CHECK(!mOutputPortSettingsChangedPending); |
| 2535 | |
| 2536 | if (mPortStatus[kPortIndexOutput] != ENABLED) { |
| 2537 | CODEC_LOGV("Deferring output port settings change."); |
| 2538 | mOutputPortSettingsChangedPending = true; |
| 2539 | return; |
| 2540 | } |
| 2541 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2542 | setState(RECONFIGURING); |
| 2543 | |
| 2544 | if (mQuirks & kNeedsFlushBeforeDisable) { |
Andreas Huber | 404cc41 | 2009-08-25 14:26:05 -0700 | [diff] [blame] | 2545 | if (!flushPortAsync(portIndex)) { |
| 2546 | onCmdComplete(OMX_CommandFlush, portIndex); |
| 2547 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2548 | } else { |
| 2549 | disablePortAsync(portIndex); |
| 2550 | } |
| 2551 | } |
| 2552 | |
Andreas Huber | 404cc41 | 2009-08-25 14:26:05 -0700 | [diff] [blame] | 2553 | bool OMXCodec::flushPortAsync(OMX_U32 portIndex) { |
Andreas Huber | 127fcdc | 2009-08-26 16:27:02 -0700 | [diff] [blame] | 2554 | CHECK(mState == EXECUTING || mState == RECONFIGURING |
| 2555 | || mState == EXECUTING_TO_IDLE); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2556 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2557 | CODEC_LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.", |
Andreas Huber | 404cc41 | 2009-08-25 14:26:05 -0700 | [diff] [blame] | 2558 | portIndex, countBuffersWeOwn(mPortBuffers[portIndex]), |
| 2559 | mPortBuffers[portIndex].size()); |
| 2560 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2561 | CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2562 | mPortStatus[portIndex] = SHUTTING_DOWN; |
| 2563 | |
Andreas Huber | 404cc41 | 2009-08-25 14:26:05 -0700 | [diff] [blame] | 2564 | if ((mQuirks & kRequiresFlushCompleteEmulation) |
| 2565 | && countBuffersWeOwn(mPortBuffers[portIndex]) |
| 2566 | == mPortBuffers[portIndex].size()) { |
| 2567 | // No flush is necessary and this component fails to send a |
| 2568 | // flush-complete event in this case. |
| 2569 | |
| 2570 | return false; |
| 2571 | } |
| 2572 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2573 | status_t err = |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2574 | mOMX->sendCommand(mNode, OMX_CommandFlush, portIndex); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2575 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | 404cc41 | 2009-08-25 14:26:05 -0700 | [diff] [blame] | 2576 | |
| 2577 | return true; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | void OMXCodec::disablePortAsync(OMX_U32 portIndex) { |
| 2581 | CHECK(mState == EXECUTING || mState == RECONFIGURING); |
| 2582 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2583 | CHECK_EQ((int)mPortStatus[portIndex], (int)ENABLED); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2584 | mPortStatus[portIndex] = DISABLING; |
| 2585 | |
Andreas Huber | d222c84 | 2010-08-26 14:29:34 -0700 | [diff] [blame] | 2586 | CODEC_LOGV("sending OMX_CommandPortDisable(%ld)", portIndex); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2587 | status_t err = |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2588 | mOMX->sendCommand(mNode, OMX_CommandPortDisable, portIndex); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2589 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2590 | |
| 2591 | freeBuffersOnPort(portIndex, true); |
| 2592 | } |
| 2593 | |
| 2594 | void OMXCodec::enablePortAsync(OMX_U32 portIndex) { |
| 2595 | CHECK(mState == EXECUTING || mState == RECONFIGURING); |
| 2596 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2597 | CHECK_EQ((int)mPortStatus[portIndex], (int)DISABLED); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2598 | mPortStatus[portIndex] = ENABLING; |
| 2599 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 2600 | CODEC_LOGV("sending OMX_CommandPortEnable(%ld)", portIndex); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2601 | status_t err = |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2602 | mOMX->sendCommand(mNode, OMX_CommandPortEnable, portIndex); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2603 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | void OMXCodec::fillOutputBuffers() { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2607 | CHECK_EQ((int)mState, (int)EXECUTING); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2608 | |
Andreas Huber | dbcb2c6 | 2010-01-14 11:32:13 -0800 | [diff] [blame] | 2609 | // This is a workaround for some decoders not properly reporting |
| 2610 | // end-of-output-stream. If we own all input buffers and also own |
| 2611 | // all output buffers and we already signalled end-of-input-stream, |
| 2612 | // the end-of-output-stream is implied. |
| 2613 | if (mSignalledEOS |
| 2614 | && countBuffersWeOwn(mPortBuffers[kPortIndexInput]) |
| 2615 | == mPortBuffers[kPortIndexInput].size() |
| 2616 | && countBuffersWeOwn(mPortBuffers[kPortIndexOutput]) |
| 2617 | == mPortBuffers[kPortIndexOutput].size()) { |
| 2618 | mNoMoreOutputData = true; |
| 2619 | mBufferFilled.signal(); |
| 2620 | |
| 2621 | return; |
| 2622 | } |
| 2623 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2624 | Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; |
| 2625 | for (size_t i = 0; i < buffers->size(); ++i) { |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 2626 | BufferInfo *info = &buffers->editItemAt(i); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2627 | if (info->mStatus == OWNED_BY_US) { |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 2628 | fillOutputBuffer(&buffers->editItemAt(i)); |
| 2629 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | void OMXCodec::drainInputBuffers() { |
Andreas Huber | d06e5b8 | 2009-08-28 13:18:14 -0700 | [diff] [blame] | 2634 | CHECK(mState == EXECUTING || mState == RECONFIGURING); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2635 | |
| 2636 | Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; |
| 2637 | for (size_t i = 0; i < buffers->size(); ++i) { |
James Dong | 5f3ab06 | 2011-01-25 16:31:28 -0800 | [diff] [blame] | 2638 | BufferInfo *info = &buffers->editItemAt(i); |
| 2639 | |
| 2640 | if (info->mStatus != OWNED_BY_US) { |
| 2641 | continue; |
| 2642 | } |
| 2643 | |
| 2644 | if (!drainInputBuffer(info)) { |
| 2645 | break; |
| 2646 | } |
| 2647 | |
| 2648 | if (mOnlySubmitOneBufferAtOneTime) { |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2649 | break; |
| 2650 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2654 | bool OMXCodec::drainInputBuffer(BufferInfo *info) { |
| 2655 | CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2656 | |
| 2657 | if (mSignalledEOS) { |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2658 | return false; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | if (mCodecSpecificDataIndex < mCodecSpecificData.size()) { |
| 2662 | const CodecSpecificData *specific = |
| 2663 | mCodecSpecificData[mCodecSpecificDataIndex]; |
| 2664 | |
| 2665 | size_t size = specific->mSize; |
| 2666 | |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 2667 | if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mMIME) |
Andreas Huber | 4f5e602 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 2668 | && !(mQuirks & kWantsNALFragments)) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2669 | static const uint8_t kNALStartCode[4] = |
| 2670 | { 0x00, 0x00, 0x00, 0x01 }; |
| 2671 | |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 2672 | CHECK(info->mSize >= specific->mSize + 4); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2673 | |
| 2674 | size += 4; |
| 2675 | |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 2676 | memcpy(info->mData, kNALStartCode, 4); |
| 2677 | memcpy((uint8_t *)info->mData + 4, |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2678 | specific->mData, specific->mSize); |
| 2679 | } else { |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 2680 | CHECK(info->mSize >= specific->mSize); |
| 2681 | memcpy(info->mData, specific->mData, specific->mSize); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2682 | } |
| 2683 | |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 2684 | mNoMoreOutputData = false; |
| 2685 | |
Andreas Huber | dbcb2c6 | 2010-01-14 11:32:13 -0800 | [diff] [blame] | 2686 | CODEC_LOGV("calling emptyBuffer with codec specific data"); |
| 2687 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2688 | status_t err = mOMX->emptyBuffer( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2689 | mNode, info->mBuffer, 0, size, |
| 2690 | OMX_BUFFERFLAG_ENDOFFRAME | OMX_BUFFERFLAG_CODECCONFIG, |
| 2691 | 0); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2692 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2693 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2694 | info->mStatus = OWNED_BY_COMPONENT; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2695 | |
| 2696 | ++mCodecSpecificDataIndex; |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2697 | return true; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2698 | } |
| 2699 | |
Andreas Huber | 1f24b30 | 2010-06-10 11:12:39 -0700 | [diff] [blame] | 2700 | if (mPaused) { |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2701 | return false; |
Andreas Huber | 1f24b30 | 2010-06-10 11:12:39 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2704 | status_t err; |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 2705 | |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2706 | bool signalEOS = false; |
| 2707 | int64_t timestampUs = 0; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2708 | |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2709 | size_t offset = 0; |
| 2710 | int32_t n = 0; |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2711 | |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2712 | for (;;) { |
| 2713 | MediaBuffer *srcBuffer; |
| 2714 | if (mSeekTimeUs >= 0) { |
| 2715 | if (mLeftOverBuffer) { |
| 2716 | mLeftOverBuffer->release(); |
| 2717 | mLeftOverBuffer = NULL; |
| 2718 | } |
James Dong | 2144f63 | 2010-12-11 10:43:41 -0800 | [diff] [blame] | 2719 | |
| 2720 | MediaSource::ReadOptions options; |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2721 | options.setSeekTo(mSeekTimeUs, mSeekMode); |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2722 | |
| 2723 | mSeekTimeUs = -1; |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2724 | mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC; |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2725 | mBufferFilled.signal(); |
| 2726 | |
| 2727 | err = mSource->read(&srcBuffer, &options); |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2728 | |
| 2729 | if (err == OK) { |
| 2730 | int64_t targetTimeUs; |
| 2731 | if (srcBuffer->meta_data()->findInt64( |
| 2732 | kKeyTargetTime, &targetTimeUs) |
| 2733 | && targetTimeUs >= 0) { |
Andreas Huber | b928983 | 2011-02-08 13:10:25 -0800 | [diff] [blame] | 2734 | CODEC_LOGV("targetTimeUs = %lld us", targetTimeUs); |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2735 | mTargetTimeUs = targetTimeUs; |
| 2736 | } else { |
| 2737 | mTargetTimeUs = -1; |
| 2738 | } |
| 2739 | } |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2740 | } else if (mLeftOverBuffer) { |
| 2741 | srcBuffer = mLeftOverBuffer; |
| 2742 | mLeftOverBuffer = NULL; |
| 2743 | |
| 2744 | err = OK; |
| 2745 | } else { |
James Dong | 2144f63 | 2010-12-11 10:43:41 -0800 | [diff] [blame] | 2746 | err = mSource->read(&srcBuffer); |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | if (err != OK) { |
| 2750 | signalEOS = true; |
| 2751 | mFinalStatus = err; |
| 2752 | mSignalledEOS = true; |
Andreas Huber | 94bced1 | 2010-12-14 09:50:46 -0800 | [diff] [blame] | 2753 | mBufferFilled.signal(); |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2754 | break; |
| 2755 | } |
| 2756 | |
| 2757 | size_t remainingBytes = info->mSize - offset; |
| 2758 | |
| 2759 | if (srcBuffer->range_length() > remainingBytes) { |
| 2760 | if (offset == 0) { |
| 2761 | CODEC_LOGE( |
| 2762 | "Codec's input buffers are too small to accomodate " |
| 2763 | "buffer read from source (info->mSize = %d, srcLength = %d)", |
| 2764 | info->mSize, srcBuffer->range_length()); |
| 2765 | |
| 2766 | srcBuffer->release(); |
| 2767 | srcBuffer = NULL; |
| 2768 | |
| 2769 | setState(ERROR); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2770 | return false; |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | mLeftOverBuffer = srcBuffer; |
| 2774 | break; |
| 2775 | } |
| 2776 | |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 2777 | bool releaseBuffer = true; |
James Dong | 4f501f0 | 2010-06-07 14:41:41 -0700 | [diff] [blame] | 2778 | if (mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)) { |
| 2779 | CHECK(mOMXLivesLocally && offset == 0); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2780 | |
| 2781 | OMX_BUFFERHEADERTYPE *header = |
| 2782 | (OMX_BUFFERHEADERTYPE *)info->mBuffer; |
| 2783 | |
James Dong | 31b9375f | 2010-11-10 21:11:41 -0800 | [diff] [blame] | 2784 | CHECK(header->pBuffer == info->mData); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2785 | |
| 2786 | header->pBuffer = |
| 2787 | (OMX_U8 *)srcBuffer->data() + srcBuffer->range_offset(); |
| 2788 | |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 2789 | releaseBuffer = false; |
| 2790 | info->mMediaBuffer = srcBuffer; |
James Dong | 4f501f0 | 2010-06-07 14:41:41 -0700 | [diff] [blame] | 2791 | } else { |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 2792 | if (mIsMetaDataStoredInVideoBuffers) { |
| 2793 | releaseBuffer = false; |
| 2794 | info->mMediaBuffer = srcBuffer; |
| 2795 | } |
James Dong | 4f501f0 | 2010-06-07 14:41:41 -0700 | [diff] [blame] | 2796 | memcpy((uint8_t *)info->mData + offset, |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2797 | (const uint8_t *)srcBuffer->data() |
| 2798 | + srcBuffer->range_offset(), |
James Dong | 4f501f0 | 2010-06-07 14:41:41 -0700 | [diff] [blame] | 2799 | srcBuffer->range_length()); |
| 2800 | } |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2801 | |
Andreas Huber | 2dd8ff8 | 2010-04-20 14:26:00 -0700 | [diff] [blame] | 2802 | int64_t lastBufferTimeUs; |
| 2803 | CHECK(srcBuffer->meta_data()->findInt64(kKeyTime, &lastBufferTimeUs)); |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 2804 | CHECK(lastBufferTimeUs >= 0); |
Andreas Huber | 2dd8ff8 | 2010-04-20 14:26:00 -0700 | [diff] [blame] | 2805 | |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2806 | if (offset == 0) { |
Andreas Huber | 2dd8ff8 | 2010-04-20 14:26:00 -0700 | [diff] [blame] | 2807 | timestampUs = lastBufferTimeUs; |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | offset += srcBuffer->range_length(); |
| 2811 | |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 2812 | if (releaseBuffer) { |
| 2813 | srcBuffer->release(); |
| 2814 | srcBuffer = NULL; |
| 2815 | } |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2816 | |
| 2817 | ++n; |
| 2818 | |
| 2819 | if (!(mQuirks & kSupportsMultipleFramesPerInputBuffer)) { |
| 2820 | break; |
| 2821 | } |
Andreas Huber | 2dd8ff8 | 2010-04-20 14:26:00 -0700 | [diff] [blame] | 2822 | |
| 2823 | int64_t coalescedDurationUs = lastBufferTimeUs - timestampUs; |
| 2824 | |
| 2825 | if (coalescedDurationUs > 250000ll) { |
| 2826 | // Don't coalesce more than 250ms worth of encoded data at once. |
| 2827 | break; |
| 2828 | } |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | if (n > 1) { |
| 2832 | LOGV("coalesced %d frames into one input buffer", n); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2836 | |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2837 | if (signalEOS) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2838 | flags |= OMX_BUFFERFLAG_EOS; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2839 | } else { |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 2840 | mNoMoreOutputData = false; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2843 | CODEC_LOGV("Calling emptyBuffer on buffer %p (length %d), " |
| 2844 | "timestamp %lld us (%.2f secs)", |
| 2845 | info->mBuffer, offset, |
| 2846 | timestampUs, timestampUs / 1E6); |
Andreas Huber | 3f42707 | 2009-10-08 11:02:27 -0700 | [diff] [blame] | 2847 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2848 | err = mOMX->emptyBuffer( |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 2849 | mNode, info->mBuffer, 0, offset, |
Andreas Huber | fa8de75 | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 2850 | flags, timestampUs); |
Andreas Huber | 3f42707 | 2009-10-08 11:02:27 -0700 | [diff] [blame] | 2851 | |
| 2852 | if (err != OK) { |
| 2853 | setState(ERROR); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2854 | return false; |
Andreas Huber | 3f42707 | 2009-10-08 11:02:27 -0700 | [diff] [blame] | 2855 | } |
| 2856 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2857 | info->mStatus = OWNED_BY_COMPONENT; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 2858 | |
| 2859 | // This component does not ever signal the EOS flag on output buffers, |
| 2860 | // Thanks for nothing. |
| 2861 | if (mSignalledEOS && !strcmp(mComponentName, "OMX.TI.Video.encoder")) { |
| 2862 | mNoMoreOutputData = true; |
| 2863 | mBufferFilled.signal(); |
| 2864 | } |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2865 | |
| 2866 | return true; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | void OMXCodec::fillOutputBuffer(BufferInfo *info) { |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2870 | CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2871 | |
Andreas Huber | 404cc41 | 2009-08-25 14:26:05 -0700 | [diff] [blame] | 2872 | if (mNoMoreOutputData) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2873 | CODEC_LOGV("There is no more output data available, not " |
Andreas Huber | 404cc41 | 2009-08-25 14:26:05 -0700 | [diff] [blame] | 2874 | "calling fillOutputBuffer"); |
| 2875 | return; |
| 2876 | } |
| 2877 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2878 | if (info->mMediaBuffer != NULL) { |
| 2879 | sp<GraphicBuffer> graphicBuffer = info->mMediaBuffer->graphicBuffer(); |
| 2880 | if (graphicBuffer != 0) { |
| 2881 | // When using a native buffer we need to lock the buffer before |
| 2882 | // giving it to OMX. |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2883 | CODEC_LOGV("Calling lockBuffer on %p", info->mBuffer); |
| 2884 | int err = mNativeWindow->lockBuffer(mNativeWindow.get(), |
| 2885 | graphicBuffer.get()); |
| 2886 | if (err != 0) { |
| 2887 | CODEC_LOGE("lockBuffer failed w/ error 0x%08x", err); |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 2888 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2889 | setState(ERROR); |
| 2890 | return; |
| 2891 | } |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | CODEC_LOGV("Calling fillBuffer on buffer %p", info->mBuffer); |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2896 | status_t err = mOMX->fillBuffer(mNode, info->mBuffer); |
Andreas Huber | 8f14c55 | 2010-04-12 10:20:12 -0700 | [diff] [blame] | 2897 | |
| 2898 | if (err != OK) { |
| 2899 | CODEC_LOGE("fillBuffer failed w/ error 0x%08x", err); |
| 2900 | |
| 2901 | setState(ERROR); |
| 2902 | return; |
| 2903 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2904 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2905 | info->mStatus = OWNED_BY_COMPONENT; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2906 | } |
| 2907 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2908 | bool OMXCodec::drainInputBuffer(IOMX::buffer_id buffer) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2909 | Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexInput]; |
| 2910 | for (size_t i = 0; i < buffers->size(); ++i) { |
| 2911 | if ((*buffers)[i].mBuffer == buffer) { |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2912 | return drainInputBuffer(&buffers->editItemAt(i)); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | CHECK(!"should not be here."); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 2917 | |
| 2918 | return false; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | void OMXCodec::fillOutputBuffer(IOMX::buffer_id buffer) { |
| 2922 | Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; |
| 2923 | for (size_t i = 0; i < buffers->size(); ++i) { |
| 2924 | if ((*buffers)[i].mBuffer == buffer) { |
| 2925 | fillOutputBuffer(&buffers->editItemAt(i)); |
| 2926 | return; |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | CHECK(!"should not be here."); |
| 2931 | } |
| 2932 | |
| 2933 | void OMXCodec::setState(State newState) { |
| 2934 | mState = newState; |
| 2935 | mAsyncCompletion.signal(); |
| 2936 | |
| 2937 | // This may cause some spurious wakeups but is necessary to |
| 2938 | // unblock the reader if we enter ERROR state. |
| 2939 | mBufferFilled.signal(); |
| 2940 | } |
| 2941 | |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 2942 | void OMXCodec::setRawAudioFormat( |
| 2943 | OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 2944 | |
| 2945 | // port definition |
| 2946 | OMX_PARAM_PORTDEFINITIONTYPE def; |
| 2947 | InitOMXParams(&def); |
| 2948 | def.nPortIndex = portIndex; |
| 2949 | status_t err = mOMX->getParameter( |
| 2950 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2951 | CHECK_EQ(err, (status_t)OK); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 2952 | def.format.audio.eEncoding = OMX_AUDIO_CodingPCM; |
| 2953 | CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition, |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2954 | &def, sizeof(def)), (status_t)OK); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 2955 | |
| 2956 | // pcm param |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 2957 | OMX_AUDIO_PARAM_PCMMODETYPE pcmParams; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 2958 | InitOMXParams(&pcmParams); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 2959 | pcmParams.nPortIndex = portIndex; |
| 2960 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 2961 | err = mOMX->getParameter( |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 2962 | mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); |
| 2963 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2964 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 2965 | |
| 2966 | pcmParams.nChannels = numChannels; |
| 2967 | pcmParams.eNumData = OMX_NumericalDataSigned; |
| 2968 | pcmParams.bInterleaved = OMX_TRUE; |
| 2969 | pcmParams.nBitPerSample = 16; |
| 2970 | pcmParams.nSamplingRate = sampleRate; |
| 2971 | pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear; |
| 2972 | |
| 2973 | if (numChannels == 1) { |
| 2974 | pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelCF; |
| 2975 | } else { |
| 2976 | CHECK_EQ(numChannels, 2); |
| 2977 | |
| 2978 | pcmParams.eChannelMapping[0] = OMX_AUDIO_ChannelLF; |
| 2979 | pcmParams.eChannelMapping[1] = OMX_AUDIO_ChannelRF; |
| 2980 | } |
| 2981 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 2982 | err = mOMX->setParameter( |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 2983 | mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams)); |
| 2984 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 2985 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 2986 | } |
| 2987 | |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 2988 | static OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(bool isAMRWB, int32_t bps) { |
| 2989 | if (isAMRWB) { |
| 2990 | if (bps <= 6600) { |
| 2991 | return OMX_AUDIO_AMRBandModeWB0; |
| 2992 | } else if (bps <= 8850) { |
| 2993 | return OMX_AUDIO_AMRBandModeWB1; |
| 2994 | } else if (bps <= 12650) { |
| 2995 | return OMX_AUDIO_AMRBandModeWB2; |
| 2996 | } else if (bps <= 14250) { |
| 2997 | return OMX_AUDIO_AMRBandModeWB3; |
| 2998 | } else if (bps <= 15850) { |
| 2999 | return OMX_AUDIO_AMRBandModeWB4; |
| 3000 | } else if (bps <= 18250) { |
| 3001 | return OMX_AUDIO_AMRBandModeWB5; |
| 3002 | } else if (bps <= 19850) { |
| 3003 | return OMX_AUDIO_AMRBandModeWB6; |
| 3004 | } else if (bps <= 23050) { |
| 3005 | return OMX_AUDIO_AMRBandModeWB7; |
| 3006 | } |
| 3007 | |
| 3008 | // 23850 bps |
| 3009 | return OMX_AUDIO_AMRBandModeWB8; |
| 3010 | } else { // AMRNB |
| 3011 | if (bps <= 4750) { |
| 3012 | return OMX_AUDIO_AMRBandModeNB0; |
| 3013 | } else if (bps <= 5150) { |
| 3014 | return OMX_AUDIO_AMRBandModeNB1; |
| 3015 | } else if (bps <= 5900) { |
| 3016 | return OMX_AUDIO_AMRBandModeNB2; |
| 3017 | } else if (bps <= 6700) { |
| 3018 | return OMX_AUDIO_AMRBandModeNB3; |
| 3019 | } else if (bps <= 7400) { |
| 3020 | return OMX_AUDIO_AMRBandModeNB4; |
| 3021 | } else if (bps <= 7950) { |
| 3022 | return OMX_AUDIO_AMRBandModeNB5; |
| 3023 | } else if (bps <= 10200) { |
| 3024 | return OMX_AUDIO_AMRBandModeNB6; |
| 3025 | } |
| 3026 | |
| 3027 | // 12200 bps |
| 3028 | return OMX_AUDIO_AMRBandModeNB7; |
| 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | void OMXCodec::setAMRFormat(bool isWAMR, int32_t bitRate) { |
Andreas Huber | 8768f2c | 2009-12-01 15:26:54 -0800 | [diff] [blame] | 3033 | OMX_U32 portIndex = mIsEncoder ? kPortIndexOutput : kPortIndexInput; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3034 | |
Andreas Huber | 8768f2c | 2009-12-01 15:26:54 -0800 | [diff] [blame] | 3035 | OMX_AUDIO_PARAM_AMRTYPE def; |
| 3036 | InitOMXParams(&def); |
| 3037 | def.nPortIndex = portIndex; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3038 | |
Andreas Huber | 8768f2c | 2009-12-01 15:26:54 -0800 | [diff] [blame] | 3039 | status_t err = |
| 3040 | mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3041 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3042 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3043 | |
Andreas Huber | 8768f2c | 2009-12-01 15:26:54 -0800 | [diff] [blame] | 3044 | def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3045 | |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 3046 | def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitRate); |
Andreas Huber | 8768f2c | 2009-12-01 15:26:54 -0800 | [diff] [blame] | 3047 | err = mOMX->setParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3048 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | ee606e6 | 2009-09-08 10:19:21 -0700 | [diff] [blame] | 3049 | |
| 3050 | //////////////////////// |
| 3051 | |
| 3052 | if (mIsEncoder) { |
| 3053 | sp<MetaData> format = mSource->getFormat(); |
| 3054 | int32_t sampleRate; |
| 3055 | int32_t numChannels; |
| 3056 | CHECK(format->findInt32(kKeySampleRate, &sampleRate)); |
| 3057 | CHECK(format->findInt32(kKeyChannelCount, &numChannels)); |
| 3058 | |
| 3059 | setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); |
| 3060 | } |
| 3061 | } |
| 3062 | |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 3063 | void OMXCodec::setAACFormat(int32_t numChannels, int32_t sampleRate, int32_t bitRate) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3064 | CHECK(numChannels == 1 || numChannels == 2); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3065 | if (mIsEncoder) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3066 | //////////////// input port //////////////////// |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3067 | setRawAudioFormat(kPortIndexInput, sampleRate, numChannels); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3068 | |
| 3069 | //////////////// output port //////////////////// |
| 3070 | // format |
| 3071 | OMX_AUDIO_PARAM_PORTFORMATTYPE format; |
| 3072 | format.nPortIndex = kPortIndexOutput; |
| 3073 | format.nIndex = 0; |
| 3074 | status_t err = OMX_ErrorNone; |
| 3075 | while (OMX_ErrorNone == err) { |
| 3076 | CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioPortFormat, |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3077 | &format, sizeof(format)), (status_t)OK); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3078 | if (format.eEncoding == OMX_AUDIO_CodingAAC) { |
| 3079 | break; |
| 3080 | } |
| 3081 | format.nIndex++; |
| 3082 | } |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3083 | CHECK_EQ((status_t)OK, err); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3084 | CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioPortFormat, |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3085 | &format, sizeof(format)), (status_t)OK); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3086 | |
| 3087 | // port definition |
| 3088 | OMX_PARAM_PORTDEFINITIONTYPE def; |
| 3089 | InitOMXParams(&def); |
| 3090 | def.nPortIndex = kPortIndexOutput; |
| 3091 | CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamPortDefinition, |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3092 | &def, sizeof(def)), (status_t)OK); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3093 | def.format.audio.bFlagErrorConcealment = OMX_TRUE; |
| 3094 | def.format.audio.eEncoding = OMX_AUDIO_CodingAAC; |
| 3095 | CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamPortDefinition, |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3096 | &def, sizeof(def)), (status_t)OK); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3097 | |
| 3098 | // profile |
| 3099 | OMX_AUDIO_PARAM_AACPROFILETYPE profile; |
| 3100 | InitOMXParams(&profile); |
| 3101 | profile.nPortIndex = kPortIndexOutput; |
| 3102 | CHECK_EQ(mOMX->getParameter(mNode, OMX_IndexParamAudioAac, |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3103 | &profile, sizeof(profile)), (status_t)OK); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3104 | profile.nChannels = numChannels; |
| 3105 | profile.eChannelMode = (numChannels == 1? |
| 3106 | OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo); |
| 3107 | profile.nSampleRate = sampleRate; |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 3108 | profile.nBitRate = bitRate; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3109 | profile.nAudioBandWidth = 0; |
| 3110 | profile.nFrameLength = 0; |
| 3111 | profile.nAACtools = OMX_AUDIO_AACToolAll; |
| 3112 | profile.nAACERtools = OMX_AUDIO_AACERNone; |
| 3113 | profile.eAACProfile = OMX_AUDIO_AACObjectLC; |
| 3114 | profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF; |
| 3115 | CHECK_EQ(mOMX->setParameter(mNode, OMX_IndexParamAudioAac, |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3116 | &profile, sizeof(profile)), (status_t)OK); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3117 | |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3118 | } else { |
| 3119 | OMX_AUDIO_PARAM_AACPROFILETYPE profile; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3120 | InitOMXParams(&profile); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3121 | profile.nPortIndex = kPortIndexInput; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3122 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3123 | status_t err = mOMX->getParameter( |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3124 | mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3125 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3126 | |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3127 | profile.nChannels = numChannels; |
| 3128 | profile.nSampleRate = sampleRate; |
| 3129 | profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3130 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3131 | err = mOMX->setParameter( |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3132 | mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3133 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3134 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | void OMXCodec::setImageOutputFormat( |
| 3138 | OMX_COLOR_FORMATTYPE format, OMX_U32 width, OMX_U32 height) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3139 | CODEC_LOGV("setImageOutputFormat(%ld, %ld)", width, height); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3140 | |
| 3141 | #if 0 |
| 3142 | OMX_INDEXTYPE index; |
| 3143 | status_t err = mOMX->get_extension_index( |
| 3144 | mNode, "OMX.TI.JPEG.decode.Config.OutputColorFormat", &index); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3145 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3146 | |
| 3147 | err = mOMX->set_config(mNode, index, &format, sizeof(format)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3148 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3149 | #endif |
| 3150 | |
| 3151 | OMX_PARAM_PORTDEFINITIONTYPE def; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3152 | InitOMXParams(&def); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3153 | def.nPortIndex = kPortIndexOutput; |
| 3154 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3155 | status_t err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3156 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3157 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3158 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3159 | CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3160 | |
| 3161 | OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 3162 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3163 | CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingUnused); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3164 | imageDef->eColorFormat = format; |
| 3165 | imageDef->nFrameWidth = width; |
| 3166 | imageDef->nFrameHeight = height; |
| 3167 | |
| 3168 | switch (format) { |
| 3169 | case OMX_COLOR_FormatYUV420PackedPlanar: |
| 3170 | case OMX_COLOR_FormatYUV411Planar: |
| 3171 | { |
| 3172 | def.nBufferSize = (width * height * 3) / 2; |
| 3173 | break; |
| 3174 | } |
| 3175 | |
| 3176 | case OMX_COLOR_FormatCbYCrY: |
| 3177 | { |
| 3178 | def.nBufferSize = width * height * 2; |
| 3179 | break; |
| 3180 | } |
| 3181 | |
| 3182 | case OMX_COLOR_Format32bitARGB8888: |
| 3183 | { |
| 3184 | def.nBufferSize = width * height * 4; |
| 3185 | break; |
| 3186 | } |
| 3187 | |
Andreas Huber | 201511c | 2009-09-08 14:01:44 -0700 | [diff] [blame] | 3188 | case OMX_COLOR_Format16bitARGB4444: |
| 3189 | case OMX_COLOR_Format16bitARGB1555: |
| 3190 | case OMX_COLOR_Format16bitRGB565: |
| 3191 | case OMX_COLOR_Format16bitBGR565: |
| 3192 | { |
| 3193 | def.nBufferSize = width * height * 2; |
| 3194 | break; |
| 3195 | } |
| 3196 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3197 | default: |
| 3198 | CHECK(!"Should not be here. Unknown color format."); |
| 3199 | break; |
| 3200 | } |
| 3201 | |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 3202 | def.nBufferCountActual = def.nBufferCountMin; |
| 3203 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3204 | err = mOMX->setParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3205 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3206 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 3207 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3208 | |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 3209 | void OMXCodec::setJPEGInputFormat( |
| 3210 | OMX_U32 width, OMX_U32 height, OMX_U32 compressedSize) { |
| 3211 | OMX_PARAM_PORTDEFINITIONTYPE def; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3212 | InitOMXParams(&def); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3213 | def.nPortIndex = kPortIndexInput; |
| 3214 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3215 | status_t err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3216 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3217 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3218 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3219 | CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainImage); |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 3220 | OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; |
| 3221 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3222 | CHECK_EQ((int)imageDef->eCompressionFormat, (int)OMX_IMAGE_CodingJPEG); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3223 | imageDef->nFrameWidth = width; |
| 3224 | imageDef->nFrameHeight = height; |
| 3225 | |
Andreas Huber | 5c0a913 | 2009-08-20 11:16:40 -0700 | [diff] [blame] | 3226 | def.nBufferSize = compressedSize; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3227 | def.nBufferCountActual = def.nBufferCountMin; |
| 3228 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3229 | err = mOMX->setParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3230 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3231 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | void OMXCodec::addCodecSpecificData(const void *data, size_t size) { |
| 3235 | CodecSpecificData *specific = |
| 3236 | (CodecSpecificData *)malloc(sizeof(CodecSpecificData) + size - 1); |
| 3237 | |
| 3238 | specific->mSize = size; |
| 3239 | memcpy(specific->mData, data, size); |
| 3240 | |
| 3241 | mCodecSpecificData.push(specific); |
| 3242 | } |
| 3243 | |
| 3244 | void OMXCodec::clearCodecSpecificData() { |
| 3245 | for (size_t i = 0; i < mCodecSpecificData.size(); ++i) { |
| 3246 | free(mCodecSpecificData.editItemAt(i)); |
| 3247 | } |
| 3248 | mCodecSpecificData.clear(); |
| 3249 | mCodecSpecificDataIndex = 0; |
| 3250 | } |
| 3251 | |
James Dong | 36e573b | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 3252 | status_t OMXCodec::start(MetaData *meta) { |
Andreas Huber | 42978e5 | 2009-08-27 10:08:39 -0700 | [diff] [blame] | 3253 | Mutex::Autolock autoLock(mLock); |
| 3254 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3255 | if (mState != LOADED) { |
| 3256 | return UNKNOWN_ERROR; |
| 3257 | } |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 3258 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3259 | sp<MetaData> params = new MetaData; |
Andreas Huber | 4f5e602 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 3260 | if (mQuirks & kWantsNALFragments) { |
| 3261 | params->setInt32(kKeyWantsNALFragments, true); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3262 | } |
James Dong | 36e573b | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 3263 | if (meta) { |
| 3264 | int64_t startTimeUs = 0; |
| 3265 | int64_t timeUs; |
| 3266 | if (meta->findInt64(kKeyTime, &timeUs)) { |
| 3267 | startTimeUs = timeUs; |
| 3268 | } |
| 3269 | params->setInt64(kKeyTime, startTimeUs); |
| 3270 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3271 | status_t err = mSource->start(params.get()); |
| 3272 | |
| 3273 | if (err != OK) { |
| 3274 | return err; |
| 3275 | } |
| 3276 | |
| 3277 | mCodecSpecificDataIndex = 0; |
Andreas Huber | 42978e5 | 2009-08-27 10:08:39 -0700 | [diff] [blame] | 3278 | mInitialBufferSubmit = true; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3279 | mSignalledEOS = false; |
| 3280 | mNoMoreOutputData = false; |
Andreas Huber | cfd5557 | 2009-10-09 14:11:28 -0700 | [diff] [blame] | 3281 | mOutputPortSettingsHaveChanged = false; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3282 | mSeekTimeUs = -1; |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 3283 | mSeekMode = ReadOptions::SEEK_CLOSEST_SYNC; |
| 3284 | mTargetTimeUs = -1; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3285 | mFilledBuffers.clear(); |
Andreas Huber | 1f24b30 | 2010-06-10 11:12:39 -0700 | [diff] [blame] | 3286 | mPaused = false; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3287 | |
| 3288 | return init(); |
| 3289 | } |
| 3290 | |
| 3291 | status_t OMXCodec::stop() { |
Andreas Huber | 7ff8939 | 2011-02-15 15:34:11 -0800 | [diff] [blame] | 3292 | CODEC_LOGV("stop mState=%d", mState); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3293 | |
| 3294 | Mutex::Autolock autoLock(mLock); |
| 3295 | |
| 3296 | while (isIntermediateState(mState)) { |
| 3297 | mAsyncCompletion.wait(mLock); |
| 3298 | } |
| 3299 | |
| 3300 | switch (mState) { |
| 3301 | case LOADED: |
| 3302 | case ERROR: |
| 3303 | break; |
| 3304 | |
| 3305 | case EXECUTING: |
| 3306 | { |
| 3307 | setState(EXECUTING_TO_IDLE); |
| 3308 | |
Andreas Huber | 127fcdc | 2009-08-26 16:27:02 -0700 | [diff] [blame] | 3309 | if (mQuirks & kRequiresFlushBeforeShutdown) { |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3310 | CODEC_LOGV("This component requires a flush before transitioning " |
Andreas Huber | 127fcdc | 2009-08-26 16:27:02 -0700 | [diff] [blame] | 3311 | "from EXECUTING to IDLE..."); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3312 | |
Andreas Huber | 127fcdc | 2009-08-26 16:27:02 -0700 | [diff] [blame] | 3313 | bool emulateInputFlushCompletion = |
| 3314 | !flushPortAsync(kPortIndexInput); |
| 3315 | |
| 3316 | bool emulateOutputFlushCompletion = |
| 3317 | !flushPortAsync(kPortIndexOutput); |
| 3318 | |
| 3319 | if (emulateInputFlushCompletion) { |
| 3320 | onCmdComplete(OMX_CommandFlush, kPortIndexInput); |
| 3321 | } |
| 3322 | |
| 3323 | if (emulateOutputFlushCompletion) { |
| 3324 | onCmdComplete(OMX_CommandFlush, kPortIndexOutput); |
| 3325 | } |
| 3326 | } else { |
| 3327 | mPortStatus[kPortIndexInput] = SHUTTING_DOWN; |
| 3328 | mPortStatus[kPortIndexOutput] = SHUTTING_DOWN; |
| 3329 | |
| 3330 | status_t err = |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3331 | mOMX->sendCommand(mNode, OMX_CommandStateSet, OMX_StateIdle); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3332 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | 127fcdc | 2009-08-26 16:27:02 -0700 | [diff] [blame] | 3333 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3334 | |
| 3335 | while (mState != LOADED && mState != ERROR) { |
| 3336 | mAsyncCompletion.wait(mLock); |
| 3337 | } |
| 3338 | |
| 3339 | break; |
| 3340 | } |
| 3341 | |
| 3342 | default: |
| 3343 | { |
| 3344 | CHECK(!"should not be here."); |
| 3345 | break; |
| 3346 | } |
| 3347 | } |
| 3348 | |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 3349 | if (mLeftOverBuffer) { |
| 3350 | mLeftOverBuffer->release(); |
| 3351 | mLeftOverBuffer = NULL; |
| 3352 | } |
| 3353 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3354 | mSource->stop(); |
| 3355 | |
Andreas Huber | 06be3b1 | 2011-01-25 14:55:00 -0800 | [diff] [blame] | 3356 | CODEC_LOGI("stopped in state %d", mState); |
Andreas Huber | 4a9375e | 2010-02-09 11:54:33 -0800 | [diff] [blame] | 3357 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3358 | return OK; |
| 3359 | } |
| 3360 | |
| 3361 | sp<MetaData> OMXCodec::getFormat() { |
Andreas Huber | cfd5557 | 2009-10-09 14:11:28 -0700 | [diff] [blame] | 3362 | Mutex::Autolock autoLock(mLock); |
| 3363 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3364 | return mOutputFormat; |
| 3365 | } |
| 3366 | |
| 3367 | status_t OMXCodec::read( |
| 3368 | MediaBuffer **buffer, const ReadOptions *options) { |
| 3369 | *buffer = NULL; |
| 3370 | |
| 3371 | Mutex::Autolock autoLock(mLock); |
| 3372 | |
Andreas Huber | d06e5b8 | 2009-08-28 13:18:14 -0700 | [diff] [blame] | 3373 | if (mState != EXECUTING && mState != RECONFIGURING) { |
| 3374 | return UNKNOWN_ERROR; |
| 3375 | } |
| 3376 | |
Andreas Huber | e981c33 | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 3377 | bool seeking = false; |
| 3378 | int64_t seekTimeUs; |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 3379 | ReadOptions::SeekMode seekMode; |
| 3380 | if (options && options->getSeekTo(&seekTimeUs, &seekMode)) { |
Andreas Huber | e981c33 | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 3381 | seeking = true; |
| 3382 | } |
| 3383 | |
Andreas Huber | 42978e5 | 2009-08-27 10:08:39 -0700 | [diff] [blame] | 3384 | if (mInitialBufferSubmit) { |
| 3385 | mInitialBufferSubmit = false; |
| 3386 | |
Andreas Huber | e981c33 | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 3387 | if (seeking) { |
| 3388 | CHECK(seekTimeUs >= 0); |
| 3389 | mSeekTimeUs = seekTimeUs; |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 3390 | mSeekMode = seekMode; |
Andreas Huber | e981c33 | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 3391 | |
| 3392 | // There's no reason to trigger the code below, there's |
| 3393 | // nothing to flush yet. |
| 3394 | seeking = false; |
Andreas Huber | 1f24b30 | 2010-06-10 11:12:39 -0700 | [diff] [blame] | 3395 | mPaused = false; |
Andreas Huber | e981c33 | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 3396 | } |
| 3397 | |
Andreas Huber | 42978e5 | 2009-08-27 10:08:39 -0700 | [diff] [blame] | 3398 | drainInputBuffers(); |
Andreas Huber | 42978e5 | 2009-08-27 10:08:39 -0700 | [diff] [blame] | 3399 | |
Andreas Huber | d06e5b8 | 2009-08-28 13:18:14 -0700 | [diff] [blame] | 3400 | if (mState == EXECUTING) { |
| 3401 | // Otherwise mState == RECONFIGURING and this code will trigger |
| 3402 | // after the output port is reenabled. |
| 3403 | fillOutputBuffers(); |
| 3404 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3405 | } |
| 3406 | |
Andreas Huber | e981c33 | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 3407 | if (seeking) { |
Andreas Huber | b928983 | 2011-02-08 13:10:25 -0800 | [diff] [blame] | 3408 | while (mState == RECONFIGURING) { |
| 3409 | mBufferFilled.wait(mLock); |
| 3410 | } |
| 3411 | |
| 3412 | if (mState != EXECUTING) { |
| 3413 | return UNKNOWN_ERROR; |
| 3414 | } |
| 3415 | |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3416 | CODEC_LOGV("seeking to %lld us (%.2f secs)", seekTimeUs, seekTimeUs / 1E6); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3417 | |
| 3418 | mSignalledEOS = false; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3419 | |
| 3420 | CHECK(seekTimeUs >= 0); |
| 3421 | mSeekTimeUs = seekTimeUs; |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 3422 | mSeekMode = seekMode; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3423 | |
| 3424 | mFilledBuffers.clear(); |
| 3425 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3426 | CHECK_EQ((int)mState, (int)EXECUTING); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3427 | |
Andreas Huber | 404cc41 | 2009-08-25 14:26:05 -0700 | [diff] [blame] | 3428 | bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput); |
| 3429 | bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput); |
| 3430 | |
| 3431 | if (emulateInputFlushCompletion) { |
| 3432 | onCmdComplete(OMX_CommandFlush, kPortIndexInput); |
| 3433 | } |
| 3434 | |
| 3435 | if (emulateOutputFlushCompletion) { |
| 3436 | onCmdComplete(OMX_CommandFlush, kPortIndexOutput); |
| 3437 | } |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 3438 | |
| 3439 | while (mSeekTimeUs >= 0) { |
| 3440 | mBufferFilled.wait(mLock); |
| 3441 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) { |
James Dong | 7455630 | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 3445 | if (mIsEncoder) { |
| 3446 | if (NO_ERROR != mBufferFilled.waitRelative(mLock, 3000000000LL)) { |
| 3447 | LOGW("Timed out waiting for buffers from video encoder: %d/%d", |
| 3448 | countBuffersWeOwn(mPortBuffers[kPortIndexInput]), |
| 3449 | countBuffersWeOwn(mPortBuffers[kPortIndexOutput])); |
| 3450 | } |
| 3451 | } else { |
| 3452 | mBufferFilled.wait(mLock); |
| 3453 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | if (mState == ERROR) { |
| 3457 | return UNKNOWN_ERROR; |
| 3458 | } |
| 3459 | |
| 3460 | if (mFilledBuffers.empty()) { |
Andreas Huber | d7d22eb | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 3461 | return mSignalledEOS ? mFinalStatus : ERROR_END_OF_STREAM; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
Andreas Huber | cfd5557 | 2009-10-09 14:11:28 -0700 | [diff] [blame] | 3464 | if (mOutputPortSettingsHaveChanged) { |
| 3465 | mOutputPortSettingsHaveChanged = false; |
| 3466 | |
| 3467 | return INFO_FORMAT_CHANGED; |
| 3468 | } |
| 3469 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3470 | size_t index = *mFilledBuffers.begin(); |
| 3471 | mFilledBuffers.erase(mFilledBuffers.begin()); |
| 3472 | |
| 3473 | BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 3474 | CHECK_EQ((int)info->mStatus, (int)OWNED_BY_US); |
| 3475 | info->mStatus = OWNED_BY_CLIENT; |
| 3476 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3477 | info->mMediaBuffer->add_ref(); |
| 3478 | *buffer = info->mMediaBuffer; |
| 3479 | |
| 3480 | return OK; |
| 3481 | } |
| 3482 | |
| 3483 | void OMXCodec::signalBufferReturned(MediaBuffer *buffer) { |
| 3484 | Mutex::Autolock autoLock(mLock); |
| 3485 | |
| 3486 | Vector<BufferInfo> *buffers = &mPortBuffers[kPortIndexOutput]; |
| 3487 | for (size_t i = 0; i < buffers->size(); ++i) { |
| 3488 | BufferInfo *info = &buffers->editItemAt(i); |
| 3489 | |
| 3490 | if (info->mMediaBuffer == buffer) { |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3491 | CHECK_EQ((int)mPortStatus[kPortIndexOutput], (int)ENABLED); |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 3492 | CHECK_EQ((int)info->mStatus, (int)OWNED_BY_CLIENT); |
| 3493 | |
| 3494 | info->mStatus = OWNED_BY_US; |
| 3495 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 3496 | if (buffer->graphicBuffer() == 0) { |
| 3497 | fillOutputBuffer(info); |
| 3498 | } else { |
| 3499 | sp<MetaData> metaData = info->mMediaBuffer->meta_data(); |
| 3500 | int32_t rendered = 0; |
| 3501 | if (!metaData->findInt32(kKeyRendered, &rendered)) { |
| 3502 | rendered = 0; |
| 3503 | } |
| 3504 | if (!rendered) { |
| 3505 | status_t err = cancelBufferToNativeWindow(info); |
| 3506 | if (err < 0) { |
| 3507 | return; |
| 3508 | } |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 3509 | } |
| 3510 | |
Andreas Huber | bbbcf65 | 2010-12-07 14:25:54 -0800 | [diff] [blame] | 3511 | info->mStatus = OWNED_BY_NATIVE_WINDOW; |
| 3512 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 3513 | // Dequeue the next buffer from the native window. |
| 3514 | BufferInfo *nextBufInfo = dequeueBufferFromNativeWindow(); |
| 3515 | if (nextBufInfo == 0) { |
| 3516 | return; |
| 3517 | } |
| 3518 | |
| 3519 | // Give the buffer to the OMX node to fill. |
| 3520 | fillOutputBuffer(nextBufInfo); |
| 3521 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3522 | return; |
| 3523 | } |
| 3524 | } |
| 3525 | |
| 3526 | CHECK(!"should not be here."); |
| 3527 | } |
| 3528 | |
| 3529 | static const char *imageCompressionFormatString(OMX_IMAGE_CODINGTYPE type) { |
| 3530 | static const char *kNames[] = { |
| 3531 | "OMX_IMAGE_CodingUnused", |
| 3532 | "OMX_IMAGE_CodingAutoDetect", |
| 3533 | "OMX_IMAGE_CodingJPEG", |
| 3534 | "OMX_IMAGE_CodingJPEG2K", |
| 3535 | "OMX_IMAGE_CodingEXIF", |
| 3536 | "OMX_IMAGE_CodingTIFF", |
| 3537 | "OMX_IMAGE_CodingGIF", |
| 3538 | "OMX_IMAGE_CodingPNG", |
| 3539 | "OMX_IMAGE_CodingLZW", |
| 3540 | "OMX_IMAGE_CodingBMP", |
| 3541 | }; |
| 3542 | |
| 3543 | size_t numNames = sizeof(kNames) / sizeof(kNames[0]); |
| 3544 | |
| 3545 | if (type < 0 || (size_t)type >= numNames) { |
| 3546 | return "UNKNOWN"; |
| 3547 | } else { |
| 3548 | return kNames[type]; |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | static const char *colorFormatString(OMX_COLOR_FORMATTYPE type) { |
| 3553 | static const char *kNames[] = { |
| 3554 | "OMX_COLOR_FormatUnused", |
| 3555 | "OMX_COLOR_FormatMonochrome", |
| 3556 | "OMX_COLOR_Format8bitRGB332", |
| 3557 | "OMX_COLOR_Format12bitRGB444", |
| 3558 | "OMX_COLOR_Format16bitARGB4444", |
| 3559 | "OMX_COLOR_Format16bitARGB1555", |
| 3560 | "OMX_COLOR_Format16bitRGB565", |
| 3561 | "OMX_COLOR_Format16bitBGR565", |
| 3562 | "OMX_COLOR_Format18bitRGB666", |
| 3563 | "OMX_COLOR_Format18bitARGB1665", |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 3564 | "OMX_COLOR_Format19bitARGB1666", |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3565 | "OMX_COLOR_Format24bitRGB888", |
| 3566 | "OMX_COLOR_Format24bitBGR888", |
| 3567 | "OMX_COLOR_Format24bitARGB1887", |
| 3568 | "OMX_COLOR_Format25bitARGB1888", |
| 3569 | "OMX_COLOR_Format32bitBGRA8888", |
| 3570 | "OMX_COLOR_Format32bitARGB8888", |
| 3571 | "OMX_COLOR_FormatYUV411Planar", |
| 3572 | "OMX_COLOR_FormatYUV411PackedPlanar", |
| 3573 | "OMX_COLOR_FormatYUV420Planar", |
| 3574 | "OMX_COLOR_FormatYUV420PackedPlanar", |
| 3575 | "OMX_COLOR_FormatYUV420SemiPlanar", |
| 3576 | "OMX_COLOR_FormatYUV422Planar", |
| 3577 | "OMX_COLOR_FormatYUV422PackedPlanar", |
| 3578 | "OMX_COLOR_FormatYUV422SemiPlanar", |
| 3579 | "OMX_COLOR_FormatYCbYCr", |
| 3580 | "OMX_COLOR_FormatYCrYCb", |
| 3581 | "OMX_COLOR_FormatCbYCrY", |
| 3582 | "OMX_COLOR_FormatCrYCbY", |
| 3583 | "OMX_COLOR_FormatYUV444Interleaved", |
| 3584 | "OMX_COLOR_FormatRawBayer8bit", |
| 3585 | "OMX_COLOR_FormatRawBayer10bit", |
| 3586 | "OMX_COLOR_FormatRawBayer8bitcompressed", |
Andreas Huber | ebf66ea | 2009-08-19 13:32:58 -0700 | [diff] [blame] | 3587 | "OMX_COLOR_FormatL2", |
| 3588 | "OMX_COLOR_FormatL4", |
| 3589 | "OMX_COLOR_FormatL8", |
| 3590 | "OMX_COLOR_FormatL16", |
| 3591 | "OMX_COLOR_FormatL24", |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3592 | "OMX_COLOR_FormatL32", |
| 3593 | "OMX_COLOR_FormatYUV420PackedSemiPlanar", |
| 3594 | "OMX_COLOR_FormatYUV422PackedSemiPlanar", |
| 3595 | "OMX_COLOR_Format18BitBGR666", |
| 3596 | "OMX_COLOR_Format24BitARGB6666", |
| 3597 | "OMX_COLOR_Format24BitABGR6666", |
| 3598 | }; |
| 3599 | |
| 3600 | size_t numNames = sizeof(kNames) / sizeof(kNames[0]); |
| 3601 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3602 | if (type == OMX_QCOM_COLOR_FormatYVU420SemiPlanar) { |
| 3603 | return "OMX_QCOM_COLOR_FormatYVU420SemiPlanar"; |
| 3604 | } else if (type < 0 || (size_t)type >= numNames) { |
| 3605 | return "UNKNOWN"; |
| 3606 | } else { |
| 3607 | return kNames[type]; |
| 3608 | } |
| 3609 | } |
| 3610 | |
| 3611 | static const char *videoCompressionFormatString(OMX_VIDEO_CODINGTYPE type) { |
| 3612 | static const char *kNames[] = { |
| 3613 | "OMX_VIDEO_CodingUnused", |
| 3614 | "OMX_VIDEO_CodingAutoDetect", |
| 3615 | "OMX_VIDEO_CodingMPEG2", |
| 3616 | "OMX_VIDEO_CodingH263", |
| 3617 | "OMX_VIDEO_CodingMPEG4", |
| 3618 | "OMX_VIDEO_CodingWMV", |
| 3619 | "OMX_VIDEO_CodingRV", |
| 3620 | "OMX_VIDEO_CodingAVC", |
| 3621 | "OMX_VIDEO_CodingMJPEG", |
| 3622 | }; |
| 3623 | |
| 3624 | size_t numNames = sizeof(kNames) / sizeof(kNames[0]); |
| 3625 | |
| 3626 | if (type < 0 || (size_t)type >= numNames) { |
| 3627 | return "UNKNOWN"; |
| 3628 | } else { |
| 3629 | return kNames[type]; |
| 3630 | } |
| 3631 | } |
| 3632 | |
| 3633 | static const char *audioCodingTypeString(OMX_AUDIO_CODINGTYPE type) { |
| 3634 | static const char *kNames[] = { |
| 3635 | "OMX_AUDIO_CodingUnused", |
| 3636 | "OMX_AUDIO_CodingAutoDetect", |
| 3637 | "OMX_AUDIO_CodingPCM", |
| 3638 | "OMX_AUDIO_CodingADPCM", |
| 3639 | "OMX_AUDIO_CodingAMR", |
| 3640 | "OMX_AUDIO_CodingGSMFR", |
| 3641 | "OMX_AUDIO_CodingGSMEFR", |
| 3642 | "OMX_AUDIO_CodingGSMHR", |
| 3643 | "OMX_AUDIO_CodingPDCFR", |
| 3644 | "OMX_AUDIO_CodingPDCEFR", |
| 3645 | "OMX_AUDIO_CodingPDCHR", |
| 3646 | "OMX_AUDIO_CodingTDMAFR", |
| 3647 | "OMX_AUDIO_CodingTDMAEFR", |
| 3648 | "OMX_AUDIO_CodingQCELP8", |
| 3649 | "OMX_AUDIO_CodingQCELP13", |
| 3650 | "OMX_AUDIO_CodingEVRC", |
| 3651 | "OMX_AUDIO_CodingSMV", |
| 3652 | "OMX_AUDIO_CodingG711", |
| 3653 | "OMX_AUDIO_CodingG723", |
| 3654 | "OMX_AUDIO_CodingG726", |
| 3655 | "OMX_AUDIO_CodingG729", |
| 3656 | "OMX_AUDIO_CodingAAC", |
| 3657 | "OMX_AUDIO_CodingMP3", |
| 3658 | "OMX_AUDIO_CodingSBC", |
| 3659 | "OMX_AUDIO_CodingVORBIS", |
| 3660 | "OMX_AUDIO_CodingWMA", |
| 3661 | "OMX_AUDIO_CodingRA", |
| 3662 | "OMX_AUDIO_CodingMIDI", |
| 3663 | }; |
| 3664 | |
| 3665 | size_t numNames = sizeof(kNames) / sizeof(kNames[0]); |
| 3666 | |
| 3667 | if (type < 0 || (size_t)type >= numNames) { |
| 3668 | return "UNKNOWN"; |
| 3669 | } else { |
| 3670 | return kNames[type]; |
| 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | static const char *audioPCMModeString(OMX_AUDIO_PCMMODETYPE type) { |
| 3675 | static const char *kNames[] = { |
| 3676 | "OMX_AUDIO_PCMModeLinear", |
| 3677 | "OMX_AUDIO_PCMModeALaw", |
| 3678 | "OMX_AUDIO_PCMModeMULaw", |
| 3679 | }; |
| 3680 | |
| 3681 | size_t numNames = sizeof(kNames) / sizeof(kNames[0]); |
| 3682 | |
| 3683 | if (type < 0 || (size_t)type >= numNames) { |
| 3684 | return "UNKNOWN"; |
| 3685 | } else { |
| 3686 | return kNames[type]; |
| 3687 | } |
| 3688 | } |
| 3689 | |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3690 | static const char *amrBandModeString(OMX_AUDIO_AMRBANDMODETYPE type) { |
| 3691 | static const char *kNames[] = { |
| 3692 | "OMX_AUDIO_AMRBandModeUnused", |
| 3693 | "OMX_AUDIO_AMRBandModeNB0", |
| 3694 | "OMX_AUDIO_AMRBandModeNB1", |
| 3695 | "OMX_AUDIO_AMRBandModeNB2", |
| 3696 | "OMX_AUDIO_AMRBandModeNB3", |
| 3697 | "OMX_AUDIO_AMRBandModeNB4", |
| 3698 | "OMX_AUDIO_AMRBandModeNB5", |
| 3699 | "OMX_AUDIO_AMRBandModeNB6", |
| 3700 | "OMX_AUDIO_AMRBandModeNB7", |
| 3701 | "OMX_AUDIO_AMRBandModeWB0", |
| 3702 | "OMX_AUDIO_AMRBandModeWB1", |
| 3703 | "OMX_AUDIO_AMRBandModeWB2", |
| 3704 | "OMX_AUDIO_AMRBandModeWB3", |
| 3705 | "OMX_AUDIO_AMRBandModeWB4", |
| 3706 | "OMX_AUDIO_AMRBandModeWB5", |
| 3707 | "OMX_AUDIO_AMRBandModeWB6", |
| 3708 | "OMX_AUDIO_AMRBandModeWB7", |
| 3709 | "OMX_AUDIO_AMRBandModeWB8", |
| 3710 | }; |
| 3711 | |
| 3712 | size_t numNames = sizeof(kNames) / sizeof(kNames[0]); |
| 3713 | |
| 3714 | if (type < 0 || (size_t)type >= numNames) { |
| 3715 | return "UNKNOWN"; |
| 3716 | } else { |
| 3717 | return kNames[type]; |
| 3718 | } |
| 3719 | } |
| 3720 | |
| 3721 | static const char *amrFrameFormatString(OMX_AUDIO_AMRFRAMEFORMATTYPE type) { |
| 3722 | static const char *kNames[] = { |
| 3723 | "OMX_AUDIO_AMRFrameFormatConformance", |
| 3724 | "OMX_AUDIO_AMRFrameFormatIF1", |
| 3725 | "OMX_AUDIO_AMRFrameFormatIF2", |
| 3726 | "OMX_AUDIO_AMRFrameFormatFSF", |
| 3727 | "OMX_AUDIO_AMRFrameFormatRTPPayload", |
| 3728 | "OMX_AUDIO_AMRFrameFormatITU", |
| 3729 | }; |
| 3730 | |
| 3731 | size_t numNames = sizeof(kNames) / sizeof(kNames[0]); |
| 3732 | |
| 3733 | if (type < 0 || (size_t)type >= numNames) { |
| 3734 | return "UNKNOWN"; |
| 3735 | } else { |
| 3736 | return kNames[type]; |
| 3737 | } |
| 3738 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3739 | |
| 3740 | void OMXCodec::dumpPortStatus(OMX_U32 portIndex) { |
| 3741 | OMX_PARAM_PORTDEFINITIONTYPE def; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3742 | InitOMXParams(&def); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3743 | def.nPortIndex = portIndex; |
| 3744 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3745 | status_t err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3746 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3747 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3748 | |
| 3749 | printf("%s Port = {\n", portIndex == kPortIndexInput ? "Input" : "Output"); |
| 3750 | |
| 3751 | CHECK((portIndex == kPortIndexInput && def.eDir == OMX_DirInput) |
| 3752 | || (portIndex == kPortIndexOutput && def.eDir == OMX_DirOutput)); |
| 3753 | |
| 3754 | printf(" nBufferCountActual = %ld\n", def.nBufferCountActual); |
| 3755 | printf(" nBufferCountMin = %ld\n", def.nBufferCountMin); |
| 3756 | printf(" nBufferSize = %ld\n", def.nBufferSize); |
| 3757 | |
| 3758 | switch (def.eDomain) { |
| 3759 | case OMX_PortDomainImage: |
| 3760 | { |
| 3761 | const OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; |
| 3762 | |
| 3763 | printf("\n"); |
| 3764 | printf(" // Image\n"); |
| 3765 | printf(" nFrameWidth = %ld\n", imageDef->nFrameWidth); |
| 3766 | printf(" nFrameHeight = %ld\n", imageDef->nFrameHeight); |
| 3767 | printf(" nStride = %ld\n", imageDef->nStride); |
| 3768 | |
| 3769 | printf(" eCompressionFormat = %s\n", |
| 3770 | imageCompressionFormatString(imageDef->eCompressionFormat)); |
| 3771 | |
| 3772 | printf(" eColorFormat = %s\n", |
| 3773 | colorFormatString(imageDef->eColorFormat)); |
| 3774 | |
| 3775 | break; |
| 3776 | } |
| 3777 | |
| 3778 | case OMX_PortDomainVideo: |
| 3779 | { |
| 3780 | OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video; |
| 3781 | |
| 3782 | printf("\n"); |
| 3783 | printf(" // Video\n"); |
| 3784 | printf(" nFrameWidth = %ld\n", videoDef->nFrameWidth); |
| 3785 | printf(" nFrameHeight = %ld\n", videoDef->nFrameHeight); |
| 3786 | printf(" nStride = %ld\n", videoDef->nStride); |
| 3787 | |
| 3788 | printf(" eCompressionFormat = %s\n", |
| 3789 | videoCompressionFormatString(videoDef->eCompressionFormat)); |
| 3790 | |
| 3791 | printf(" eColorFormat = %s\n", |
| 3792 | colorFormatString(videoDef->eColorFormat)); |
| 3793 | |
| 3794 | break; |
| 3795 | } |
| 3796 | |
| 3797 | case OMX_PortDomainAudio: |
| 3798 | { |
| 3799 | OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio; |
| 3800 | |
| 3801 | printf("\n"); |
| 3802 | printf(" // Audio\n"); |
| 3803 | printf(" eEncoding = %s\n", |
| 3804 | audioCodingTypeString(audioDef->eEncoding)); |
| 3805 | |
| 3806 | if (audioDef->eEncoding == OMX_AUDIO_CodingPCM) { |
| 3807 | OMX_AUDIO_PARAM_PCMMODETYPE params; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3808 | InitOMXParams(¶ms); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3809 | params.nPortIndex = portIndex; |
| 3810 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3811 | err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3812 | mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3813 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3814 | |
| 3815 | printf(" nSamplingRate = %ld\n", params.nSamplingRate); |
| 3816 | printf(" nChannels = %ld\n", params.nChannels); |
| 3817 | printf(" bInterleaved = %d\n", params.bInterleaved); |
| 3818 | printf(" nBitPerSample = %ld\n", params.nBitPerSample); |
| 3819 | |
| 3820 | printf(" eNumData = %s\n", |
| 3821 | params.eNumData == OMX_NumericalDataSigned |
| 3822 | ? "signed" : "unsigned"); |
| 3823 | |
| 3824 | printf(" ePCMMode = %s\n", audioPCMModeString(params.ePCMMode)); |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3825 | } else if (audioDef->eEncoding == OMX_AUDIO_CodingAMR) { |
| 3826 | OMX_AUDIO_PARAM_AMRTYPE amr; |
| 3827 | InitOMXParams(&amr); |
| 3828 | amr.nPortIndex = portIndex; |
| 3829 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3830 | err = mOMX->getParameter( |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3831 | mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3832 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3833 | |
| 3834 | printf(" nChannels = %ld\n", amr.nChannels); |
| 3835 | printf(" eAMRBandMode = %s\n", |
| 3836 | amrBandModeString(amr.eAMRBandMode)); |
| 3837 | printf(" eAMRFrameFormat = %s\n", |
| 3838 | amrFrameFormatString(amr.eAMRFrameFormat)); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | break; |
| 3842 | } |
| 3843 | |
| 3844 | default: |
| 3845 | { |
| 3846 | printf(" // Unknown\n"); |
| 3847 | break; |
| 3848 | } |
| 3849 | } |
| 3850 | |
| 3851 | printf("}\n"); |
| 3852 | } |
| 3853 | |
Jamie Gennis | 58a36ad | 2010-10-07 14:08:38 -0700 | [diff] [blame] | 3854 | status_t OMXCodec::initNativeWindow() { |
| 3855 | // Enable use of a GraphicBuffer as the output for this node. This must |
| 3856 | // happen before getting the IndexParamPortDefinition parameter because it |
| 3857 | // will affect the pixel format that the node reports. |
| 3858 | status_t err = mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_TRUE); |
| 3859 | if (err != 0) { |
| 3860 | return err; |
| 3861 | } |
| 3862 | |
| 3863 | return OK; |
| 3864 | } |
| 3865 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3866 | void OMXCodec::initOutputFormat(const sp<MetaData> &inputFormat) { |
| 3867 | mOutputFormat = new MetaData; |
| 3868 | mOutputFormat->setCString(kKeyDecoderComponent, mComponentName); |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 3869 | if (mIsEncoder) { |
| 3870 | int32_t timeScale; |
| 3871 | if (inputFormat->findInt32(kKeyTimeScale, &timeScale)) { |
| 3872 | mOutputFormat->setInt32(kKeyTimeScale, timeScale); |
| 3873 | } |
| 3874 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3875 | |
| 3876 | OMX_PARAM_PORTDEFINITIONTYPE def; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3877 | InitOMXParams(&def); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3878 | def.nPortIndex = kPortIndexOutput; |
| 3879 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3880 | status_t err = mOMX->getParameter( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3881 | mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3882 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3883 | |
| 3884 | switch (def.eDomain) { |
| 3885 | case OMX_PortDomainImage: |
| 3886 | { |
| 3887 | OMX_IMAGE_PORTDEFINITIONTYPE *imageDef = &def.format.image; |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3888 | CHECK_EQ((int)imageDef->eCompressionFormat, |
| 3889 | (int)OMX_IMAGE_CodingUnused); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3890 | |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 3891 | mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3892 | mOutputFormat->setInt32(kKeyColorFormat, imageDef->eColorFormat); |
| 3893 | mOutputFormat->setInt32(kKeyWidth, imageDef->nFrameWidth); |
| 3894 | mOutputFormat->setInt32(kKeyHeight, imageDef->nFrameHeight); |
| 3895 | break; |
| 3896 | } |
| 3897 | |
| 3898 | case OMX_PortDomainAudio: |
| 3899 | { |
| 3900 | OMX_AUDIO_PORTDEFINITIONTYPE *audio_def = &def.format.audio; |
| 3901 | |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3902 | if (audio_def->eEncoding == OMX_AUDIO_CodingPCM) { |
| 3903 | OMX_AUDIO_PARAM_PCMMODETYPE params; |
Andreas Huber | 4c48342 | 2009-09-02 16:05:36 -0700 | [diff] [blame] | 3904 | InitOMXParams(¶ms); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3905 | params.nPortIndex = kPortIndexOutput; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3906 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3907 | err = mOMX->getParameter( |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3908 | mNode, OMX_IndexParamAudioPcm, ¶ms, sizeof(params)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3909 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3910 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3911 | CHECK_EQ((int)params.eNumData, (int)OMX_NumericalDataSigned); |
| 3912 | CHECK_EQ(params.nBitPerSample, 16u); |
| 3913 | CHECK_EQ((int)params.ePCMMode, (int)OMX_AUDIO_PCMModeLinear); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3914 | |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3915 | int32_t numChannels, sampleRate; |
| 3916 | inputFormat->findInt32(kKeyChannelCount, &numChannels); |
| 3917 | inputFormat->findInt32(kKeySampleRate, &sampleRate); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3918 | |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3919 | if ((OMX_U32)numChannels != params.nChannels) { |
| 3920 | LOGW("Codec outputs a different number of channels than " |
Andreas Huber | e331c7b | 2010-02-01 10:51:50 -0800 | [diff] [blame] | 3921 | "the input stream contains (contains %d channels, " |
| 3922 | "codec outputs %ld channels).", |
| 3923 | numChannels, params.nChannels); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3924 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3925 | |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 3926 | mOutputFormat->setCString( |
| 3927 | kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3928 | |
| 3929 | // Use the codec-advertised number of channels, as some |
| 3930 | // codecs appear to output stereo even if the input data is |
Andreas Huber | e331c7b | 2010-02-01 10:51:50 -0800 | [diff] [blame] | 3931 | // mono. If we know the codec lies about this information, |
| 3932 | // use the actual number of channels instead. |
| 3933 | mOutputFormat->setInt32( |
| 3934 | kKeyChannelCount, |
| 3935 | (mQuirks & kDecoderLiesAboutNumberOfChannels) |
| 3936 | ? numChannels : params.nChannels); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3937 | |
| 3938 | // The codec-reported sampleRate is not reliable... |
| 3939 | mOutputFormat->setInt32(kKeySampleRate, sampleRate); |
| 3940 | } else if (audio_def->eEncoding == OMX_AUDIO_CodingAMR) { |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3941 | OMX_AUDIO_PARAM_AMRTYPE amr; |
| 3942 | InitOMXParams(&amr); |
| 3943 | amr.nPortIndex = kPortIndexOutput; |
| 3944 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 3945 | err = mOMX->getParameter( |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3946 | mNode, OMX_IndexParamAudioAmr, &amr, sizeof(amr)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3947 | CHECK_EQ(err, (status_t)OK); |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3948 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 3949 | CHECK_EQ(amr.nChannels, 1u); |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3950 | mOutputFormat->setInt32(kKeyChannelCount, 1); |
| 3951 | |
| 3952 | if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeNB0 |
| 3953 | && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeNB7) { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 3954 | mOutputFormat->setCString( |
| 3955 | kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_NB); |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3956 | mOutputFormat->setInt32(kKeySampleRate, 8000); |
| 3957 | } else if (amr.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0 |
| 3958 | && amr.eAMRBandMode <= OMX_AUDIO_AMRBandModeWB8) { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 3959 | mOutputFormat->setCString( |
| 3960 | kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AMR_WB); |
Andreas Huber | 7ae02c8 | 2009-09-09 16:29:47 -0700 | [diff] [blame] | 3961 | mOutputFormat->setInt32(kKeySampleRate, 16000); |
| 3962 | } else { |
| 3963 | CHECK(!"Unknown AMR band mode."); |
| 3964 | } |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3965 | } else if (audio_def->eEncoding == OMX_AUDIO_CodingAAC) { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 3966 | mOutputFormat->setCString( |
| 3967 | kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC); |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 3968 | int32_t numChannels, sampleRate, bitRate; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3969 | inputFormat->findInt32(kKeyChannelCount, &numChannels); |
| 3970 | inputFormat->findInt32(kKeySampleRate, &sampleRate); |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 3971 | inputFormat->findInt32(kKeyBitRate, &bitRate); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 3972 | mOutputFormat->setInt32(kKeyChannelCount, numChannels); |
| 3973 | mOutputFormat->setInt32(kKeySampleRate, sampleRate); |
James Dong | 17299ab | 2010-05-14 15:45:22 -0700 | [diff] [blame] | 3974 | mOutputFormat->setInt32(kKeyBitRate, bitRate); |
Andreas Huber | da050cf2 | 2009-09-02 14:01:43 -0700 | [diff] [blame] | 3975 | } else { |
| 3976 | CHECK(!"Should not be here. Unknown audio encoding."); |
Andreas Huber | 43ad6eaf | 2009-09-01 16:02:43 -0700 | [diff] [blame] | 3977 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3978 | break; |
| 3979 | } |
| 3980 | |
| 3981 | case OMX_PortDomainVideo: |
| 3982 | { |
| 3983 | OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video; |
| 3984 | |
| 3985 | if (video_def->eCompressionFormat == OMX_VIDEO_CodingUnused) { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 3986 | mOutputFormat->setCString( |
| 3987 | kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3988 | } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingMPEG4) { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 3989 | mOutputFormat->setCString( |
| 3990 | kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3991 | } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingH263) { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 3992 | mOutputFormat->setCString( |
| 3993 | kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3994 | } else if (video_def->eCompressionFormat == OMX_VIDEO_CodingAVC) { |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 3995 | mOutputFormat->setCString( |
| 3996 | kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 3997 | } else { |
| 3998 | CHECK(!"Unknown compression format."); |
| 3999 | } |
| 4000 | |
James Dong | 5592bcc | 2010-10-22 17:10:43 -0700 | [diff] [blame] | 4001 | mOutputFormat->setInt32(kKeyWidth, video_def->nFrameWidth); |
| 4002 | mOutputFormat->setInt32(kKeyHeight, video_def->nFrameHeight); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 4003 | mOutputFormat->setInt32(kKeyColorFormat, video_def->eColorFormat); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 4004 | |
James Dong | 0c9153d | 2010-11-23 11:30:21 -0800 | [diff] [blame] | 4005 | if (!mIsEncoder) { |
| 4006 | OMX_CONFIG_RECTTYPE rect; |
James Dong | a173541 | 2011-01-07 14:56:34 -0800 | [diff] [blame] | 4007 | InitOMXParams(&rect); |
| 4008 | rect.nPortIndex = kPortIndexOutput; |
James Dong | 0c9153d | 2010-11-23 11:30:21 -0800 | [diff] [blame] | 4009 | status_t err = |
| 4010 | mOMX->getConfig( |
| 4011 | mNode, OMX_IndexConfigCommonOutputCrop, |
| 4012 | &rect, sizeof(rect)); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 4013 | |
James Dong | 0c9153d | 2010-11-23 11:30:21 -0800 | [diff] [blame] | 4014 | if (err == OK) { |
| 4015 | CHECK_GE(rect.nLeft, 0); |
| 4016 | CHECK_GE(rect.nTop, 0); |
| 4017 | CHECK_GE(rect.nWidth, 0u); |
| 4018 | CHECK_GE(rect.nHeight, 0u); |
| 4019 | CHECK_LE(rect.nLeft + rect.nWidth - 1, video_def->nFrameWidth); |
| 4020 | CHECK_LE(rect.nTop + rect.nHeight - 1, video_def->nFrameHeight); |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 4021 | |
James Dong | 0c9153d | 2010-11-23 11:30:21 -0800 | [diff] [blame] | 4022 | mOutputFormat->setRect( |
| 4023 | kKeyCropRect, |
| 4024 | rect.nLeft, |
| 4025 | rect.nTop, |
| 4026 | rect.nLeft + rect.nWidth - 1, |
| 4027 | rect.nTop + rect.nHeight - 1); |
| 4028 | } else { |
| 4029 | mOutputFormat->setRect( |
| 4030 | kKeyCropRect, |
| 4031 | 0, 0, |
| 4032 | video_def->nFrameWidth - 1, |
| 4033 | video_def->nFrameHeight - 1); |
| 4034 | } |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 4035 | } |
| 4036 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 4037 | break; |
| 4038 | } |
| 4039 | |
| 4040 | default: |
| 4041 | { |
| 4042 | CHECK(!"should not be here, neither audio nor video."); |
| 4043 | break; |
| 4044 | } |
| 4045 | } |
| 4046 | } |
| 4047 | |
Andreas Huber | 1f24b30 | 2010-06-10 11:12:39 -0700 | [diff] [blame] | 4048 | status_t OMXCodec::pause() { |
| 4049 | Mutex::Autolock autoLock(mLock); |
| 4050 | |
| 4051 | mPaused = true; |
| 4052 | |
| 4053 | return OK; |
| 4054 | } |
| 4055 | |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 4056 | //////////////////////////////////////////////////////////////////////////////// |
| 4057 | |
| 4058 | status_t QueryCodecs( |
| 4059 | const sp<IOMX> &omx, |
| 4060 | const char *mime, bool queryDecoders, |
| 4061 | Vector<CodecCapabilities> *results) { |
| 4062 | results->clear(); |
| 4063 | |
| 4064 | for (int index = 0;; ++index) { |
| 4065 | const char *componentName; |
| 4066 | |
| 4067 | if (!queryDecoders) { |
| 4068 | componentName = GetCodec( |
| 4069 | kEncoderInfo, sizeof(kEncoderInfo) / sizeof(kEncoderInfo[0]), |
| 4070 | mime, index); |
| 4071 | } else { |
| 4072 | componentName = GetCodec( |
| 4073 | kDecoderInfo, sizeof(kDecoderInfo) / sizeof(kDecoderInfo[0]), |
| 4074 | mime, index); |
| 4075 | } |
| 4076 | |
| 4077 | if (!componentName) { |
| 4078 | return OK; |
| 4079 | } |
| 4080 | |
Andreas Huber | 1a189a8 | 2010-03-24 13:49:20 -0700 | [diff] [blame] | 4081 | if (strncmp(componentName, "OMX.", 4)) { |
| 4082 | // Not an OpenMax component but a software codec. |
| 4083 | |
| 4084 | results->push(); |
| 4085 | CodecCapabilities *caps = &results->editItemAt(results->size() - 1); |
| 4086 | caps->mComponentName = componentName; |
| 4087 | |
| 4088 | continue; |
| 4089 | } |
| 4090 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 4091 | sp<OMXCodecObserver> observer = new OMXCodecObserver; |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 4092 | IOMX::node_id node; |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 4093 | status_t err = omx->allocateNode(componentName, observer, &node); |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 4094 | |
| 4095 | if (err != OK) { |
| 4096 | continue; |
| 4097 | } |
| 4098 | |
James Dong | 722d591 | 2010-04-13 10:56:59 -0700 | [diff] [blame] | 4099 | OMXCodec::setComponentRole(omx, node, !queryDecoders, mime); |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 4100 | |
| 4101 | results->push(); |
| 4102 | CodecCapabilities *caps = &results->editItemAt(results->size() - 1); |
| 4103 | caps->mComponentName = componentName; |
| 4104 | |
| 4105 | OMX_VIDEO_PARAM_PROFILELEVELTYPE param; |
| 4106 | InitOMXParams(¶m); |
| 4107 | |
| 4108 | param.nPortIndex = queryDecoders ? 0 : 1; |
| 4109 | |
| 4110 | for (param.nProfileIndex = 0;; ++param.nProfileIndex) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 4111 | err = omx->getParameter( |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 4112 | node, OMX_IndexParamVideoProfileLevelQuerySupported, |
| 4113 | ¶m, sizeof(param)); |
| 4114 | |
| 4115 | if (err != OK) { |
| 4116 | break; |
| 4117 | } |
| 4118 | |
| 4119 | CodecProfileLevel profileLevel; |
| 4120 | profileLevel.mProfile = param.eProfile; |
| 4121 | profileLevel.mLevel = param.eLevel; |
| 4122 | |
| 4123 | caps->mProfileLevels.push(profileLevel); |
| 4124 | } |
| 4125 | |
James Dong | d781089 | 2010-11-11 00:33:05 -0800 | [diff] [blame] | 4126 | // Color format query |
| 4127 | OMX_VIDEO_PARAM_PORTFORMATTYPE portFormat; |
| 4128 | InitOMXParams(&portFormat); |
| 4129 | portFormat.nPortIndex = queryDecoders ? 1 : 0; |
| 4130 | for (portFormat.nIndex = 0;; ++portFormat.nIndex) { |
| 4131 | err = omx->getParameter( |
| 4132 | node, OMX_IndexParamVideoPortFormat, |
| 4133 | &portFormat, sizeof(portFormat)); |
| 4134 | if (err != OK) { |
| 4135 | break; |
| 4136 | } |
| 4137 | caps->mColorFormats.push(portFormat.eColorFormat); |
| 4138 | } |
| 4139 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame] | 4140 | CHECK_EQ(omx->freeNode(node), (status_t)OK); |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 4141 | } |
| 4142 | } |
| 4143 | |
James Dong | 31b9375f | 2010-11-10 21:11:41 -0800 | [diff] [blame] | 4144 | void OMXCodec::restorePatchedDataPointer(BufferInfo *info) { |
| 4145 | CHECK(mIsEncoder && (mQuirks & kAvoidMemcopyInputRecordingFrames)); |
| 4146 | CHECK(mOMXLivesLocally); |
| 4147 | |
| 4148 | OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *)info->mBuffer; |
| 4149 | header->pBuffer = (OMX_U8 *)info->mData; |
| 4150 | } |
| 4151 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 4152 | } // namespace android |