You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1543 lines
48KB

  1. /*
  2. * copyright (c) 2015 Rick Kern <kernrj@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <VideoToolbox/VideoToolbox.h>
  21. #include <CoreVideo/CoreVideo.h>
  22. #include <CoreMedia/CoreMedia.h>
  23. #include <TargetConditionals.h>
  24. #include <Availability.h>
  25. #include "avcodec.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/atomic.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavcodec/avcodec.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "internal.h"
  33. #include <pthread.h>
  34. typedef enum VT_H264Profile {
  35. H264_PROF_AUTO,
  36. H264_PROF_BASELINE,
  37. H264_PROF_MAIN,
  38. H264_PROF_HIGH,
  39. H264_PROF_COUNT
  40. } VT_H264Profile;
  41. typedef enum VTH264Entropy{
  42. VT_ENTROPY_NOT_SET,
  43. VT_CAVLC,
  44. VT_CABAC
  45. } VTH264Entropy;
  46. static const uint8_t start_code[] = { 0, 0, 0, 1 };
  47. typedef struct BufNode {
  48. CMSampleBufferRef cm_buffer;
  49. struct BufNode* next;
  50. int error;
  51. } BufNode;
  52. typedef struct VTEncContext {
  53. AVClass *class;
  54. VTCompressionSessionRef session;
  55. pthread_mutex_t lock;
  56. pthread_cond_t cv_sample_sent;
  57. int async_error;
  58. BufNode *q_head;
  59. BufNode *q_tail;
  60. int64_t frame_ct_out;
  61. int64_t frame_ct_in;
  62. int64_t first_pts;
  63. int64_t dts_delta;
  64. int64_t profile;
  65. int64_t level;
  66. int64_t entropy;
  67. int64_t allow_sw;
  68. bool flushing;
  69. bool has_b_frames;
  70. bool warned_color_range;
  71. } VTEncContext;
  72. /**
  73. * NULL-safe release of *refPtr, and sets value to NULL.
  74. */
  75. static void vt_release_num(CFNumberRef* refPtr){
  76. if (!*refPtr) {
  77. return;
  78. }
  79. CFRelease(*refPtr);
  80. *refPtr = NULL;
  81. }
  82. static void set_async_error(VTEncContext *vtctx, int err)
  83. {
  84. BufNode *info;
  85. pthread_mutex_lock(&vtctx->lock);
  86. vtctx->async_error = err;
  87. info = vtctx->q_head;
  88. vtctx->q_head = vtctx->q_tail = NULL;
  89. while (info) {
  90. BufNode *next = info->next;
  91. CFRelease(info->cm_buffer);
  92. av_free(info);
  93. info = next;
  94. }
  95. pthread_mutex_unlock(&vtctx->lock);
  96. }
  97. static int vtenc_q_pop(VTEncContext *vtctx, bool wait, CMSampleBufferRef *buf)
  98. {
  99. BufNode *info;
  100. pthread_mutex_lock(&vtctx->lock);
  101. if (vtctx->async_error) {
  102. pthread_mutex_unlock(&vtctx->lock);
  103. return vtctx->async_error;
  104. }
  105. if (vtctx->flushing && vtctx->frame_ct_in == vtctx->frame_ct_out) {
  106. *buf = NULL;
  107. pthread_mutex_unlock(&vtctx->lock);
  108. return 0;
  109. }
  110. while (!vtctx->q_head && !vtctx->async_error && wait) {
  111. pthread_cond_wait(&vtctx->cv_sample_sent, &vtctx->lock);
  112. }
  113. if (!vtctx->q_head) {
  114. pthread_mutex_unlock(&vtctx->lock);
  115. *buf = NULL;
  116. return 0;
  117. }
  118. info = vtctx->q_head;
  119. vtctx->q_head = vtctx->q_head->next;
  120. if (!vtctx->q_head) {
  121. vtctx->q_tail = NULL;
  122. }
  123. pthread_mutex_unlock(&vtctx->lock);
  124. *buf = info->cm_buffer;
  125. av_free(info);
  126. vtctx->frame_ct_out++;
  127. return 0;
  128. }
  129. static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer)
  130. {
  131. BufNode *info = av_malloc(sizeof(BufNode));
  132. if (!info) {
  133. set_async_error(vtctx, AVERROR(ENOMEM));
  134. return;
  135. }
  136. CFRetain(buffer);
  137. info->cm_buffer = buffer;
  138. info->next = NULL;
  139. pthread_mutex_lock(&vtctx->lock);
  140. pthread_cond_signal(&vtctx->cv_sample_sent);
  141. if (!vtctx->q_head) {
  142. vtctx->q_head = info;
  143. } else {
  144. vtctx->q_tail->next = info;
  145. }
  146. vtctx->q_tail = info;
  147. pthread_mutex_unlock(&vtctx->lock);
  148. }
  149. static CMVideoCodecType get_cm_codec_type(enum AVCodecID id)
  150. {
  151. switch (id) {
  152. case AV_CODEC_ID_H264: return kCMVideoCodecType_H264;
  153. default: return 0;
  154. }
  155. }
  156. static void vtenc_free_block(void *opaque, uint8_t *data)
  157. {
  158. CMBlockBufferRef block = opaque;
  159. CFRelease(block);
  160. }
  161. /**
  162. * Get the parameter sets from a CMSampleBufferRef.
  163. * @param dst If *dst isn't NULL, the parameters are copied into existing
  164. * memory. *dst_size must be set accordingly when *dst != NULL.
  165. * If *dst is NULL, it will be allocated.
  166. * In all cases, *dst_size is set to the number of bytes used starting
  167. * at *dst.
  168. */
  169. static int get_params_size(
  170. AVCodecContext *avctx,
  171. CMVideoFormatDescriptionRef vid_fmt,
  172. size_t *size)
  173. {
  174. size_t total_size = 0;
  175. size_t ps_count;
  176. int is_count_bad = 0;
  177. size_t i;
  178. int status;
  179. status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
  180. 0,
  181. NULL,
  182. NULL,
  183. &ps_count,
  184. NULL);
  185. if (status) {
  186. is_count_bad = 1;
  187. ps_count = 0;
  188. status = 0;
  189. }
  190. for (i = 0; i < ps_count || is_count_bad; i++) {
  191. const uint8_t *ps;
  192. size_t ps_size;
  193. status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
  194. i,
  195. &ps,
  196. &ps_size,
  197. NULL,
  198. NULL);
  199. if (status) {
  200. /*
  201. * When ps_count is invalid, status != 0 ends the loop normally
  202. * unless we didn't get any parameter sets.
  203. */
  204. if (i > 0 && is_count_bad) status = 0;
  205. break;
  206. }
  207. total_size += ps_size + sizeof(start_code);
  208. }
  209. if (status) {
  210. av_log(avctx, AV_LOG_ERROR, "Error getting parameter set sizes: %d\n", status);
  211. return AVERROR_EXTERNAL;
  212. }
  213. *size = total_size;
  214. return 0;
  215. }
  216. static int copy_param_sets(
  217. AVCodecContext *avctx,
  218. CMVideoFormatDescriptionRef vid_fmt,
  219. uint8_t *dst,
  220. size_t dst_size)
  221. {
  222. size_t ps_count;
  223. int is_count_bad = 0;
  224. int status;
  225. size_t offset = 0;
  226. size_t i;
  227. status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
  228. 0,
  229. NULL,
  230. NULL,
  231. &ps_count,
  232. NULL);
  233. if (status) {
  234. is_count_bad = 1;
  235. ps_count = 0;
  236. status = 0;
  237. }
  238. for (i = 0; i < ps_count || is_count_bad; i++) {
  239. const uint8_t *ps;
  240. size_t ps_size;
  241. size_t next_offset;
  242. status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
  243. i,
  244. &ps,
  245. &ps_size,
  246. NULL,
  247. NULL);
  248. if (status) {
  249. if (i > 0 && is_count_bad) status = 0;
  250. break;
  251. }
  252. next_offset = offset + sizeof(start_code) + ps_size;
  253. if (dst_size < next_offset) {
  254. av_log(avctx, AV_LOG_ERROR, "Error: buffer too small for parameter sets.\n");
  255. return AVERROR_BUFFER_TOO_SMALL;
  256. }
  257. memcpy(dst + offset, start_code, sizeof(start_code));
  258. offset += sizeof(start_code);
  259. memcpy(dst + offset, ps, ps_size);
  260. offset = next_offset;
  261. }
  262. if (status) {
  263. av_log(avctx, AV_LOG_ERROR, "Error getting parameter set data: %d\n", status);
  264. return AVERROR_EXTERNAL;
  265. }
  266. return 0;
  267. }
  268. static int set_extradata(AVCodecContext *avctx, CMSampleBufferRef sample_buffer)
  269. {
  270. CMVideoFormatDescriptionRef vid_fmt;
  271. size_t total_size;
  272. int status;
  273. vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
  274. if (!vid_fmt) {
  275. av_log(avctx, AV_LOG_ERROR, "No video format.\n");
  276. return AVERROR_EXTERNAL;
  277. }
  278. status = get_params_size(avctx, vid_fmt, &total_size);
  279. if (status) {
  280. av_log(avctx, AV_LOG_ERROR, "Could not get parameter sets.\n");
  281. return status;
  282. }
  283. avctx->extradata = av_malloc(total_size);
  284. if (!avctx->extradata) {
  285. return AVERROR(ENOMEM);
  286. }
  287. avctx->extradata_size = total_size;
  288. status = copy_param_sets(avctx, vid_fmt, avctx->extradata, total_size);
  289. if (status) {
  290. av_log(avctx, AV_LOG_ERROR, "Could not copy param sets.\n");
  291. return status;
  292. }
  293. return 0;
  294. }
  295. static void vtenc_output_callback(
  296. void *ctx,
  297. void *sourceFrameCtx,
  298. OSStatus status,
  299. VTEncodeInfoFlags flags,
  300. CMSampleBufferRef sample_buffer)
  301. {
  302. AVCodecContext *avctx = ctx;
  303. VTEncContext *vtctx = avctx->priv_data;
  304. if (vtctx->async_error) {
  305. if(sample_buffer) CFRelease(sample_buffer);
  306. return;
  307. }
  308. if (status || !sample_buffer) {
  309. av_log(avctx, AV_LOG_ERROR, "Error encoding frame: %d\n", (int)status);
  310. set_async_error(vtctx, AVERROR_EXTERNAL);
  311. return;
  312. }
  313. if (!avctx->extradata && (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
  314. int set_status = set_extradata(avctx, sample_buffer);
  315. if (set_status) {
  316. set_async_error(vtctx, set_status);
  317. return;
  318. }
  319. }
  320. vtenc_q_push(vtctx, sample_buffer);
  321. }
  322. static int get_length_code_size(
  323. AVCodecContext *avctx,
  324. CMSampleBufferRef sample_buffer,
  325. size_t *size)
  326. {
  327. CMVideoFormatDescriptionRef vid_fmt;
  328. int isize;
  329. int status;
  330. vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
  331. if (!vid_fmt) {
  332. av_log(avctx, AV_LOG_ERROR, "Error getting buffer format description.\n");
  333. return AVERROR_EXTERNAL;
  334. }
  335. status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
  336. 0,
  337. NULL,
  338. NULL,
  339. NULL,
  340. &isize);
  341. if (status) {
  342. av_log(avctx, AV_LOG_ERROR, "Error getting length code size: %d\n", status);
  343. return AVERROR_EXTERNAL;
  344. }
  345. *size = isize;
  346. return 0;
  347. }
  348. /*
  349. * Returns true on success.
  350. *
  351. * If profile_level_val is NULL and this method returns true, don't specify the
  352. * profile/level to the encoder.
  353. */
  354. static bool get_vt_profile_level(AVCodecContext *avctx,
  355. CFStringRef *profile_level_val)
  356. {
  357. VTEncContext *vtctx = avctx->priv_data;
  358. int64_t profile = vtctx->profile;
  359. if (profile == H264_PROF_AUTO && vtctx->level) {
  360. //Need to pick a profile if level is not auto-selected.
  361. profile = vtctx->has_b_frames ? H264_PROF_MAIN : H264_PROF_BASELINE;
  362. }
  363. *profile_level_val = NULL;
  364. switch (profile) {
  365. case H264_PROF_AUTO:
  366. return true;
  367. case H264_PROF_BASELINE:
  368. switch (vtctx->level) {
  369. case 0: *profile_level_val = kVTProfileLevel_H264_Baseline_AutoLevel; break;
  370. case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3; break;
  371. case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0; break;
  372. case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1; break;
  373. case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2; break;
  374. case 40: *profile_level_val = kVTProfileLevel_H264_Baseline_4_0; break;
  375. case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1; break;
  376. case 42: *profile_level_val = kVTProfileLevel_H264_Baseline_4_2; break;
  377. case 50: *profile_level_val = kVTProfileLevel_H264_Baseline_5_0; break;
  378. case 51: *profile_level_val = kVTProfileLevel_H264_Baseline_5_1; break;
  379. case 52: *profile_level_val = kVTProfileLevel_H264_Baseline_5_2; break;
  380. }
  381. break;
  382. case H264_PROF_MAIN:
  383. switch (vtctx->level) {
  384. case 0: *profile_level_val = kVTProfileLevel_H264_Main_AutoLevel; break;
  385. case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0; break;
  386. case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1; break;
  387. case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2; break;
  388. case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0; break;
  389. case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1; break;
  390. case 42: *profile_level_val = kVTProfileLevel_H264_Main_4_2; break;
  391. case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0; break;
  392. case 51: *profile_level_val = kVTProfileLevel_H264_Main_5_1; break;
  393. case 52: *profile_level_val = kVTProfileLevel_H264_Main_5_2; break;
  394. }
  395. break;
  396. case H264_PROF_HIGH:
  397. switch (vtctx->level) {
  398. case 0: *profile_level_val = kVTProfileLevel_H264_High_AutoLevel; break;
  399. case 30: *profile_level_val = kVTProfileLevel_H264_High_3_0; break;
  400. case 31: *profile_level_val = kVTProfileLevel_H264_High_3_1; break;
  401. case 32: *profile_level_val = kVTProfileLevel_H264_High_3_2; break;
  402. case 40: *profile_level_val = kVTProfileLevel_H264_High_4_0; break;
  403. case 41: *profile_level_val = kVTProfileLevel_H264_High_4_1; break;
  404. case 42: *profile_level_val = kVTProfileLevel_H264_High_4_2; break;
  405. case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0; break;
  406. case 51: *profile_level_val = kVTProfileLevel_H264_High_5_1; break;
  407. case 52: *profile_level_val = kVTProfileLevel_H264_High_5_2; break;
  408. }
  409. break;
  410. }
  411. if (!*profile_level_val) {
  412. av_log(avctx, AV_LOG_ERROR, "Invalid Profile/Level.\n");
  413. return false;
  414. }
  415. return true;
  416. }
  417. static int get_cv_pixel_format(AVCodecContext* avctx,
  418. enum AVPixelFormat fmt,
  419. enum AVColorRange range,
  420. int* av_pixel_format,
  421. int* range_guessed)
  422. {
  423. if (range_guessed) *range_guessed = range != AVCOL_RANGE_MPEG &&
  424. range != AVCOL_RANGE_JPEG;
  425. //MPEG range is used when no range is set
  426. if (fmt == AV_PIX_FMT_NV12) {
  427. *av_pixel_format = range == AVCOL_RANGE_JPEG ?
  428. kCVPixelFormatType_420YpCbCr8BiPlanarFullRange :
  429. kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
  430. } else if (fmt == AV_PIX_FMT_YUV420P) {
  431. *av_pixel_format = range == AVCOL_RANGE_JPEG ?
  432. kCVPixelFormatType_420YpCbCr8PlanarFullRange :
  433. kCVPixelFormatType_420YpCbCr8Planar;
  434. } else {
  435. return AVERROR(EINVAL);
  436. }
  437. return 0;
  438. }
  439. static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
  440. CFMutableDictionaryRef* dict)
  441. {
  442. CFNumberRef cv_color_format_num = NULL;
  443. CFNumberRef width_num = NULL;
  444. CFNumberRef height_num = NULL;
  445. CFMutableDictionaryRef pixel_buffer_info = NULL;
  446. int cv_color_format;
  447. int status = get_cv_pixel_format(avctx,
  448. avctx->pix_fmt,
  449. avctx->color_range,
  450. &cv_color_format,
  451. NULL);
  452. if (status) return status;
  453. pixel_buffer_info = CFDictionaryCreateMutable(
  454. kCFAllocatorDefault,
  455. 20,
  456. &kCFCopyStringDictionaryKeyCallBacks,
  457. &kCFTypeDictionaryValueCallBacks);
  458. if (!pixel_buffer_info) goto pbinfo_nomem;
  459. cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
  460. kCFNumberSInt32Type,
  461. &cv_color_format);
  462. if (!cv_color_format_num) goto pbinfo_nomem;
  463. CFDictionarySetValue(pixel_buffer_info,
  464. kCVPixelBufferPixelFormatTypeKey,
  465. cv_color_format_num);
  466. vt_release_num(&cv_color_format_num);
  467. width_num = CFNumberCreate(kCFAllocatorDefault,
  468. kCFNumberSInt32Type,
  469. &avctx->width);
  470. if (!width_num) return AVERROR(ENOMEM);
  471. CFDictionarySetValue(pixel_buffer_info,
  472. kCVPixelBufferWidthKey,
  473. width_num);
  474. vt_release_num(&width_num);
  475. height_num = CFNumberCreate(kCFAllocatorDefault,
  476. kCFNumberSInt32Type,
  477. &avctx->height);
  478. if (!height_num) goto pbinfo_nomem;
  479. CFDictionarySetValue(pixel_buffer_info,
  480. kCVPixelBufferHeightKey,
  481. height_num);
  482. vt_release_num(&height_num);
  483. *dict = pixel_buffer_info;
  484. return 0;
  485. pbinfo_nomem:
  486. vt_release_num(&cv_color_format_num);
  487. vt_release_num(&width_num);
  488. vt_release_num(&height_num);
  489. if (pixel_buffer_info) CFRelease(pixel_buffer_info);
  490. return AVERROR(ENOMEM);
  491. }
  492. static av_cold int vtenc_init(AVCodecContext *avctx)
  493. {
  494. CFMutableDictionaryRef enc_info;
  495. CFMutableDictionaryRef pixel_buffer_info;
  496. CMVideoCodecType codec_type;
  497. VTEncContext *vtctx = avctx->priv_data;
  498. CFStringRef profile_level;
  499. SInt32 bit_rate = avctx->bit_rate;
  500. CFNumberRef bit_rate_num;
  501. CFBooleanRef has_b_frames_cfbool;
  502. int status;
  503. codec_type = get_cm_codec_type(avctx->codec_id);
  504. if (!codec_type) {
  505. av_log(avctx, AV_LOG_ERROR, "Error: no mapping for AVCodecID %d\n", avctx->codec_id);
  506. return AVERROR(EINVAL);
  507. }
  508. vtctx->has_b_frames = avctx->max_b_frames > 0;
  509. if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
  510. av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
  511. vtctx->has_b_frames = false;
  512. }
  513. if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) {
  514. av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
  515. vtctx->entropy = VT_ENTROPY_NOT_SET;
  516. }
  517. if (!get_vt_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
  518. vtctx->session = NULL;
  519. enc_info = CFDictionaryCreateMutable(
  520. kCFAllocatorDefault,
  521. 20,
  522. &kCFCopyStringDictionaryKeyCallBacks,
  523. &kCFTypeDictionaryValueCallBacks
  524. );
  525. if (!enc_info) return AVERROR(ENOMEM);
  526. #if !TARGET_OS_IPHONE
  527. if (!vtctx->allow_sw) {
  528. CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, kCFBooleanTrue);
  529. } else {
  530. CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, kCFBooleanTrue);
  531. }
  532. #endif
  533. if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
  534. status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
  535. if (status) {
  536. CFRelease(enc_info);
  537. return status;
  538. }
  539. } else {
  540. pixel_buffer_info = NULL;
  541. }
  542. status = VTCompressionSessionCreate(
  543. kCFAllocatorDefault,
  544. avctx->width,
  545. avctx->height,
  546. codec_type,
  547. enc_info,
  548. pixel_buffer_info,
  549. kCFAllocatorDefault,
  550. vtenc_output_callback,
  551. avctx,
  552. &vtctx->session
  553. );
  554. if (pixel_buffer_info) CFRelease(pixel_buffer_info);
  555. CFRelease(enc_info);
  556. if (status || !vtctx->session) {
  557. av_log(avctx, AV_LOG_ERROR, "Error: cannot create compression session: %d\n", status);
  558. #if !TARGET_OS_IPHONE
  559. if (!vtctx->allow_sw) {
  560. av_log(avctx, AV_LOG_ERROR, "Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
  561. }
  562. #endif
  563. return AVERROR_EXTERNAL;
  564. }
  565. bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
  566. kCFNumberSInt32Type,
  567. &bit_rate);
  568. if (!bit_rate_num) return AVERROR(ENOMEM);
  569. status = VTSessionSetProperty(vtctx->session,
  570. kVTCompressionPropertyKey_AverageBitRate,
  571. bit_rate_num);
  572. CFRelease(bit_rate_num);
  573. if (status) {
  574. av_log(avctx, AV_LOG_ERROR, "Error setting bitrate property: %d\n", status);
  575. return AVERROR_EXTERNAL;
  576. }
  577. if (profile_level) {
  578. status = VTSessionSetProperty(vtctx->session,
  579. kVTCompressionPropertyKey_ProfileLevel,
  580. profile_level);
  581. if (status) {
  582. av_log(avctx, AV_LOG_ERROR, "Error setting profile/level property: %d\n", status);
  583. return AVERROR_EXTERNAL;
  584. }
  585. }
  586. if (avctx->gop_size > 0) {
  587. CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
  588. kCFNumberIntType,
  589. &avctx->gop_size);
  590. status = VTSessionSetProperty(vtctx->session,
  591. kVTCompressionPropertyKey_MaxKeyFrameInterval,
  592. interval);
  593. if (status) {
  594. av_log(avctx, AV_LOG_ERROR, "Error setting 'max key-frame interval' property: %d\n", status);
  595. return AVERROR_EXTERNAL;
  596. }
  597. }
  598. if (!vtctx->has_b_frames) {
  599. status = VTSessionSetProperty(vtctx->session,
  600. kVTCompressionPropertyKey_AllowFrameReordering,
  601. kCFBooleanFalse);
  602. if (status) {
  603. av_log(avctx, AV_LOG_ERROR, "Error setting 'allow frame reordering' property: %d\n", status);
  604. return AVERROR_EXTERNAL;
  605. }
  606. }
  607. if (vtctx->entropy != VT_ENTROPY_NOT_SET) {
  608. CFStringRef entropy = vtctx->entropy == VT_CABAC ?
  609. kVTH264EntropyMode_CABAC:
  610. kVTH264EntropyMode_CAVLC;
  611. status = VTSessionSetProperty(vtctx->session,
  612. kVTCompressionPropertyKey_H264EntropyMode,
  613. entropy);
  614. if (status) {
  615. av_log(avctx, AV_LOG_ERROR, "Error setting entropy property: %d\n", status);
  616. return AVERROR_EXTERNAL;
  617. }
  618. }
  619. status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
  620. if (status) {
  621. av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status);
  622. return AVERROR_EXTERNAL;
  623. }
  624. pthread_mutex_init(&vtctx->lock, NULL);
  625. pthread_cond_init(&vtctx->cv_sample_sent, NULL);
  626. vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
  627. status = VTSessionCopyProperty(vtctx->session,
  628. kVTCompressionPropertyKey_AllowFrameReordering,
  629. kCFAllocatorDefault,
  630. &has_b_frames_cfbool);
  631. if (!status) {
  632. //Some devices don't output B-frames for main profile, even if requested.
  633. vtctx->has_b_frames = CFBooleanGetValue(has_b_frames_cfbool);
  634. CFRelease(has_b_frames_cfbool);
  635. }
  636. avctx->has_b_frames = vtctx->has_b_frames;
  637. return 0;
  638. }
  639. static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame)
  640. {
  641. CFArrayRef attachments;
  642. CFDictionaryRef attachment;
  643. CFBooleanRef not_sync;
  644. CFIndex len;
  645. attachments = CMSampleBufferGetSampleAttachmentsArray(buffer, false);
  646. len = !attachments ? 0 : CFArrayGetCount(attachments);
  647. if (!len) {
  648. *is_key_frame = true;
  649. return;
  650. }
  651. attachment = CFArrayGetValueAtIndex(attachments, 0);
  652. if (CFDictionaryGetValueIfPresent(attachment,
  653. kCMSampleAttachmentKey_NotSync,
  654. (const void **)&not_sync))
  655. {
  656. *is_key_frame = !CFBooleanGetValue(not_sync);
  657. } else {
  658. *is_key_frame = true;
  659. }
  660. }
  661. /**
  662. * Replaces length codes with H.264 Annex B start codes.
  663. * length_code_size must equal sizeof(start_code).
  664. * On failure, the contents of data may have been modified.
  665. *
  666. * @param length_code_size Byte length of each length code
  667. * @param data Call with NAL units prefixed with length codes.
  668. * On success, the length codes are replace with
  669. * start codes.
  670. * @param size Length of data, excluding any padding.
  671. * @return 0 on success
  672. * AVERROR_BUFFER_TOO_SMALL if length code size is smaller
  673. * than a start code or if a length_code in data specifies
  674. * data beyond the end of its buffer.
  675. */
  676. static int replace_length_codes(size_t length_code_size,
  677. uint8_t *data,
  678. size_t size)
  679. {
  680. size_t remaining_size = size;
  681. if (length_code_size != sizeof(start_code)) {
  682. av_log(NULL, AV_LOG_ERROR, "Start code size and length code size not equal.\n");
  683. return AVERROR_BUFFER_TOO_SMALL;
  684. }
  685. while (remaining_size > 0) {
  686. size_t box_len = 0;
  687. size_t i;
  688. for (i = 0; i < length_code_size; i++) {
  689. box_len <<= 8;
  690. box_len |= data[i];
  691. }
  692. if (remaining_size < box_len + sizeof(start_code)) {
  693. av_log(NULL, AV_LOG_ERROR, "Length is out of range.\n");
  694. AVERROR_BUFFER_TOO_SMALL;
  695. }
  696. memcpy(data, start_code, sizeof(start_code));
  697. data += box_len + sizeof(start_code);
  698. remaining_size -= box_len + sizeof(start_code);
  699. }
  700. return 0;
  701. }
  702. /**
  703. * Copies NAL units and replaces length codes with
  704. * H.264 Annex B start codes. On failure, the contents of
  705. * dst_data may have been modified.
  706. *
  707. * @param length_code_size Byte length of each length code
  708. * @param src_data NAL units prefixed with length codes.
  709. * @param src_size Length of buffer, excluding any padding.
  710. * @param dst_data Must be zeroed before calling this function.
  711. * Contains the copied NAL units prefixed with
  712. * start codes when the function returns
  713. * successfully.
  714. * @param dst_size Length of dst_data
  715. * @return 0 on success
  716. * AVERROR_INVALIDDATA if length_code_size is invalid
  717. * AVERROR_BUFFER_TOO_SMALL if dst_data is too small
  718. * or if a length_code in src_data specifies data beyond
  719. * the end of its buffer.
  720. */
  721. static int copy_replace_length_codes(
  722. size_t length_code_size,
  723. const uint8_t *src_data,
  724. size_t src_size,
  725. uint8_t *dst_data,
  726. size_t dst_size)
  727. {
  728. size_t remaining_src_size = src_size;
  729. size_t remaining_dst_size = dst_size;
  730. if (length_code_size > 4) {
  731. return AVERROR_INVALIDDATA;
  732. }
  733. while (remaining_src_size > 0) {
  734. size_t curr_src_len;
  735. size_t curr_dst_len;
  736. size_t box_len = 0;
  737. size_t i;
  738. uint8_t *dst_box;
  739. const uint8_t *src_box;
  740. for (i = 0; i < length_code_size; i++) {
  741. box_len <<= 8;
  742. box_len |= src_data[i];
  743. }
  744. curr_src_len = box_len + length_code_size;
  745. curr_dst_len = box_len + sizeof(start_code);
  746. if (remaining_src_size < curr_src_len) {
  747. return AVERROR_BUFFER_TOO_SMALL;
  748. }
  749. if (remaining_dst_size < curr_dst_len) {
  750. return AVERROR_BUFFER_TOO_SMALL;
  751. }
  752. dst_box = dst_data + sizeof(start_code);
  753. src_box = src_data + length_code_size;
  754. memcpy(dst_data, start_code, sizeof(start_code));
  755. memcpy(dst_box, src_box, box_len);
  756. src_data += curr_src_len;
  757. dst_data += curr_dst_len;
  758. remaining_src_size -= curr_src_len;
  759. remaining_dst_size -= curr_dst_len;
  760. }
  761. return 0;
  762. }
  763. static int vtenc_cm_to_avpacket(
  764. AVCodecContext *avctx,
  765. CMSampleBufferRef sample_buffer,
  766. AVPacket *pkt)
  767. {
  768. VTEncContext *vtctx = avctx->priv_data;
  769. int status;
  770. bool is_key_frame;
  771. bool add_header;
  772. char *buf_data;
  773. size_t length_code_size;
  774. size_t header_size = 0;
  775. size_t in_buf_size;
  776. int64_t dts_delta;
  777. int64_t time_base_num;
  778. CMTime pts;
  779. CMTime dts;
  780. CMBlockBufferRef block;
  781. CMVideoFormatDescriptionRef vid_fmt;
  782. vtenc_get_frame_info(sample_buffer, &is_key_frame);
  783. status = get_length_code_size(avctx, sample_buffer, &length_code_size);
  784. if (status) return status;
  785. add_header = is_key_frame && !(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER);
  786. if (add_header) {
  787. vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
  788. if (!vid_fmt) {
  789. av_log(avctx, AV_LOG_ERROR, "Cannot get format description.\n");
  790. }
  791. int status = get_params_size(avctx, vid_fmt, &header_size);
  792. if (status) return status;
  793. }
  794. block = CMSampleBufferGetDataBuffer(sample_buffer);
  795. if (!block) {
  796. av_log(avctx, AV_LOG_ERROR, "Could not get block buffer from sample buffer.\n");
  797. return AVERROR_EXTERNAL;
  798. }
  799. status = CMBlockBufferGetDataPointer(block, 0, &in_buf_size, NULL, &buf_data);
  800. if (status) {
  801. av_log(avctx, AV_LOG_ERROR, "Error: cannot get data pointer: %d\n", status);
  802. return AVERROR_EXTERNAL;
  803. }
  804. size_t out_buf_size = header_size + in_buf_size;
  805. bool can_reuse_cmbuffer = !add_header &&
  806. !pkt->data &&
  807. length_code_size == sizeof(start_code);
  808. av_init_packet(pkt);
  809. if (can_reuse_cmbuffer) {
  810. AVBufferRef* buf_ref = av_buffer_create(
  811. buf_data,
  812. out_buf_size,
  813. vtenc_free_block,
  814. block,
  815. 0
  816. );
  817. if (!buf_ref) return AVERROR(ENOMEM);
  818. CFRetain(block);
  819. pkt->buf = buf_ref;
  820. pkt->data = buf_data;
  821. pkt->size = in_buf_size;
  822. status = replace_length_codes(length_code_size, pkt->data, pkt->size);
  823. if (status) {
  824. av_log(avctx, AV_LOG_ERROR, "Error replacing length codes: %d\n", status);
  825. return status;
  826. }
  827. } else {
  828. if (!pkt->data) {
  829. status = av_new_packet(pkt, out_buf_size);
  830. if(status) return status;
  831. }
  832. if (pkt->size < out_buf_size) {
  833. av_log(avctx, AV_LOG_ERROR, "Error: packet's buffer is too small.\n");
  834. return AVERROR_BUFFER_TOO_SMALL;
  835. }
  836. if (add_header) {
  837. status = copy_param_sets(avctx, vid_fmt, pkt->data, out_buf_size);
  838. if(status) return status;
  839. }
  840. status = copy_replace_length_codes(
  841. length_code_size,
  842. buf_data,
  843. in_buf_size,
  844. pkt->data + header_size,
  845. pkt->size - header_size
  846. );
  847. if (status) {
  848. av_log(avctx, AV_LOG_ERROR, "Error copying packet data: %d", status);
  849. return status;
  850. }
  851. }
  852. if (is_key_frame) {
  853. pkt->flags |= AV_PKT_FLAG_KEY;
  854. }
  855. pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
  856. dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
  857. if (CMTIME_IS_INVALID(dts)) {
  858. if (!vtctx->has_b_frames) {
  859. dts = pts;
  860. } else {
  861. av_log(avctx, AV_LOG_ERROR, "DTS is invalid.\n");
  862. return AVERROR_EXTERNAL;
  863. }
  864. }
  865. dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0;
  866. time_base_num = avctx->time_base.num;
  867. pkt->pts = pts.value / time_base_num;
  868. pkt->dts = dts.value / time_base_num - dts_delta;
  869. return 0;
  870. }
  871. /*
  872. * contiguous_buf_size is 0 if not contiguous, and the size of the buffer
  873. * containing all planes if so.
  874. */
  875. static int get_cv_pixel_info(
  876. AVCodecContext *avctx,
  877. const AVFrame *frame,
  878. int *color,
  879. int *plane_count,
  880. size_t *widths,
  881. size_t *heights,
  882. size_t *strides,
  883. size_t *contiguous_buf_size)
  884. {
  885. VTEncContext *vtctx = avctx->priv_data;
  886. int av_format = frame->format;
  887. int av_color_range = av_frame_get_color_range(frame);
  888. int i;
  889. int range_guessed;
  890. int status;
  891. status = get_cv_pixel_format(avctx, av_format, av_color_range, color, &range_guessed);
  892. if (status) {
  893. av_log(avctx,
  894. AV_LOG_ERROR,
  895. "Could not get pixel format for color format '%s' range '%s'.\n",
  896. av_get_pix_fmt_name(av_format),
  897. av_color_range > AVCOL_RANGE_UNSPECIFIED &&
  898. av_color_range < AVCOL_RANGE_NB ?
  899. av_color_range_name(av_color_range) :
  900. "Unknown");
  901. return AVERROR(EINVAL);
  902. }
  903. if (range_guessed) {
  904. if (!vtctx->warned_color_range) {
  905. vtctx->warned_color_range = true;
  906. av_log(avctx,
  907. AV_LOG_WARNING,
  908. "Color range not set for %s. Using MPEG range.\n",
  909. av_get_pix_fmt_name(av_format));
  910. }
  911. av_log(avctx, AV_LOG_WARNING, "");
  912. }
  913. switch (av_format) {
  914. case AV_PIX_FMT_NV12:
  915. *plane_count = 2;
  916. widths [0] = avctx->width;
  917. heights[0] = avctx->height;
  918. strides[0] = frame ? frame->linesize[0] : avctx->width;
  919. widths [1] = (avctx->width + 1) / 2;
  920. heights[1] = (avctx->height + 1) / 2;
  921. strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) & -2;
  922. break;
  923. case AV_PIX_FMT_YUV420P:
  924. *plane_count = 3;
  925. widths [0] = avctx->width;
  926. heights[0] = avctx->height;
  927. strides[0] = frame ? frame->linesize[0] : avctx->width;
  928. widths [1] = (avctx->width + 1) / 2;
  929. heights[1] = (avctx->height + 1) / 2;
  930. strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) / 2;
  931. widths [2] = (avctx->width + 1) / 2;
  932. heights[2] = (avctx->height + 1) / 2;
  933. strides[2] = frame ? frame->linesize[2] : (avctx->width + 1) / 2;
  934. break;
  935. default:
  936. av_log(
  937. avctx,
  938. AV_LOG_ERROR,
  939. "Could not get frame format info for color %d range %d.\n",
  940. av_format,
  941. av_color_range);
  942. return AVERROR(EINVAL);
  943. }
  944. *contiguous_buf_size = 0;
  945. for (i = 0; i < *plane_count; i++) {
  946. if (i < *plane_count - 1 &&
  947. frame->data[i] + strides[i] * heights[i] != frame->data[i + 1]) {
  948. *contiguous_buf_size = 0;
  949. break;
  950. }
  951. *contiguous_buf_size += strides[i] * heights[i];
  952. }
  953. return 0;
  954. }
  955. #if !TARGET_OS_IPHONE
  956. //Not used on iOS - frame is always copied.
  957. static void free_avframe(
  958. void *release_ctx,
  959. const void *data,
  960. size_t size,
  961. size_t plane_count,
  962. const void *plane_addresses[])
  963. {
  964. AVFrame *frame = release_ctx;
  965. av_frame_free(&frame);
  966. }
  967. #else
  968. //Not used on OSX - frame is never copied.
  969. static int copy_avframe_to_pixel_buffer(AVCodecContext *avctx,
  970. const AVFrame *frame,
  971. CVPixelBufferRef cv_img,
  972. const size_t *plane_strides,
  973. const size_t *plane_rows)
  974. {
  975. int i, j;
  976. size_t plane_count;
  977. int status;
  978. int rows;
  979. int src_stride;
  980. int dst_stride;
  981. uint8_t *src_addr;
  982. uint8_t *dst_addr;
  983. size_t copy_bytes;
  984. status = CVPixelBufferLockBaseAddress(cv_img, 0);
  985. if (status) {
  986. av_log(
  987. avctx,
  988. AV_LOG_ERROR,
  989. "Error: Could not lock base address of CVPixelBuffer: %d.\n",
  990. status
  991. );
  992. }
  993. if (CVPixelBufferIsPlanar(cv_img)) {
  994. plane_count = CVPixelBufferGetPlaneCount(cv_img);
  995. for (i = 0; frame->data[i]; i++) {
  996. if (i == plane_count) {
  997. CVPixelBufferUnlockBaseAddress(cv_img, 0);
  998. av_log(avctx,
  999. AV_LOG_ERROR,
  1000. "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
  1001. );
  1002. return AVERROR_EXTERNAL;
  1003. }
  1004. dst_addr = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img, i);
  1005. src_addr = (uint8_t*)frame->data[i];
  1006. dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img, i);
  1007. src_stride = plane_strides[i];
  1008. rows = plane_rows[i];
  1009. if (dst_stride == src_stride) {
  1010. memcpy(dst_addr, src_addr, src_stride * rows);
  1011. } else {
  1012. copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
  1013. for (j = 0; j < rows; j++) {
  1014. memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
  1015. }
  1016. }
  1017. }
  1018. } else {
  1019. if (frame->data[1]) {
  1020. CVPixelBufferUnlockBaseAddress(cv_img, 0);
  1021. av_log(avctx,
  1022. AV_LOG_ERROR,
  1023. "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
  1024. );
  1025. return AVERROR_EXTERNAL;
  1026. }
  1027. dst_addr = (uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
  1028. src_addr = (uint8_t*)frame->data[0];
  1029. dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
  1030. src_stride = plane_strides[0];
  1031. rows = plane_rows[0];
  1032. if (dst_stride == src_stride) {
  1033. memcpy(dst_addr, src_addr, src_stride * rows);
  1034. } else {
  1035. copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
  1036. for (j = 0; j < rows; j++) {
  1037. memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
  1038. }
  1039. }
  1040. }
  1041. status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
  1042. if (status) {
  1043. av_log(avctx, AV_LOG_ERROR, "Error: Could not unlock CVPixelBuffer base address: %d.\n", status);
  1044. return AVERROR_EXTERNAL;
  1045. }
  1046. return 0;
  1047. }
  1048. #endif //!TARGET_OS_IPHONE
  1049. static int create_cv_pixel_buffer(AVCodecContext *avctx,
  1050. const AVFrame *frame,
  1051. CVPixelBufferRef *cv_img)
  1052. {
  1053. int plane_count;
  1054. int color;
  1055. size_t widths [AV_NUM_DATA_POINTERS];
  1056. size_t heights[AV_NUM_DATA_POINTERS];
  1057. size_t strides[AV_NUM_DATA_POINTERS];
  1058. int status;
  1059. size_t contiguous_buf_size;
  1060. CVPixelBufferPoolRef pix_buf_pool;
  1061. VTEncContext* vtctx = avctx->priv_data;
  1062. if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX) {
  1063. av_assert0(frame->format == AV_PIX_FMT_VIDEOTOOLBOX);
  1064. *cv_img = (CVPixelBufferRef)frame->data[3];
  1065. av_assert0(*cv_img);
  1066. CFRetain(*cv_img);
  1067. return 0;
  1068. }
  1069. memset(widths, 0, sizeof(widths));
  1070. memset(heights, 0, sizeof(heights));
  1071. memset(strides, 0, sizeof(strides));
  1072. status = get_cv_pixel_info(
  1073. avctx,
  1074. frame,
  1075. &color,
  1076. &plane_count,
  1077. widths,
  1078. heights,
  1079. strides,
  1080. &contiguous_buf_size
  1081. );
  1082. if (status) {
  1083. av_log(
  1084. avctx,
  1085. AV_LOG_ERROR,
  1086. "Error: Cannot convert format %d color_range %d: %d\n",
  1087. frame->format,
  1088. av_frame_get_color_range(frame),
  1089. status
  1090. );
  1091. return AVERROR_EXTERNAL;
  1092. }
  1093. #if TARGET_OS_IPHONE
  1094. pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->session);
  1095. if (!pix_buf_pool) {
  1096. av_log(avctx, AV_LOG_ERROR, "Could not get pixel buffer pool.\n");
  1097. return AVERROR_EXTERNAL;
  1098. }
  1099. status = CVPixelBufferPoolCreatePixelBuffer(NULL,
  1100. pix_buf_pool,
  1101. cv_img);
  1102. if (status) {
  1103. av_log(avctx, AV_LOG_ERROR, "Could not create pixel buffer from pool: %d.\n", status);
  1104. return AVERROR_EXTERNAL;
  1105. }
  1106. status = copy_avframe_to_pixel_buffer(avctx, frame, *cv_img, strides, heights);
  1107. if (status) {
  1108. CFRelease(*cv_img);
  1109. *cv_img = NULL;
  1110. return status;
  1111. }
  1112. #else
  1113. AVFrame *enc_frame = av_frame_alloc();
  1114. if (!enc_frame) return AVERROR(ENOMEM);
  1115. status = av_frame_ref(enc_frame, frame);
  1116. if (status) {
  1117. av_frame_free(&enc_frame);
  1118. return status;
  1119. }
  1120. status = CVPixelBufferCreateWithPlanarBytes(
  1121. kCFAllocatorDefault,
  1122. enc_frame->width,
  1123. enc_frame->height,
  1124. color,
  1125. NULL,
  1126. contiguous_buf_size,
  1127. plane_count,
  1128. (void **)enc_frame->data,
  1129. widths,
  1130. heights,
  1131. strides,
  1132. free_avframe,
  1133. enc_frame,
  1134. NULL,
  1135. cv_img
  1136. );
  1137. if (status) {
  1138. av_log(avctx, AV_LOG_ERROR, "Error: Could not create CVPixelBuffer: %d\n", status);
  1139. return AVERROR_EXTERNAL;
  1140. }
  1141. #endif
  1142. return 0;
  1143. }
  1144. static int vtenc_send_frame(AVCodecContext *avctx,
  1145. VTEncContext *vtctx,
  1146. const AVFrame *frame)
  1147. {
  1148. CMTime time;
  1149. CVPixelBufferRef cv_img = NULL;
  1150. int status = create_cv_pixel_buffer(avctx, frame, &cv_img);
  1151. if (status) return status;
  1152. time = CMTimeMake(frame->pts * avctx->time_base.num, avctx->time_base.den);
  1153. status = VTCompressionSessionEncodeFrame(
  1154. vtctx->session,
  1155. cv_img,
  1156. time,
  1157. kCMTimeInvalid,
  1158. NULL,
  1159. NULL,
  1160. NULL
  1161. );
  1162. CFRelease(cv_img);
  1163. if (status) {
  1164. av_log(avctx, AV_LOG_ERROR, "Error: cannot encode frame: %d\n", status);
  1165. return AVERROR_EXTERNAL;
  1166. }
  1167. return 0;
  1168. }
  1169. static av_cold int vtenc_frame(
  1170. AVCodecContext *avctx,
  1171. AVPacket *pkt,
  1172. const AVFrame *frame,
  1173. int *got_packet)
  1174. {
  1175. VTEncContext *vtctx = avctx->priv_data;
  1176. bool get_frame;
  1177. int status;
  1178. CMSampleBufferRef buf = NULL;
  1179. if (frame) {
  1180. status = vtenc_send_frame(avctx, vtctx, frame);
  1181. if (status) {
  1182. status = AVERROR_EXTERNAL;
  1183. goto end_nopkt;
  1184. }
  1185. if (vtctx->frame_ct_in == 0) {
  1186. vtctx->first_pts = frame->pts;
  1187. } else if(vtctx->frame_ct_in == 1 && vtctx->has_b_frames) {
  1188. vtctx->dts_delta = frame->pts - vtctx->first_pts;
  1189. }
  1190. vtctx->frame_ct_in++;
  1191. } else if(!vtctx->flushing) {
  1192. vtctx->flushing = true;
  1193. status = VTCompressionSessionCompleteFrames(vtctx->session,
  1194. kCMTimeIndefinite);
  1195. if (status) {
  1196. av_log(avctx, AV_LOG_ERROR, "Error flushing frames: %d\n", status);
  1197. status = AVERROR_EXTERNAL;
  1198. goto end_nopkt;
  1199. }
  1200. }
  1201. *got_packet = 0;
  1202. get_frame = vtctx->dts_delta >= 0 || !frame;
  1203. if (!get_frame) {
  1204. status = 0;
  1205. goto end_nopkt;
  1206. }
  1207. status = vtenc_q_pop(vtctx, !frame, &buf);
  1208. if (status) goto end_nopkt;
  1209. if (!buf) goto end_nopkt;
  1210. status = vtenc_cm_to_avpacket(avctx, buf, pkt);
  1211. CFRelease(buf);
  1212. if (status) goto end_nopkt;
  1213. *got_packet = 1;
  1214. return 0;
  1215. end_nopkt:
  1216. av_packet_unref(pkt);
  1217. return status;
  1218. }
  1219. static av_cold int vtenc_close(AVCodecContext *avctx)
  1220. {
  1221. VTEncContext *vtctx = avctx->priv_data;
  1222. if(!vtctx->session) return 0;
  1223. pthread_cond_destroy(&vtctx->cv_sample_sent);
  1224. pthread_mutex_destroy(&vtctx->lock);
  1225. CFRelease(vtctx->session);
  1226. vtctx->session = NULL;
  1227. return 0;
  1228. }
  1229. static const enum AVPixelFormat pix_fmts[] = {
  1230. AV_PIX_FMT_VIDEOTOOLBOX,
  1231. AV_PIX_FMT_NV12,
  1232. AV_PIX_FMT_YUV420P,
  1233. AV_PIX_FMT_NONE
  1234. };
  1235. #define OFFSET(x) offsetof(VTEncContext, x)
  1236. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1237. static const AVOption options[] = {
  1238. { "profile", "Profile", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = H264_PROF_AUTO }, H264_PROF_AUTO, H264_PROF_COUNT, VE, "profile" },
  1239. { "baseline", "Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_BASELINE }, INT_MIN, INT_MAX, VE, "profile" },
  1240. { "main", "Main Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
  1241. { "high", "High Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_HIGH }, INT_MIN, INT_MAX, VE, "profile" },
  1242. { "level", "Level", OFFSET(level), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, VE, "level" },
  1243. { "1.3", "Level 1.3, only available with Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX, VE, "level" },
  1244. { "3.0", "Level 3.0", 0, AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX, VE, "level" },
  1245. { "3.1", "Level 3.1", 0, AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX, VE, "level" },
  1246. { "3.2", "Level 3.2", 0, AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX, VE, "level" },
  1247. { "4.0", "Level 4.0", 0, AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX, VE, "level" },
  1248. { "4.1", "Level 4.1", 0, AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX, VE, "level" },
  1249. { "4.2", "Level 4.2", 0, AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX, VE, "level" },
  1250. { "5.0", "Level 5.0", 0, AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX, VE, "level" },
  1251. { "5.1", "Level 5.1", 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX, VE, "level" },
  1252. { "5.2", "Level 5.2", 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX, VE, "level" },
  1253. { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL,
  1254. { .i64 = 0 }, 0, 1, VE },
  1255. { "coder", "Entropy coding", OFFSET(entropy), AV_OPT_TYPE_INT, { .i64 = VT_ENTROPY_NOT_SET }, VT_ENTROPY_NOT_SET, VT_CABAC, VE, "coder" },
  1256. { "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "coder" },
  1257. { "vlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "coder" },
  1258. { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "coder" },
  1259. { "ac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "coder" },
  1260. { NULL },
  1261. };
  1262. static const AVClass h264_videotoolbox_class = {
  1263. .class_name = "h264_videotoolbox",
  1264. .item_name = av_default_item_name,
  1265. .option = options,
  1266. .version = LIBAVUTIL_VERSION_INT,
  1267. };
  1268. AVCodec ff_h264_videotoolbox_encoder = {
  1269. .name = "h264_videotoolbox",
  1270. .long_name = NULL_IF_CONFIG_SMALL("VideoToolbox H.264 Encoder"),
  1271. .type = AVMEDIA_TYPE_VIDEO,
  1272. .id = AV_CODEC_ID_H264,
  1273. .priv_data_size = sizeof(VTEncContext),
  1274. .pix_fmts = pix_fmts,
  1275. .init = vtenc_init,
  1276. .encode2 = vtenc_frame,
  1277. .close = vtenc_close,
  1278. .capabilities = AV_CODEC_CAP_DELAY,
  1279. .priv_class = &h264_videotoolbox_class,
  1280. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  1281. FF_CODEC_CAP_INIT_CLEANUP,
  1282. };