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.

418 lines
12KB

  1. /*
  2. *
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "audioconvert.h"
  20. #include "buffer.h"
  21. #include "common.h"
  22. #include "frame.h"
  23. #include "imgutils.h"
  24. #include "mem.h"
  25. #include "samplefmt.h"
  26. #define MAKE_ACCESSORS(str, name, type, field) \
  27. type av_##name##_get_##field(const str *s) { return s->field; } \
  28. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  29. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  30. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  31. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  32. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  33. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  34. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  35. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  36. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  37. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  38. AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
  39. static void get_frame_defaults(AVFrame *frame)
  40. {
  41. if (frame->extended_data != frame->data)
  42. av_freep(&frame->extended_data);
  43. memset(frame, 0, sizeof(*frame));
  44. frame->pts = AV_NOPTS_VALUE;
  45. frame->key_frame = 1;
  46. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  47. frame->format = -1; /* unknown */
  48. frame->extended_data = frame->data;
  49. }
  50. AVFrame *av_frame_alloc(void)
  51. {
  52. AVFrame *frame = av_mallocz(sizeof(*frame));
  53. if (!frame)
  54. return NULL;
  55. get_frame_defaults(frame);
  56. return frame;
  57. }
  58. void av_frame_free(AVFrame **frame)
  59. {
  60. if (!frame || !*frame)
  61. return;
  62. av_frame_unref(*frame);
  63. av_freep(frame);
  64. }
  65. static int get_video_buffer(AVFrame *frame, int align)
  66. {
  67. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  68. int ret, i;
  69. if (!desc)
  70. return AVERROR(EINVAL);
  71. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  72. return ret;
  73. if (!frame->linesize[0]) {
  74. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  75. frame->width);
  76. if (ret < 0)
  77. return ret;
  78. for (i = 0; i < 4 && frame->linesize[i]; i++)
  79. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  80. }
  81. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  82. int h = frame->height;
  83. if (i == 1 || i == 2)
  84. h = -((-h) >> desc->log2_chroma_h);
  85. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
  86. if (!frame->buf[i])
  87. goto fail;
  88. frame->data[i] = frame->buf[i]->data;
  89. }
  90. if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL) {
  91. av_buffer_unref(&frame->buf[1]);
  92. frame->buf[1] = av_buffer_alloc(1024);
  93. if (!frame->buf[1])
  94. goto fail;
  95. frame->data[1] = frame->buf[1]->data;
  96. }
  97. frame->extended_data = frame->data;
  98. return 0;
  99. fail:
  100. av_frame_unref(frame);
  101. return AVERROR(ENOMEM);
  102. }
  103. static int get_audio_buffer(AVFrame *frame, int align)
  104. {
  105. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  106. int planar = av_sample_fmt_is_planar(frame->format);
  107. int planes = planar ? channels : 1;
  108. int ret, i;
  109. if (!frame->linesize[0]) {
  110. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  111. frame->nb_samples, frame->format,
  112. align);
  113. if (ret < 0)
  114. return ret;
  115. }
  116. if (planes > AV_NUM_DATA_POINTERS) {
  117. frame->extended_data = av_mallocz(planes *
  118. sizeof(*frame->extended_data));
  119. frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
  120. sizeof(*frame->extended_buf));
  121. if (!frame->extended_data || !frame->extended_buf) {
  122. av_freep(&frame->extended_data);
  123. av_freep(&frame->extended_buf);
  124. return AVERROR(ENOMEM);
  125. }
  126. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  127. } else
  128. frame->extended_data = frame->data;
  129. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  130. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  131. if (!frame->buf[i]) {
  132. av_frame_unref(frame);
  133. return AVERROR(ENOMEM);
  134. }
  135. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  136. }
  137. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  138. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  139. if (!frame->extended_buf[i]) {
  140. av_frame_unref(frame);
  141. return AVERROR(ENOMEM);
  142. }
  143. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  144. }
  145. return 0;
  146. }
  147. int av_frame_get_buffer(AVFrame *frame, int align)
  148. {
  149. if (frame->format < 0)
  150. return AVERROR(EINVAL);
  151. if (frame->width > 0 && frame->height > 0)
  152. return get_video_buffer(frame, align);
  153. else if (frame->nb_samples > 0 && frame->channel_layout)
  154. return get_audio_buffer(frame, align);
  155. return AVERROR(EINVAL);
  156. }
  157. int av_frame_ref(AVFrame *dst, AVFrame *src)
  158. {
  159. int i, ret = 0;
  160. dst->format = src->format;
  161. dst->width = src->width;
  162. dst->height = src->height;
  163. dst->channel_layout = src->channel_layout;
  164. dst->nb_samples = src->nb_samples;
  165. ret = av_frame_copy_props(dst, src);
  166. if (ret < 0)
  167. return ret;
  168. /* duplicate the frame data if it's not refcounted */
  169. if (!src->buf[0]) {
  170. ret = av_frame_get_buffer(dst, 32);
  171. if (ret < 0)
  172. return ret;
  173. if (src->nb_samples) {
  174. int ch = av_get_channel_layout_nb_channels(src->channel_layout);
  175. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  176. dst->nb_samples, ch, dst->format);
  177. } else {
  178. av_image_copy(dst->data, dst->linesize, src->data, src->linesize,
  179. dst->format, dst->width, dst->height);
  180. }
  181. return 0;
  182. }
  183. /* ref the buffers */
  184. for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
  185. dst->buf[i] = av_buffer_ref(src->buf[i]);
  186. if (!dst->buf[i]) {
  187. ret = AVERROR(ENOMEM);
  188. goto fail;
  189. }
  190. }
  191. if (src->extended_buf) {
  192. dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
  193. src->nb_extended_buf);
  194. if (!dst->extended_buf) {
  195. ret = AVERROR(ENOMEM);
  196. goto fail;
  197. }
  198. dst->nb_extended_buf = src->nb_extended_buf;
  199. for (i = 0; i < src->nb_extended_buf; i++) {
  200. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  201. if (!dst->extended_buf[i]) {
  202. ret = AVERROR(ENOMEM);
  203. goto fail;
  204. }
  205. }
  206. }
  207. /* duplicate extended data */
  208. if (src->extended_data != src->data) {
  209. int ch = av_get_channel_layout_nb_channels(src->channel_layout);
  210. if (!ch) {
  211. ret = AVERROR(EINVAL);
  212. goto fail;
  213. }
  214. dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
  215. if (!dst->extended_data) {
  216. ret = AVERROR(ENOMEM);
  217. goto fail;
  218. }
  219. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  220. } else
  221. dst->extended_data = dst->data;
  222. memcpy(dst->data, src->data, sizeof(src->data));
  223. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  224. return 0;
  225. fail:
  226. av_frame_unref(dst);
  227. return ret;
  228. }
  229. AVFrame *av_frame_clone(AVFrame *src)
  230. {
  231. AVFrame *ret = av_frame_alloc();
  232. if (!ret)
  233. return NULL;
  234. if (av_frame_ref(ret, src) < 0)
  235. av_frame_free(&ret);
  236. return ret;
  237. }
  238. void av_frame_unref(AVFrame *frame)
  239. {
  240. int i;
  241. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  242. av_buffer_unref(&frame->buf[i]);
  243. for (i = 0; i < frame->nb_extended_buf; i++)
  244. av_buffer_unref(&frame->extended_buf[i]);
  245. av_freep(&frame->extended_buf);
  246. get_frame_defaults(frame);
  247. }
  248. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  249. {
  250. *dst = *src;
  251. if (src->extended_data == src->data)
  252. dst->extended_data = dst->data;
  253. memset(src, 0, sizeof(*src));
  254. get_frame_defaults(src);
  255. }
  256. int av_frame_is_writable(AVFrame *frame)
  257. {
  258. int i, ret = 1;
  259. /* assume non-refcounted frames are not writable */
  260. if (!frame->buf[0])
  261. return 0;
  262. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
  263. ret &= !!av_buffer_is_writable(frame->buf[i]);
  264. for (i = 0; i < frame->nb_extended_buf; i++)
  265. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  266. return ret;
  267. }
  268. int av_frame_make_writable(AVFrame *frame)
  269. {
  270. AVFrame tmp;
  271. int ret;
  272. if (!frame->buf[0])
  273. return AVERROR(EINVAL);
  274. if (av_frame_is_writable(frame))
  275. return 0;
  276. memset(&tmp, 0, sizeof(tmp));
  277. tmp.format = frame->format;
  278. tmp.width = frame->width;
  279. tmp.height = frame->height;
  280. tmp.channel_layout = frame->channel_layout;
  281. tmp.nb_samples = frame->nb_samples;
  282. ret = av_frame_get_buffer(&tmp, 32);
  283. if (ret < 0)
  284. return ret;
  285. if (tmp.nb_samples) {
  286. int ch = av_get_channel_layout_nb_channels(tmp.channel_layout);
  287. av_samples_copy(tmp.extended_data, frame->extended_data, 0, 0,
  288. frame->nb_samples, ch, frame->format);
  289. } else {
  290. av_image_copy(tmp.data, tmp.linesize, frame->data, frame->linesize,
  291. frame->format, frame->width, frame->height);
  292. }
  293. ret = av_frame_copy_props(&tmp, frame);
  294. if (ret < 0) {
  295. av_frame_unref(&tmp);
  296. return ret;
  297. }
  298. av_frame_unref(frame);
  299. *frame = tmp;
  300. if (tmp.data == tmp.extended_data)
  301. frame->extended_data = frame->data;
  302. return 0;
  303. }
  304. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  305. {
  306. dst->key_frame = src->key_frame;
  307. dst->pict_type = src->pict_type;
  308. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  309. dst->pts = src->pts;
  310. dst->interlaced_frame = src->interlaced_frame;
  311. dst->top_field_first = src->top_field_first;
  312. dst->sample_rate = src->sample_rate;
  313. dst->opaque = src->opaque;
  314. dst->pkt_pts = src->pkt_pts;
  315. dst->pkt_dts = src->pkt_dts;
  316. dst->quality = src->quality;
  317. dst->coded_picture_number = src->coded_picture_number;
  318. dst->display_picture_number = src->display_picture_number;
  319. return 0;
  320. }
  321. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  322. {
  323. uint8_t *data;
  324. int planes, i;
  325. if (frame->nb_samples) {
  326. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  327. if (!channels)
  328. return NULL;
  329. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  330. } else
  331. planes = 4;
  332. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  333. return NULL;
  334. data = frame->extended_data[plane];
  335. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  336. AVBufferRef *buf = frame->buf[i];
  337. if (data >= buf->data && data < buf->data + buf->size)
  338. return buf;
  339. }
  340. for (i = 0; i < frame->nb_extended_buf; i++) {
  341. AVBufferRef *buf = frame->extended_buf[i];
  342. if (data >= buf->data && data < buf->data + buf->size)
  343. return buf;
  344. }
  345. return NULL;
  346. }