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.

490 lines
14KB

  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 "dict.h"
  23. #include "frame.h"
  24. #include "imgutils.h"
  25. #include "mem.h"
  26. #include "samplefmt.h"
  27. #define MAKE_ACCESSORS(str, name, type, field) \
  28. type av_##name##_get_##field(const str *s) { return s->field; } \
  29. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  30. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  31. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  32. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  33. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  34. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  35. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  36. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  37. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  38. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  39. AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
  40. static void get_frame_defaults(AVFrame *frame)
  41. {
  42. if (frame->extended_data != frame->data)
  43. av_freep(&frame->extended_data);
  44. memset(frame, 0, sizeof(*frame));
  45. frame->pts = AV_NOPTS_VALUE;
  46. frame->key_frame = 1;
  47. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  48. frame->format = -1; /* unknown */
  49. frame->extended_data = frame->data;
  50. }
  51. AVFrame *av_frame_alloc(void)
  52. {
  53. AVFrame *frame = av_mallocz(sizeof(*frame));
  54. if (!frame)
  55. return NULL;
  56. get_frame_defaults(frame);
  57. return frame;
  58. }
  59. void av_frame_free(AVFrame **frame)
  60. {
  61. if (!frame || !*frame)
  62. return;
  63. av_frame_unref(*frame);
  64. av_freep(frame);
  65. }
  66. static int get_video_buffer(AVFrame *frame, int align)
  67. {
  68. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  69. int ret, i;
  70. if (!desc)
  71. return AVERROR(EINVAL);
  72. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  73. return ret;
  74. if (!frame->linesize[0]) {
  75. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  76. frame->width);
  77. if (ret < 0)
  78. return ret;
  79. for (i = 0; i < 4 && frame->linesize[i]; i++)
  80. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  81. }
  82. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  83. int h = frame->height;
  84. if (i == 1 || i == 2)
  85. h = -((-h) >> desc->log2_chroma_h);
  86. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
  87. if (!frame->buf[i])
  88. goto fail;
  89. frame->data[i] = frame->buf[i]->data;
  90. }
  91. if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL) {
  92. av_buffer_unref(&frame->buf[1]);
  93. frame->buf[1] = av_buffer_alloc(1024);
  94. if (!frame->buf[1])
  95. goto fail;
  96. frame->data[1] = frame->buf[1]->data;
  97. }
  98. frame->extended_data = frame->data;
  99. return 0;
  100. fail:
  101. av_frame_unref(frame);
  102. return AVERROR(ENOMEM);
  103. }
  104. static int get_audio_buffer(AVFrame *frame, int align)
  105. {
  106. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  107. int planar = av_sample_fmt_is_planar(frame->format);
  108. int planes = planar ? channels : 1;
  109. int ret, i;
  110. if (!frame->linesize[0]) {
  111. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  112. frame->nb_samples, frame->format,
  113. align);
  114. if (ret < 0)
  115. return ret;
  116. }
  117. if (planes > AV_NUM_DATA_POINTERS) {
  118. frame->extended_data = av_mallocz(planes *
  119. sizeof(*frame->extended_data));
  120. frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
  121. sizeof(*frame->extended_buf));
  122. if (!frame->extended_data || !frame->extended_buf) {
  123. av_freep(&frame->extended_data);
  124. av_freep(&frame->extended_buf);
  125. return AVERROR(ENOMEM);
  126. }
  127. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  128. } else
  129. frame->extended_data = frame->data;
  130. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  131. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  132. if (!frame->buf[i]) {
  133. av_frame_unref(frame);
  134. return AVERROR(ENOMEM);
  135. }
  136. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  137. }
  138. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  139. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  140. if (!frame->extended_buf[i]) {
  141. av_frame_unref(frame);
  142. return AVERROR(ENOMEM);
  143. }
  144. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  145. }
  146. return 0;
  147. }
  148. int av_frame_get_buffer(AVFrame *frame, int align)
  149. {
  150. if (frame->format < 0)
  151. return AVERROR(EINVAL);
  152. if (frame->width > 0 && frame->height > 0)
  153. return get_video_buffer(frame, align);
  154. else if (frame->nb_samples > 0 && frame->channel_layout)
  155. return get_audio_buffer(frame, align);
  156. return AVERROR(EINVAL);
  157. }
  158. int av_frame_ref(AVFrame *dst, AVFrame *src)
  159. {
  160. int i, ret = 0;
  161. dst->format = src->format;
  162. dst->width = src->width;
  163. dst->height = src->height;
  164. dst->channel_layout = src->channel_layout;
  165. dst->nb_samples = src->nb_samples;
  166. ret = av_frame_copy_props(dst, src);
  167. if (ret < 0)
  168. return ret;
  169. /* duplicate the frame data if it's not refcounted */
  170. if (!src->buf[0]) {
  171. ret = av_frame_get_buffer(dst, 32);
  172. if (ret < 0)
  173. return ret;
  174. if (src->nb_samples) {
  175. int ch = av_get_channel_layout_nb_channels(src->channel_layout);
  176. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  177. dst->nb_samples, ch, dst->format);
  178. } else {
  179. av_image_copy(dst->data, dst->linesize, src->data, src->linesize,
  180. dst->format, dst->width, dst->height);
  181. }
  182. return 0;
  183. }
  184. /* ref the buffers */
  185. for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
  186. dst->buf[i] = av_buffer_ref(src->buf[i]);
  187. if (!dst->buf[i]) {
  188. ret = AVERROR(ENOMEM);
  189. goto fail;
  190. }
  191. }
  192. if (src->extended_buf) {
  193. dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
  194. src->nb_extended_buf);
  195. if (!dst->extended_buf) {
  196. ret = AVERROR(ENOMEM);
  197. goto fail;
  198. }
  199. dst->nb_extended_buf = src->nb_extended_buf;
  200. for (i = 0; i < src->nb_extended_buf; i++) {
  201. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  202. if (!dst->extended_buf[i]) {
  203. ret = AVERROR(ENOMEM);
  204. goto fail;
  205. }
  206. }
  207. }
  208. /* duplicate extended data */
  209. if (src->extended_data != src->data) {
  210. int ch = av_get_channel_layout_nb_channels(src->channel_layout);
  211. if (!ch) {
  212. ret = AVERROR(EINVAL);
  213. goto fail;
  214. }
  215. dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
  216. if (!dst->extended_data) {
  217. ret = AVERROR(ENOMEM);
  218. goto fail;
  219. }
  220. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  221. } else
  222. dst->extended_data = dst->data;
  223. memcpy(dst->data, src->data, sizeof(src->data));
  224. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  225. return 0;
  226. fail:
  227. av_frame_unref(dst);
  228. return ret;
  229. }
  230. AVFrame *av_frame_clone(AVFrame *src)
  231. {
  232. AVFrame *ret = av_frame_alloc();
  233. if (!ret)
  234. return NULL;
  235. if (av_frame_ref(ret, src) < 0)
  236. av_frame_free(&ret);
  237. return ret;
  238. }
  239. void av_frame_unref(AVFrame *frame)
  240. {
  241. int i;
  242. for (i = 0; i < frame->nb_side_data; i++) {
  243. av_freep(&frame->side_data[i]->data);
  244. av_dict_free(&frame->side_data[i]->metadata);
  245. av_freep(&frame->side_data[i]);
  246. }
  247. av_freep(&frame->side_data);
  248. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  249. av_buffer_unref(&frame->buf[i]);
  250. for (i = 0; i < frame->nb_extended_buf; i++)
  251. av_buffer_unref(&frame->extended_buf[i]);
  252. av_freep(&frame->extended_buf);
  253. get_frame_defaults(frame);
  254. }
  255. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  256. {
  257. *dst = *src;
  258. if (src->extended_data == src->data)
  259. dst->extended_data = dst->data;
  260. memset(src, 0, sizeof(*src));
  261. get_frame_defaults(src);
  262. }
  263. int av_frame_is_writable(AVFrame *frame)
  264. {
  265. int i, ret = 1;
  266. /* assume non-refcounted frames are not writable */
  267. if (!frame->buf[0])
  268. return 0;
  269. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
  270. ret &= !!av_buffer_is_writable(frame->buf[i]);
  271. for (i = 0; i < frame->nb_extended_buf; i++)
  272. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  273. return ret;
  274. }
  275. int av_frame_make_writable(AVFrame *frame)
  276. {
  277. AVFrame tmp;
  278. int ret;
  279. if (!frame->buf[0])
  280. return AVERROR(EINVAL);
  281. if (av_frame_is_writable(frame))
  282. return 0;
  283. memset(&tmp, 0, sizeof(tmp));
  284. tmp.format = frame->format;
  285. tmp.width = frame->width;
  286. tmp.height = frame->height;
  287. tmp.channel_layout = frame->channel_layout;
  288. tmp.nb_samples = frame->nb_samples;
  289. ret = av_frame_get_buffer(&tmp, 32);
  290. if (ret < 0)
  291. return ret;
  292. if (tmp.nb_samples) {
  293. int ch = av_get_channel_layout_nb_channels(tmp.channel_layout);
  294. av_samples_copy(tmp.extended_data, frame->extended_data, 0, 0,
  295. frame->nb_samples, ch, frame->format);
  296. } else {
  297. av_image_copy(tmp.data, tmp.linesize, frame->data, frame->linesize,
  298. frame->format, frame->width, frame->height);
  299. }
  300. ret = av_frame_copy_props(&tmp, frame);
  301. if (ret < 0) {
  302. av_frame_unref(&tmp);
  303. return ret;
  304. }
  305. av_frame_unref(frame);
  306. *frame = tmp;
  307. if (tmp.data == tmp.extended_data)
  308. frame->extended_data = frame->data;
  309. return 0;
  310. }
  311. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  312. {
  313. int i;
  314. dst->key_frame = src->key_frame;
  315. dst->pict_type = src->pict_type;
  316. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  317. dst->pts = src->pts;
  318. dst->interlaced_frame = src->interlaced_frame;
  319. dst->top_field_first = src->top_field_first;
  320. dst->sample_rate = src->sample_rate;
  321. dst->opaque = src->opaque;
  322. dst->pkt_pts = src->pkt_pts;
  323. dst->pkt_dts = src->pkt_dts;
  324. dst->quality = src->quality;
  325. dst->coded_picture_number = src->coded_picture_number;
  326. dst->display_picture_number = src->display_picture_number;
  327. for (i = 0; i < src->nb_side_data; i++) {
  328. const AVFrameSideData *sd_src = src->side_data[i];
  329. AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
  330. sd_src->size);
  331. if (!sd_dst) {
  332. for (i = 0; i < dst->nb_side_data; i++) {
  333. av_freep(&dst->side_data[i]->data);
  334. av_freep(&dst->side_data[i]);
  335. av_dict_free(&dst->side_data[i]->metadata);
  336. }
  337. av_freep(&dst->side_data);
  338. return AVERROR(ENOMEM);
  339. }
  340. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  341. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  342. }
  343. return 0;
  344. }
  345. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  346. {
  347. uint8_t *data;
  348. int planes, i;
  349. if (frame->nb_samples) {
  350. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  351. if (!channels)
  352. return NULL;
  353. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  354. } else
  355. planes = 4;
  356. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  357. return NULL;
  358. data = frame->extended_data[plane];
  359. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  360. AVBufferRef *buf = frame->buf[i];
  361. if (data >= buf->data && data < buf->data + buf->size)
  362. return buf;
  363. }
  364. for (i = 0; i < frame->nb_extended_buf; i++) {
  365. AVBufferRef *buf = frame->extended_buf[i];
  366. if (data >= buf->data && data < buf->data + buf->size)
  367. return buf;
  368. }
  369. return NULL;
  370. }
  371. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  372. enum AVFrameSideDataType type,
  373. int size)
  374. {
  375. AVFrameSideData *ret, **tmp;
  376. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  377. return NULL;
  378. tmp = av_realloc(frame->side_data,
  379. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  380. if (!tmp)
  381. return NULL;
  382. frame->side_data = tmp;
  383. ret = av_mallocz(sizeof(*ret));
  384. if (!ret)
  385. return NULL;
  386. ret->data = av_malloc(size);
  387. if (!ret->data) {
  388. av_freep(&ret);
  389. return NULL;
  390. }
  391. ret->size = size;
  392. ret->type = type;
  393. frame->side_data[frame->nb_side_data++] = ret;
  394. return ret;
  395. }
  396. AVFrameSideData *av_frame_get_side_data(AVFrame *frame,
  397. enum AVFrameSideDataType type)
  398. {
  399. int i;
  400. for (i = 0; i < frame->nb_side_data; i++) {
  401. if (frame->side_data[i]->type == type)
  402. return frame->side_data[i];
  403. }
  404. return NULL;
  405. }