plugin_win_mf_codec_h264.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /* Copyright (C) 2013 Mamadou DIOP
  2. * Copyright (C) 2013 Doubango Telecom <http://www.doubango.org>
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. */
  19. #include "internals/mf_codec.h"
  20. #include "internals/mf_utils.h"
  21. #include "tinydav/codecs/h264/tdav_codec_h264_common.h"
  22. #include "tinyrtp/rtp/trtp_rtp_packet.h"
  23. #include "tinymedia/tmedia_codec.h"
  24. #include "tinymedia/tmedia_params.h"
  25. #include "tinymedia/tmedia_defaults.h"
  26. #include "tsk_params.h"
  27. #include "tsk_memory.h"
  28. #include "tsk_debug.h"
  29. typedef struct mf_codec_h264_s {
  30. TDAV_DECLARE_CODEC_H264_COMMON;
  31. // Encoder
  32. struct {
  33. MFCodecVideoH264* pInst;
  34. void* buffer;
  35. int64_t frame_count;
  36. tsk_bool_t force_idr;
  37. int32_t quality; // [1-31]
  38. int rotation;
  39. int neg_width;
  40. int neg_height;
  41. int neg_fps;
  42. int max_bitrate_bps;
  43. int32_t max_bw_kpbs;
  44. tsk_bool_t passthrough; // whether to bypass encoding
  45. } encoder;
  46. // decoder
  47. struct {
  48. MFCodecVideoH264* pInst;
  49. void* accumulator;
  50. tsk_size_t accumulator_pos;
  51. tsk_size_t accumulator_size;
  52. uint16_t last_seq;
  53. tsk_bool_t passthrough; // whether to bypass decoding
  54. } decoder;
  55. }
  56. mf_codec_h264_t;
  57. #if !defined(PLUGIN_MF_H264_GOP_SIZE_IN_SECONDS)
  58. # define PLUGIN_MF_H264_GOP_SIZE_IN_SECONDS 25
  59. #endif
  60. static int mf_codec_h264_init(mf_codec_h264_t* self, profile_idc_t profile);
  61. static int mf_codec_h264_deinit(mf_codec_h264_t* self);
  62. static int mf_codec_h264_open_encoder(mf_codec_h264_t* self);
  63. static int mf_codec_h264_close_encoder(mf_codec_h264_t* self);
  64. static int mf_codec_h264_open_decoder(mf_codec_h264_t* self);
  65. static int mf_codec_h264_close_decoder(mf_codec_h264_t* self);
  66. /* ============ H.264 Base/Main Profile X.X Plugin interface functions ================= */
  67. static int mf_codec_h264_set(tmedia_codec_t* self, const tmedia_param_t* param)
  68. {
  69. mf_codec_h264_t* h264 = (mf_codec_h264_t*)self;
  70. if(!self->opened) {
  71. TSK_DEBUG_ERROR("Codec not opened");
  72. return -1;
  73. }
  74. if(param->value_type == tmedia_pvt_int32) {
  75. if(tsk_striequals(param->key, "action")) {
  76. tmedia_codec_action_t action = (tmedia_codec_action_t)TSK_TO_INT32((uint8_t*)param->value);
  77. switch(action) {
  78. case tmedia_codec_action_encode_idr: {
  79. h264->encoder.force_idr = tsk_true;
  80. break;
  81. }
  82. case tmedia_codec_action_bw_down: {
  83. h264->encoder.quality = TSK_CLAMP(1, (h264->encoder.quality + 1), 31);
  84. break;
  85. }
  86. case tmedia_codec_action_bw_up: {
  87. h264->encoder.quality = TSK_CLAMP(1, (h264->encoder.quality - 1), 31);
  88. break;
  89. }
  90. }
  91. return 0;
  92. }
  93. else if(tsk_striequals(param->key, "bypass-encoding")) {
  94. h264->encoder.passthrough = *((int32_t*)param->value) ? tsk_true : tsk_false;
  95. h264->encoder.pInst->setBundled(h264->encoder.passthrough);
  96. TSK_DEBUG_INFO("[H.264] bypass-encoding = %d", h264->encoder.passthrough);
  97. return 0;
  98. }
  99. else if(tsk_striequals(param->key, "bypass-decoding")) {
  100. h264->decoder.passthrough = *((int32_t*)param->value) ? tsk_true : tsk_false;
  101. h264->decoder.pInst->setBundled(h264->decoder.passthrough);
  102. TSK_DEBUG_INFO("[H.264] bypass-decoding = %d", h264->decoder.passthrough);
  103. return 0;
  104. }
  105. else if(tsk_striequals(param->key, "rotation")) {
  106. int rotation = *((int32_t*)param->value);
  107. if(h264->encoder.rotation != rotation) {
  108. if(self->opened) {
  109. int ret;
  110. h264->encoder.rotation = rotation;
  111. if((ret = mf_codec_h264_close_encoder(h264))) {
  112. return ret;
  113. }
  114. if((ret = mf_codec_h264_open_encoder(h264))) {
  115. return ret;
  116. }
  117. }
  118. }
  119. return 0;
  120. }
  121. }
  122. return -1;
  123. }
  124. static int mf_codec_h264_open(tmedia_codec_t* self)
  125. {
  126. int ret;
  127. mf_codec_h264_t* h264 = (mf_codec_h264_t*)self;
  128. if(!h264) {
  129. TSK_DEBUG_ERROR("Invalid parameter");
  130. return -1;
  131. }
  132. /* the caller (base class) already checked that the codec is not opened */
  133. // Encoder
  134. if((ret = mf_codec_h264_open_encoder(h264))) {
  135. return ret;
  136. }
  137. // Decoder
  138. if((ret = mf_codec_h264_open_decoder(h264))) {
  139. return ret;
  140. }
  141. return 0;
  142. }
  143. static int mf_codec_h264_close(tmedia_codec_t* self)
  144. {
  145. mf_codec_h264_t* h264 = (mf_codec_h264_t*)self;
  146. if(!h264) {
  147. TSK_DEBUG_ERROR("Invalid parameter");
  148. return -1;
  149. }
  150. /* the caller (base class) alreasy checked that the codec is opened */
  151. // Encoder
  152. mf_codec_h264_close_encoder(h264);
  153. // Decoder
  154. mf_codec_h264_close_decoder(h264);
  155. return 0;
  156. }
  157. static tsk_size_t mf_codec_h264_encode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size)
  158. {
  159. int ret = 0;
  160. tsk_bool_t send_idr, send_hdr;
  161. mf_codec_h264_t* h264 = (mf_codec_h264_t*)self;
  162. tdav_codec_h264_common_t* common = (tdav_codec_h264_common_t*)self;
  163. if(!self || !in_data || !in_size) {
  164. TSK_DEBUG_ERROR("Invalid parameter");
  165. return 0;
  166. }
  167. if(!self->opened || !h264->encoder.pInst || !h264->encoder.pInst->IsReady()) {
  168. TSK_DEBUG_ERROR("Encoder not opened or not ready");
  169. return 0;
  170. }
  171. HRESULT hr = S_OK;
  172. IMFSample *pSampleOut = NULL;
  173. IMFMediaBuffer* pBufferOut = NULL;
  174. // send IDR for:
  175. // - the first frame
  176. // - remote peer requested an IDR
  177. // - every second within the first 4seconds
  178. send_idr = (
  179. h264->encoder.frame_count++ == 0
  180. || h264 ->encoder.force_idr
  181. || ( (h264->encoder.frame_count < h264->encoder.neg_fps * 4) && ((h264->encoder.frame_count % h264->encoder.neg_fps)==0) )
  182. );
  183. if(send_idr) {
  184. CHECK_HR(hr = h264->encoder.pInst->RequestKeyFrame());
  185. }
  186. // send SPS and PPS headers for:
  187. // - IDR frames (not required but it's the easiest way to deal with pkt loss)
  188. // - every 5 seconds after the first 4seconds
  189. send_hdr = (
  190. send_idr
  191. || ( (h264->encoder.frame_count % (h264->encoder.neg_fps * 5))==0 )
  192. );
  193. if(send_hdr) {
  194. //FIXME: MF_MT_MPEG_SEQUENCE_HEADER
  195. // tdav_codec_h264_rtp_encap(TDAV_CODEC_H264_COMMON(h264), h264->encoder.context->extradata, (tsk_size_t)h264->encoder.context->extradata_size);
  196. }
  197. if (h264->encoder.passthrough) {
  198. tdav_codec_h264_rtp_encap(common, (const uint8_t*)in_data, in_size);
  199. return 0;
  200. }
  201. // Encode data
  202. CHECK_HR(hr = h264->encoder.pInst->Process(in_data, (UINT32)in_size, &pSampleOut));
  203. if(pSampleOut) {
  204. CHECK_HR(hr = pSampleOut->GetBufferByIndex(0, &pBufferOut));
  205. BYTE* pBufferPtr = NULL;
  206. DWORD dwDataLength = 0;
  207. CHECK_HR(hr = pBufferOut->GetCurrentLength(&dwDataLength));
  208. if(dwDataLength > 0) {
  209. CHECK_HR(hr = pBufferOut->Lock(&pBufferPtr, NULL, NULL));
  210. tdav_codec_h264_rtp_encap(common, (const uint8_t*)pBufferPtr, (tsk_size_t)dwDataLength);
  211. CHECK_HR(hr = pBufferOut->Unlock());
  212. }
  213. }
  214. // reset
  215. h264->encoder.force_idr = tsk_false;
  216. bail:
  217. SafeRelease(&pSampleOut);
  218. SafeRelease(&pBufferOut);
  219. return 0;
  220. }
  221. static tsk_size_t mf_codec_h264_decode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr)
  222. {
  223. mf_codec_h264_t* h264 = (mf_codec_h264_t*)self;
  224. const trtp_rtp_header_t* rtp_hdr = (const trtp_rtp_header_t*)proto_hdr;
  225. const uint8_t* pay_ptr = tsk_null;
  226. tsk_size_t pay_size = 0;
  227. int ret;
  228. tsk_bool_t append_scp, end_of_unit;
  229. tsk_bool_t sps_or_pps;
  230. tsk_size_t retsize = 0, size_to_copy = 0;
  231. static const tsk_size_t xmax_size = (3840 * 2160 * 3) >> 3; // >>3 instead of >>1 (not an error)
  232. static tsk_size_t start_code_prefix_size = sizeof(H264_START_CODE_PREFIX);
  233. if(!h264 || !in_data || !in_size || !out_data) {
  234. TSK_DEBUG_ERROR("Invalid parameter");
  235. return 0;
  236. }
  237. if(!self->opened || !h264->encoder.pInst || !h264->decoder.pInst->IsReady()) {
  238. TSK_DEBUG_ERROR("Decoder not opened or not ready");
  239. return 0;
  240. }
  241. HRESULT hr = S_OK;
  242. IMFSample *pSampleOut = NULL;
  243. IMFMediaBuffer* pBufferOut = NULL;
  244. /* Packet lost? */
  245. if((h264->decoder.last_seq + 1) != rtp_hdr->seq_num && h264->decoder.last_seq) {
  246. TSK_DEBUG_INFO("[H.264] Packet loss, seq_num=%d", (h264->decoder.last_seq + 1));
  247. }
  248. h264->decoder.last_seq = rtp_hdr->seq_num;
  249. /* 5.3. NAL Unit Octet Usage
  250. +---------------+
  251. |0|1|2|3|4|5|6|7|
  252. +-+-+-+-+-+-+-+-+
  253. |F|NRI| Type |
  254. +---------------+
  255. */
  256. if (*((uint8_t*)in_data) & 0x80) {
  257. TSK_DEBUG_WARN("F=1");
  258. /* reset accumulator */
  259. h264->decoder.accumulator_pos = 0;
  260. return 0;
  261. }
  262. /* get payload */
  263. if ((ret = tdav_codec_h264_get_pay(in_data, in_size, (const void**)&pay_ptr, &pay_size, &append_scp, &end_of_unit)) || !pay_ptr || !pay_size) {
  264. TSK_DEBUG_ERROR("Depayloader failed to get H.264 content");
  265. return 0;
  266. }
  267. //append_scp = tsk_true;
  268. size_to_copy = pay_size + (append_scp ? start_code_prefix_size : 0);
  269. // whether it's SPS or PPS (append_scp is false for subsequent FUA chuncks)
  270. sps_or_pps = append_scp && pay_ptr && ((pay_ptr[0] & 0x1F) == 7 || (pay_ptr[0] & 0x1F) == 8);
  271. // start-accumulator
  272. if (!h264->decoder.accumulator) {
  273. if (size_to_copy > xmax_size) {
  274. TSK_DEBUG_ERROR("%u too big to contain valid encoded data. xmax_size=%u", size_to_copy, xmax_size);
  275. return 0;
  276. }
  277. if (!(h264->decoder.accumulator = tsk_calloc(size_to_copy, sizeof(uint8_t)))) {
  278. TSK_DEBUG_ERROR("Failed to allocated new buffer");
  279. return 0;
  280. }
  281. h264->decoder.accumulator_size = size_to_copy;
  282. }
  283. if ((h264->decoder.accumulator_pos + size_to_copy) >= xmax_size) {
  284. TSK_DEBUG_ERROR("BufferOverflow");
  285. h264->decoder.accumulator_pos = 0;
  286. return 0;
  287. }
  288. if ((h264->decoder.accumulator_pos + size_to_copy) > h264->decoder.accumulator_size) {
  289. if(!(h264->decoder.accumulator = tsk_realloc(h264->decoder.accumulator, (h264->decoder.accumulator_pos + size_to_copy)))) {
  290. TSK_DEBUG_ERROR("Failed to reallocated new buffer");
  291. h264->decoder.accumulator_pos = 0;
  292. h264->decoder.accumulator_size = 0;
  293. return 0;
  294. }
  295. h264->decoder.accumulator_size = (h264->decoder.accumulator_pos + size_to_copy);
  296. }
  297. if (append_scp) {
  298. memcpy(&((uint8_t*)h264->decoder.accumulator)[h264->decoder.accumulator_pos], H264_START_CODE_PREFIX, start_code_prefix_size);
  299. h264->decoder.accumulator_pos += start_code_prefix_size;
  300. }
  301. memcpy(&((uint8_t*)h264->decoder.accumulator)[h264->decoder.accumulator_pos], pay_ptr, pay_size);
  302. h264->decoder.accumulator_pos += pay_size;
  303. // end-accumulator
  304. /*if(sps_or_pps){
  305. // http://libav-users.943685.n4.nabble.com/Decode-H264-streams-how-to-fill-AVCodecContext-from-SPS-PPS-td2484472.html
  306. // SPS and PPS should be bundled with IDR
  307. TSK_DEBUG_INFO("Receiving SPS or PPS ...to be tied to an IDR");
  308. }
  309. else */if (rtp_hdr->marker) {
  310. if (h264->decoder.passthrough) {
  311. if (*out_max_size < h264->decoder.accumulator_pos) {
  312. if ((*out_data = tsk_realloc(*out_data, h264->decoder.accumulator_pos))) {
  313. *out_max_size = h264->decoder.accumulator_pos;
  314. }
  315. else {
  316. *out_max_size = 0;
  317. return 0;
  318. }
  319. }
  320. memcpy(*out_data, h264->decoder.accumulator, h264->decoder.accumulator_pos);
  321. retsize = h264->decoder.accumulator_pos;
  322. }
  323. else { // !h264->decoder.passthrough
  324. /* decode the picture */
  325. CHECK_HR(hr = h264->decoder.pInst->Process(h264->decoder.accumulator, (UINT32)h264->decoder.accumulator_pos, &pSampleOut));
  326. if (pSampleOut) {
  327. CHECK_HR(hr = pSampleOut->GetBufferByIndex(0, &pBufferOut));
  328. BYTE* pBufferPtr = NULL;
  329. DWORD dwDataLength = 0;
  330. CHECK_HR(hr = pBufferOut->GetCurrentLength(&dwDataLength));
  331. if (dwDataLength > 0) {
  332. CHECK_HR(hr = pBufferOut->Lock(&pBufferPtr, NULL, NULL));
  333. {
  334. /* IDR ? */
  335. if(((pay_ptr[0] & 0x1F) == 0x05) && TMEDIA_CODEC_VIDEO(self)->in.callback) {
  336. TSK_DEBUG_INFO("Decoded H.264 IDR");
  337. TMEDIA_CODEC_VIDEO(self)->in.result.type = tmedia_video_decode_result_type_idr;
  338. TMEDIA_CODEC_VIDEO(self)->in.result.proto_hdr = proto_hdr;
  339. TMEDIA_CODEC_VIDEO(self)->in.callback(&TMEDIA_CODEC_VIDEO(self)->in.result);
  340. }
  341. /* fill out */
  342. if(*out_max_size < dwDataLength) {
  343. if((*out_data = tsk_realloc(*out_data, dwDataLength))) {
  344. *out_max_size = dwDataLength;
  345. }
  346. else {
  347. *out_max_size = 0;
  348. return 0;
  349. }
  350. }
  351. retsize = (tsk_size_t)dwDataLength;
  352. TMEDIA_CODEC_VIDEO(h264)->in.width = h264->decoder.pInst->GetWidth();
  353. TMEDIA_CODEC_VIDEO(h264)->in.height = h264->decoder.pInst->GetHeight();
  354. memcpy(*out_data, pBufferPtr, retsize);
  355. }
  356. CHECK_HR(hr = pBufferOut->Unlock());
  357. }
  358. }
  359. }// else(!h264->decoder.passthrough)
  360. } // else if(rtp_hdr->marker)
  361. bail:
  362. if (rtp_hdr->marker) {
  363. h264->decoder.accumulator_pos = 0;
  364. }
  365. if (FAILED(hr) /*|| (!pSampleOut && rtp_hdr->marker)*/) {
  366. TSK_DEBUG_INFO("Failed to decode the buffer with error code =%d, size=%u, append=%s", ret, h264->decoder.accumulator_pos, append_scp ? "yes" : "no");
  367. if(TMEDIA_CODEC_VIDEO(self)->in.callback) {
  368. TMEDIA_CODEC_VIDEO(self)->in.result.type = tmedia_video_decode_result_type_error;
  369. TMEDIA_CODEC_VIDEO(self)->in.result.proto_hdr = proto_hdr;
  370. TMEDIA_CODEC_VIDEO(self)->in.callback(&TMEDIA_CODEC_VIDEO(self)->in.result);
  371. }
  372. }
  373. SafeRelease(&pSampleOut);
  374. SafeRelease(&pBufferOut);
  375. return retsize;
  376. }
  377. static tsk_bool_t mf_codec_h264_sdp_att_match(const tmedia_codec_t* self, const char* att_name, const char* att_value)
  378. {
  379. return tdav_codec_h264_common_sdp_att_match((tdav_codec_h264_common_t*)self, att_name, att_value);
  380. }
  381. static char* mf_codec_h264_sdp_att_get(const tmedia_codec_t* self, const char* att_name)
  382. {
  383. char* att = tdav_codec_h264_common_sdp_att_get((const tdav_codec_h264_common_t*)self, att_name);
  384. if(att && tsk_striequals(att_name, "fmtp")) {
  385. tsk_strcat(&att, "; impl=MF");
  386. }
  387. return att;
  388. }
  389. /* ============ H.264 Base Profile Plugin interface ================= */
  390. /* constructor */
  391. static tsk_object_t* mf_codec_h264_base_ctor(tsk_object_t * self, va_list * app)
  392. {
  393. mf_codec_h264_t *h264 = (mf_codec_h264_t*)self;
  394. if(h264) {
  395. /* init base: called by tmedia_codec_create() */
  396. /* init self */
  397. if(mf_codec_h264_init(h264, profile_idc_baseline) != 0) {
  398. return tsk_null;
  399. }
  400. }
  401. return self;
  402. }
  403. /* destructor */
  404. static tsk_object_t* mf_codec_h264_base_dtor(tsk_object_t * self)
  405. {
  406. mf_codec_h264_t *h264 = (mf_codec_h264_t*)self;
  407. if(h264) {
  408. /* deinit base */
  409. tdav_codec_h264_common_deinit(TDAV_CODEC_H264_COMMON(self));
  410. /* deinit self */
  411. mf_codec_h264_deinit(h264);
  412. }
  413. return self;
  414. }
  415. /* object definition */
  416. static const tsk_object_def_t mf_codec_h264_base_def_s = {
  417. sizeof(mf_codec_h264_t),
  418. mf_codec_h264_base_ctor,
  419. mf_codec_h264_base_dtor,
  420. tmedia_codec_cmp,
  421. };
  422. /* plugin definition*/
  423. static const tmedia_codec_plugin_def_t mf_codec_h264_base_plugin_def_s = {
  424. &mf_codec_h264_base_def_s,
  425. tmedia_video,
  426. tmedia_codec_id_h264_bp,
  427. "H264",
  428. "H264 Base Profile (Media Foundation)",
  429. TMEDIA_CODEC_FORMAT_H264_BP,
  430. tsk_true,
  431. 90000, // rate
  432. /* audio */
  433. { 0 },
  434. /* video (width, height, fps) */
  435. {176, 144, 0}, // fps is @deprecated
  436. mf_codec_h264_set,
  437. mf_codec_h264_open,
  438. mf_codec_h264_close,
  439. mf_codec_h264_encode,
  440. mf_codec_h264_decode,
  441. mf_codec_h264_sdp_att_match,
  442. mf_codec_h264_sdp_att_get
  443. };
  444. const tmedia_codec_plugin_def_t *mf_codec_h264_base_plugin_def_t = &mf_codec_h264_base_plugin_def_s;
  445. /* ============ H.264 Main Profile Plugin interface ================= */
  446. /* constructor */
  447. static tsk_object_t* mf_codec_h264_main_ctor(tsk_object_t * self, va_list * app)
  448. {
  449. mf_codec_h264_t *h264 = (mf_codec_h264_t*)self;
  450. if(h264) {
  451. /* init base: called by tmedia_codec_create() */
  452. /* init self */
  453. if(mf_codec_h264_init(h264, profile_idc_main) != 0) {
  454. return tsk_null;
  455. }
  456. }
  457. return self;
  458. }
  459. /* destructor */
  460. static tsk_object_t* mf_codec_h264_main_dtor(tsk_object_t * self)
  461. {
  462. mf_codec_h264_t *h264 = (mf_codec_h264_t*)self;
  463. if(h264) {
  464. /* deinit base */
  465. tdav_codec_h264_common_deinit(TDAV_CODEC_H264_COMMON(self));
  466. /* deinit self */
  467. mf_codec_h264_deinit(h264);
  468. }
  469. return self;
  470. }
  471. /* object definition */
  472. static const tsk_object_def_t mf_codec_h264_main_def_s = {
  473. sizeof(mf_codec_h264_t),
  474. mf_codec_h264_main_ctor,
  475. mf_codec_h264_main_dtor,
  476. tmedia_codec_cmp,
  477. };
  478. /* plugin definition*/
  479. static const tmedia_codec_plugin_def_t mf_codec_h264_main_plugin_def_s = {
  480. &mf_codec_h264_main_def_s,
  481. tmedia_video,
  482. tmedia_codec_id_h264_mp,
  483. "H264",
  484. "H264 Main Profile (Media Foundation)",
  485. TMEDIA_CODEC_FORMAT_H264_MP,
  486. tsk_true,
  487. 90000, // rate
  488. /* audio */
  489. { 0 },
  490. /* video (width, height, fps)*/
  491. {176, 144, 0},// fps is @deprecated
  492. mf_codec_h264_set,
  493. mf_codec_h264_open,
  494. mf_codec_h264_close,
  495. mf_codec_h264_encode,
  496. mf_codec_h264_decode,
  497. mf_codec_h264_sdp_att_match,
  498. mf_codec_h264_sdp_att_get
  499. };
  500. const tmedia_codec_plugin_def_t *mf_codec_h264_main_plugin_def_t = &mf_codec_h264_main_plugin_def_s;
  501. /* ============ Common To all H264 codecs ================= */
  502. int mf_codec_h264_open_encoder(mf_codec_h264_t* self)
  503. {
  504. HRESULT hr = S_OK;
  505. int32_t max_bw_kpbs;
  506. tdav_codec_h264_common_t* common = (tdav_codec_h264_common_t*)self;
  507. if(self->encoder.pInst) {
  508. TSK_DEBUG_ERROR("Encoder already initialized");
  509. #if defined(E_ILLEGAL_METHOD_CALL)
  510. CHECK_HR(hr = E_ILLEGAL_METHOD_CALL);
  511. #else
  512. CHECK_HR(hr = 0x8000000EL);
  513. #endif
  514. }
  515. // create encoder
  516. if(!(self->encoder.pInst = (common->profile == profile_idc_baseline) ? MFCodecVideoH264::CreateCodecH264Base(MFCodecType_Encoder) : MFCodecVideoH264::CreateCodecH264Main(MFCodecType_Encoder))) {
  517. TSK_DEBUG_ERROR("Failed to find H.264 encoder");
  518. CHECK_HR(hr = E_OUTOFMEMORY);
  519. }
  520. //self->encoder.context->pix_fmt = PIX_FMT_YUV420P;
  521. //self->encoder.context->time_base.num = 1;
  522. //self->encoder.context->time_base.den = TMEDIA_CODEC_VIDEO(self)->out.fps;
  523. self->encoder.neg_width = (self->encoder.rotation == 90 || self->encoder.rotation == 270) ? TMEDIA_CODEC_VIDEO(self)->out.height : TMEDIA_CODEC_VIDEO(self)->out.width;
  524. self->encoder.neg_height = (self->encoder.rotation == 90 || self->encoder.rotation == 270) ? TMEDIA_CODEC_VIDEO(self)->out.width : TMEDIA_CODEC_VIDEO(self)->out.height;
  525. self->encoder.neg_fps = TMEDIA_CODEC_VIDEO(self)->out.fps;
  526. max_bw_kpbs = TSK_CLAMP(
  527. 0,
  528. tmedia_get_video_bandwidth_kbps_2(self->encoder.neg_width, self->encoder.neg_height, self->encoder.neg_fps),
  529. self->encoder.max_bw_kpbs
  530. );
  531. self->encoder.max_bitrate_bps = (max_bw_kpbs * 1024);
  532. TSK_DEBUG_INFO("[H.264 MF Encoder] neg_width=%d, neg_height=%d, neg_fps=%d, max_bitrate_bps=%d",
  533. self->encoder.neg_width,
  534. self->encoder.neg_height,
  535. self->encoder.neg_fps,
  536. self->encoder.max_bitrate_bps
  537. );
  538. CHECK_HR(hr = self->encoder.pInst->Initialize(
  539. self->encoder.neg_fps,
  540. self->encoder.neg_width,
  541. self->encoder.neg_height,
  542. self->encoder.max_bitrate_bps));
  543. CHECK_HR(hr = self->encoder.pInst->SetGOPSize(self->encoder.neg_fps * PLUGIN_MF_H264_GOP_SIZE_IN_SECONDS));
  544. CHECK_HR(hr = self->encoder.pInst->SetSliceMaxSizeInBytes((H264_RTP_PAYLOAD_SIZE - 100)));
  545. bail:
  546. return SUCCEEDED(hr) ? 0 : -1;
  547. }
  548. int mf_codec_h264_close_encoder(mf_codec_h264_t* self)
  549. {
  550. if(self) {
  551. SafeRelease(&self->encoder.pInst);
  552. if(self->encoder.buffer) {
  553. TSK_FREE(self->encoder.buffer);
  554. }
  555. self->encoder.frame_count = 0;
  556. }
  557. return 0;
  558. }
  559. int mf_codec_h264_open_decoder(mf_codec_h264_t* self)
  560. {
  561. HRESULT hr = S_OK;
  562. tdav_codec_h264_common_t* common = (tdav_codec_h264_common_t*)self;
  563. if(self->decoder.pInst) {
  564. TSK_DEBUG_ERROR("Decoder already initialized");
  565. #if defined(E_ILLEGAL_METHOD_CALL)
  566. CHECK_HR(hr = E_ILLEGAL_METHOD_CALL);
  567. #else
  568. CHECK_HR(hr = 0x8000000EL);
  569. #endif
  570. }
  571. // create decoder
  572. if(!(self->decoder.pInst = (common->profile == profile_idc_baseline) ? MFCodecVideoH264::CreateCodecH264Base(MFCodecType_Decoder) : MFCodecVideoH264::CreateCodecH264Main(MFCodecType_Decoder))) {
  573. TSK_DEBUG_ERROR("Failed to find H.264 encoder");
  574. CHECK_HR(hr = E_OUTOFMEMORY);
  575. }
  576. TSK_DEBUG_INFO("[H.264 MF Decoder] neg_width=%d, neg_height=%d, neg_fps=%d",
  577. TMEDIA_CODEC_VIDEO(self)->in.width,
  578. TMEDIA_CODEC_VIDEO(self)->in.height,
  579. TMEDIA_CODEC_VIDEO(self)->in.fps
  580. );
  581. CHECK_HR(hr = self->decoder.pInst->Initialize(
  582. TMEDIA_CODEC_VIDEO(self)->in.fps,
  583. TMEDIA_CODEC_VIDEO(self)->in.width,
  584. TMEDIA_CODEC_VIDEO(self)->in.height));
  585. bail:
  586. return SUCCEEDED(hr) ? 0 : -1;
  587. }
  588. int mf_codec_h264_close_decoder(mf_codec_h264_t* self)
  589. {
  590. if(self) {
  591. SafeRelease(&self->decoder.pInst);
  592. TSK_FREE(self->decoder.accumulator);
  593. self->decoder.accumulator_pos = 0;
  594. }
  595. return 0;
  596. }
  597. int mf_codec_h264_init(mf_codec_h264_t* self, profile_idc_t profile)
  598. {
  599. int ret = 0;
  600. level_idc_t level;
  601. tdav_codec_h264_common_t* common = (tdav_codec_h264_common_t*)self;
  602. if(!self) {
  603. TSK_DEBUG_ERROR("Invalid parameter");
  604. return -1;
  605. }
  606. if((ret = tdav_codec_h264_common_init(common))) {
  607. TSK_DEBUG_ERROR("mf_codec_h264_common_init() faile with error code=%d", ret);
  608. return ret;
  609. }
  610. if((ret = tdav_codec_h264_common_level_from_size(TMEDIA_CODEC_VIDEO(self)->out.width, TMEDIA_CODEC_VIDEO(self)->out.height, &level))) {
  611. TSK_DEBUG_ERROR("Failed to find level for size=[%u, %u]", TMEDIA_CODEC_VIDEO(self)->out.width, TMEDIA_CODEC_VIDEO(self)->out.height);
  612. return ret;
  613. }
  614. (self)->encoder.max_bw_kpbs = tmedia_defaults_get_bandwidth_video_upload_max();
  615. if (MFUtils::IsLowLatencyH264SupportsMaxSliceSize()) {
  616. common->pack_mode_local = H264_PACKETIZATION_MODE;
  617. }
  618. else {
  619. common->pack_mode_local = Non_Interleaved_Mode;
  620. }
  621. common->profile = profile;
  622. common->level = level;
  623. TMEDIA_CODEC_VIDEO(self)->in.max_mbps = TMEDIA_CODEC_VIDEO(self)->out.max_mbps = H264_MAX_MBPS*1000;
  624. TMEDIA_CODEC_VIDEO(self)->in.max_br = TMEDIA_CODEC_VIDEO(self)->out.max_br = H264_MAX_BR*1000;
  625. TMEDIA_CODEC_VIDEO(self)->in.chroma = tmedia_chroma_nv12;
  626. TMEDIA_CODEC_VIDEO(self)->out.chroma = tmedia_chroma_nv12;
  627. self->encoder.quality = 1;
  628. return ret;
  629. }
  630. int mf_codec_h264_deinit(mf_codec_h264_t* self)
  631. {
  632. if(!self) {
  633. TSK_DEBUG_ERROR("Invalid parameter");
  634. return -1;
  635. }
  636. mf_codec_h264_close((tmedia_codec_t*)self);
  637. return 0;
  638. }