0024-FFmpeg-optimize-copy-for-omxdec.patch 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. Description: <short summary of the patch>
  2. TODO: Put a short summary on the line above and replace this paragraph
  3. with a longer explanation of this change. Complete the meta-information
  4. with other relevant fields (see below for details). To make it easier, the
  5. information below has been extracted from the changelog. Adjust it or drop
  6. it.
  7. .
  8. ffmpeg (7:5.1.2-3) unstable; urgency=medium
  9. .
  10. * debian/rules: Disable more tests on ppc64 (Closes: #1022764)
  11. * debian/patches: Add upstream patch to fix FTBFS after vulkan changes
  12. (Closes: #1031447)
  13. Author: Sebastian Ramacher <sramacher@debian.org>
  14. Bug-Debian: https://bugs.debian.org/1022764
  15. Bug-Debian: https://bugs.debian.org/1031447
  16. ---
  17. The information above should follow the Patch Tagging Guidelines, please
  18. checkout https://dep.debian.net/deps/dep3/ to learn about the format. Here
  19. are templates for supplementary fields that you might want to add:
  20. Origin: (upstream|backport|vendor|other), (<patch-url>|commit:<commit-id>)
  21. Bug: <upstream-bugtracker-url>
  22. Bug-Debian: https://bugs.debian.org/<bugnumber>
  23. Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
  24. Forwarded: (no|not-needed|<patch-forwarded-url>)
  25. Applied-Upstream: <version>, (<commit-url>|commit:<commid-id>)
  26. Reviewed-By: <name and email of someone who approved/reviewed the patch>
  27. Last-Update: 2023-03-15
  28. --- ffmpeg-5.1.2.orig/libavcodec/allcodecs.c
  29. +++ ffmpeg-5.1.2/libavcodec/allcodecs.c
  30. @@ -34,6 +34,10 @@
  31. #include "codec_id.h"
  32. #include "codec_internal.h"
  33. +extern const FFCodec ff_h264_omx_decoder;
  34. +extern const FFCodec ff_hevc_omx_encoder;
  35. +extern const FFCodec ff_hevc_omx_decoder;
  36. +extern const FFCodec ff_mjpeg_omx_decoder;
  37. extern const FFCodec ff_a64multi_encoder;
  38. extern const FFCodec ff_a64multi5_encoder;
  39. extern const FFCodec ff_aasc_decoder;
  40. @@ -823,11 +827,7 @@ extern const FFCodec ff_h264_cuvid_decod
  41. extern const FFCodec ff_h264_mf_encoder;
  42. extern const FFCodec ff_h264_nvenc_encoder;
  43. extern const FFCodec ff_h264_omx_encoder;
  44. -extern const FFCodec ff_h264_omx_decoder;
  45. -extern const FFCodec ff_hevc_omx_encoder;
  46. -extern const FFCodec ff_hevc_omx_decoder;
  47. extern const FFCodec ff_h264_qsv_encoder;
  48. -extern const FFCodec ff_mjpeg_omx_decoder;
  49. extern const FFCodec ff_h264_v4l2m2m_encoder;
  50. extern const FFCodec ff_h264_vaapi_encoder;
  51. extern const FFCodec ff_h264_videotoolbox_encoder;
  52. --- ffmpeg-5.1.2.orig/libavcodec/omxdec.c
  53. +++ ffmpeg-5.1.2/libavcodec/omxdec.c
  54. @@ -34,6 +34,7 @@
  55. #include <stdlib.h>
  56. #include <sys/time.h>
  57. +#include "libavcodec/decode.h"
  58. #include "libavutil/avstring.h"
  59. #include "libavutil/avutil.h"
  60. #include "libavutil/common.h"
  61. @@ -355,6 +356,7 @@ typedef struct OMXCodecContext {
  62. int eos_sent, got_eos, evnet_bufferflag, first_get_outbuffer;
  63. int extradata_sent;
  64. + int has_cleanup;
  65. uint8_t *output_buf;
  66. int output_buf_size;
  67. @@ -943,6 +945,7 @@ static av_cold int omx_component_init(AV
  68. static av_cold void cleanup(OMXCodecContext *s)
  69. {
  70. int i, executing;
  71. + OMX_BUFFERHEADERTYPE *buffer;
  72. pthread_mutex_lock(&s->state_mutex);
  73. executing = s->state == OMX_StateExecuting;
  74. @@ -953,17 +956,24 @@ static av_cold void cleanup(OMXCodecCont
  75. wait_for_state(s, OMX_StateIdle);
  76. OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateLoaded, NULL);
  77. for (i = 0; i < s->num_in_buffers; i++) {
  78. - OMX_BUFFERHEADERTYPE *buffer = get_buffer(&s->input_mutex, &s->input_cond,
  79. + buffer = get_buffer(&s->input_mutex, &s->input_cond,
  80. &s->num_free_in_buffers, s->free_in_buffers, 1);
  81. if (s->input_zerocopy)
  82. buffer->pBuffer = NULL;
  83. OMX_FreeBuffer(s->handle, s->in_port, buffer);
  84. }
  85. - for (i = 0; i < s->num_out_buffers; i++) {
  86. - OMX_BUFFERHEADERTYPE *buffer = get_buffer(&s->output_mutex, &s->output_cond,
  87. - &s->num_done_out_buffers, s->done_out_buffers, 1);
  88. +
  89. + while (buffer = get_buffer(&s->output_mutex, &s->output_cond,
  90. + &s->num_done_out_buffers, s->done_out_buffers, 0)) {
  91. OMX_FreeBuffer(s->handle, s->out_port, buffer);
  92. + s->num_out_buffers--;
  93. + }
  94. +
  95. + if (s->num_out_buffers) {
  96. + s->has_cleanup = 1;
  97. + return;
  98. }
  99. +
  100. wait_for_state(s, OMX_StateLoaded);
  101. }
  102. if (s->handle) {
  103. @@ -1099,6 +1109,94 @@ fail:
  104. return ret;
  105. }
  106. +static void omx_free_out_buffer(void *opaque, uint8_t *unused)
  107. +{
  108. + OMX_ERRORTYPE err;
  109. + OMX_BUFFERHEADERTYPE *buffer = opaque;
  110. + OMXCodecContext *s = buffer->pAppPrivate;
  111. +
  112. + if (!s->has_cleanup) {
  113. + err = OMX_FillThisBuffer(s->handle, buffer);
  114. + if (err != OMX_ErrorNone) {
  115. + append_buffer(&s->output_mutex, &s->output_cond, &s->num_done_out_buffers, s->done_out_buffers, buffer);
  116. + av_log(s->avctx, AV_LOG_ERROR, "OMX_FillThisBuffer failed, err: %x\n", err);
  117. + }
  118. + } else {
  119. + OMX_FreeBuffer(s->handle, s->out_port, buffer);
  120. + s->num_out_buffers--;
  121. + if (!s->num_out_buffers) {
  122. + wait_for_state(s, OMX_StateLoaded);
  123. + if (s->handle) {
  124. + s->omx_context->ptr_FreeHandle(s->handle);
  125. + s->handle = NULL;
  126. + }
  127. +
  128. + omx_deinit(s->omx_context);
  129. + s->omx_context = NULL;
  130. + if (s->mutex_cond_inited) {
  131. + pthread_cond_destroy(&s->state_cond);
  132. + pthread_mutex_destroy(&s->state_mutex);
  133. + pthread_cond_destroy(&s->input_cond);
  134. + pthread_mutex_destroy(&s->input_mutex);
  135. + pthread_cond_destroy(&s->output_cond);
  136. + pthread_mutex_destroy(&s->output_mutex);
  137. + pthread_cond_destroy(&s->disableEVnt_cond);
  138. + pthread_mutex_destroy(&s->disableEVnt_mutex);
  139. + s->mutex_cond_inited = 0;
  140. + }
  141. + OMXDecodeQueueDestory(&s->decode_pts_queue);
  142. + av_freep(&s->decode_pts_queue);
  143. + av_freep(&s->in_buffer_headers);
  144. + av_freep(&s->out_buffer_headers);
  145. + av_freep(&s->free_in_buffers);
  146. + av_freep(&s->done_out_buffers);
  147. + av_freep(&s->output_buf);
  148. + }
  149. + }
  150. +}
  151. +
  152. +static int omx_buf_to_swframe(OMXCodecContext *s, AVFrame *frame, OMX_BUFFERHEADERTYPE *buffer)
  153. +{
  154. + int ret;
  155. + uint8_t *dst[4];
  156. + int linesize[4];
  157. +
  158. + frame->buf[0] = av_buffer_create((char *)buffer->pBuffer, buffer->nFilledLen, omx_free_out_buffer, buffer, 0);
  159. +
  160. + if (!frame->buf[0])
  161. + return AVERROR(ENOMEM);
  162. +
  163. + ret = av_image_fill_arrays(dst, linesize, buffer->pBuffer,
  164. + s->avctx->pix_fmt, s->stride, s->plane_size, 1);
  165. + if (ret < 0){
  166. + av_log(s->avctx, AV_LOG_ERROR, "av_image_fill_arrays ret:%d\n", ret);
  167. + return AVERROR(EINVAL);
  168. + }
  169. +
  170. + frame->linesize[0] = linesize[0];
  171. + frame->data[0] = frame->buf[0]->data;
  172. +
  173. + /* fixup special cases */
  174. + switch (s->avctx->pix_fmt) {
  175. + case AV_PIX_FMT_NV12:
  176. + case AV_PIX_FMT_NV21:
  177. + frame->linesize[1] = linesize[1];
  178. + frame->data[1] = dst[1];
  179. + break;
  180. +
  181. + case AV_PIX_FMT_YUV420P:
  182. + frame->linesize[1] = linesize[1];
  183. + frame->linesize[2] = linesize[2];
  184. + frame->data[1] = dst[1];
  185. + frame->data[2] = dst[2];
  186. + break;
  187. +
  188. + default:
  189. + break;
  190. + }
  191. +
  192. + return 0;
  193. +}
  194. static int omx_decode_frame(AVCodecContext *avctx, AVFrame *data,
  195. int *got_packet, AVPacket *pkt)
  196. @@ -1111,9 +1209,6 @@ static int omx_decode_frame(AVCodecConte
  197. AVFrame *avframe = data;
  198. - uint8_t *dst[4];
  199. - int linesize[4];
  200. -
  201. av_log(avctx, AV_LOG_VERBOSE, "s->decode_flag: %d\n", s->decode_flag);
  202. av_log(avctx, AV_LOG_VERBOSE, "avctx->time_base: %d/%d \n", avctx->time_base.num, avctx->time_base.den);
  203. av_log(avctx, AV_LOG_VERBOSE, "avctx->pkt_timebase: %d/%d \n", avctx->pkt_timebase.num, avctx->pkt_timebase.den);
  204. @@ -1226,20 +1321,21 @@ static int omx_decode_frame(AVCodecConte
  205. if (buffer->nFlags & OMX_BUFFERFLAG_EOS)
  206. s->got_eos = 1;
  207. - if ((ret = ff_get_buffer(avctx, avframe, 0)) < 0) {
  208. - av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
  209. + avframe->width = avctx->width;
  210. + avframe->height = avctx->height;
  211. +
  212. + ret = ff_decode_frame_props(avctx, avframe);
  213. + if(ret < 0) {
  214. + av_log(avctx, AV_LOG_ERROR, "Unable to fill buffer props\n");
  215. goto end;
  216. }
  217. - ret = av_image_fill_arrays(dst, linesize, buffer->pBuffer,
  218. - avctx->pix_fmt, s->stride, s->plane_size, 1);
  219. - if (ret < 0){
  220. - av_log(avctx, AV_LOG_ERROR, "av_image_fill_arrays ret:%d\n", ret);
  221. + ret = omx_buf_to_swframe(s, avframe, buffer);
  222. + if(ret < 0) {
  223. + av_log(avctx, AV_LOG_ERROR, "Unable to alloce frame\n");
  224. goto end;
  225. }
  226. - av_image_copy(avframe->data, avframe->linesize, (const uint8_t**)dst, linesize,
  227. - avctx->pix_fmt, avctx->width, avctx->height);
  228. if (pkt->pts) {
  229. if (OMXDecodeQueueEmpty(&s->decode_pts_queue) != 0){
  230. av_log(avctx, AV_LOG_ERROR, "The queue of decode pts is empty.\n");
  231. @@ -1251,10 +1347,7 @@ static int omx_decode_frame(AVCodecConte
  232. s->decode_flag += 1;
  233. *got_packet = 1;
  234. - /*
  235. - if ((ret = av_frame_ref(data, avframe)) < 0)
  236. - goto end;
  237. - */
  238. + return ret;
  239. end:
  240. err = OMX_FillThisBuffer(s->handle, buffer);