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.

1465 lines
43KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/dict.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <limits.h>
  44. #include <float.h>
  45. static int volatile entangled_thread_counter=0;
  46. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  47. static void *codec_mutex;
  48. void *av_fast_realloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
  49. {
  50. if(min_size < *size)
  51. return ptr;
  52. min_size= FFMAX(17*min_size/16 + 32, min_size);
  53. ptr= av_realloc(ptr, min_size);
  54. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  55. min_size= 0;
  56. *size= min_size;
  57. return ptr;
  58. }
  59. void av_fast_malloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
  60. {
  61. void **p = ptr;
  62. if (min_size < *size)
  63. return;
  64. min_size= FFMAX(17*min_size/16 + 32, min_size);
  65. av_free(*p);
  66. *p = av_malloc(min_size);
  67. if (!*p) min_size = 0;
  68. *size= min_size;
  69. }
  70. /* encoder management */
  71. static AVCodec *first_avcodec = NULL;
  72. AVCodec *av_codec_next(AVCodec *c){
  73. if(c) return c->next;
  74. else return first_avcodec;
  75. }
  76. void avcodec_register(AVCodec *codec)
  77. {
  78. AVCodec **p;
  79. avcodec_init();
  80. p = &first_avcodec;
  81. while (*p != NULL) p = &(*p)->next;
  82. *p = codec;
  83. codec->next = NULL;
  84. }
  85. #if LIBAVCODEC_VERSION_MAJOR < 53
  86. void register_avcodec(AVCodec *codec)
  87. {
  88. avcodec_register(codec);
  89. }
  90. #endif
  91. unsigned avcodec_get_edge_width(void)
  92. {
  93. return EDGE_WIDTH;
  94. }
  95. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  96. s->coded_width = width;
  97. s->coded_height= height;
  98. s->width = -((-width )>>s->lowres);
  99. s->height= -((-height)>>s->lowres);
  100. }
  101. typedef struct InternalBuffer{
  102. int last_pic_num;
  103. uint8_t *base[4];
  104. uint8_t *data[4];
  105. int linesize[4];
  106. int width, height;
  107. enum PixelFormat pix_fmt;
  108. }InternalBuffer;
  109. #define INTERNAL_BUFFER_SIZE (32+1)
  110. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  111. int w_align= 1;
  112. int h_align= 1;
  113. switch(s->pix_fmt){
  114. case PIX_FMT_YUV420P:
  115. case PIX_FMT_YUYV422:
  116. case PIX_FMT_UYVY422:
  117. case PIX_FMT_YUV422P:
  118. case PIX_FMT_YUV440P:
  119. case PIX_FMT_YUV444P:
  120. case PIX_FMT_GRAY8:
  121. case PIX_FMT_GRAY16BE:
  122. case PIX_FMT_GRAY16LE:
  123. case PIX_FMT_YUVJ420P:
  124. case PIX_FMT_YUVJ422P:
  125. case PIX_FMT_YUVJ440P:
  126. case PIX_FMT_YUVJ444P:
  127. case PIX_FMT_YUVA420P:
  128. case PIX_FMT_YUV420P9LE:
  129. case PIX_FMT_YUV420P9BE:
  130. case PIX_FMT_YUV420P10LE:
  131. case PIX_FMT_YUV420P10BE:
  132. case PIX_FMT_YUV422P10LE:
  133. case PIX_FMT_YUV422P10BE:
  134. case PIX_FMT_YUV444P9LE:
  135. case PIX_FMT_YUV444P9BE:
  136. case PIX_FMT_YUV444P10LE:
  137. case PIX_FMT_YUV444P10BE:
  138. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  139. h_align= 16;
  140. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
  141. h_align= 32; // interlaced is rounded up to 2 MBs
  142. break;
  143. case PIX_FMT_YUV411P:
  144. case PIX_FMT_UYYVYY411:
  145. w_align=32;
  146. h_align=8;
  147. break;
  148. case PIX_FMT_YUV410P:
  149. if(s->codec_id == CODEC_ID_SVQ1){
  150. w_align=64;
  151. h_align=64;
  152. }
  153. case PIX_FMT_RGB555:
  154. if(s->codec_id == CODEC_ID_RPZA){
  155. w_align=4;
  156. h_align=4;
  157. }
  158. case PIX_FMT_PAL8:
  159. case PIX_FMT_BGR8:
  160. case PIX_FMT_RGB8:
  161. if(s->codec_id == CODEC_ID_SMC){
  162. w_align=4;
  163. h_align=4;
  164. }
  165. break;
  166. case PIX_FMT_BGR24:
  167. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  168. w_align=4;
  169. h_align=4;
  170. }
  171. break;
  172. default:
  173. w_align= 1;
  174. h_align= 1;
  175. break;
  176. }
  177. *width = FFALIGN(*width , w_align);
  178. *height= FFALIGN(*height, h_align);
  179. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  180. *height+=2; // some of the optimized chroma MC reads one line too much
  181. // which is also done in mpeg decoders with lowres > 0
  182. linesize_align[0] =
  183. linesize_align[1] =
  184. linesize_align[2] =
  185. linesize_align[3] = STRIDE_ALIGN;
  186. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  187. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  188. //picture size unneccessarily in some cases. The solution here is not
  189. //pretty and better ideas are welcome!
  190. #if HAVE_MMX
  191. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  192. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  193. s->codec_id == CODEC_ID_VP6A) {
  194. linesize_align[0] =
  195. linesize_align[1] =
  196. linesize_align[2] = 16;
  197. }
  198. #endif
  199. }
  200. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  201. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  202. int linesize_align[4];
  203. int align;
  204. avcodec_align_dimensions2(s, width, height, linesize_align);
  205. align = FFMAX(linesize_align[0], linesize_align[3]);
  206. linesize_align[1] <<= chroma_shift;
  207. linesize_align[2] <<= chroma_shift;
  208. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  209. *width=FFALIGN(*width, align);
  210. }
  211. #if LIBAVCODEC_VERSION_MAJOR < 53
  212. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  213. return av_image_check_size(w, h, 0, av_log_ctx);
  214. }
  215. #endif
  216. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  217. int i;
  218. int w= s->width;
  219. int h= s->height;
  220. InternalBuffer *buf;
  221. int *picture_number;
  222. if(pic->data[0]!=NULL) {
  223. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  224. return -1;
  225. }
  226. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  227. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  228. return -1;
  229. }
  230. if(av_image_check_size(w, h, 0, s))
  231. return -1;
  232. if(s->internal_buffer==NULL){
  233. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  234. }
  235. #if 0
  236. s->internal_buffer= av_fast_realloc(
  237. s->internal_buffer,
  238. &s->internal_buffer_size,
  239. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  240. );
  241. #endif
  242. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  243. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  244. (*picture_number)++;
  245. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  246. if(s->active_thread_type&FF_THREAD_FRAME) {
  247. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  248. return -1;
  249. }
  250. for(i=0; i<4; i++){
  251. av_freep(&buf->base[i]);
  252. buf->data[i]= NULL;
  253. }
  254. }
  255. if(buf->base[0]){
  256. pic->age= *picture_number - buf->last_pic_num;
  257. buf->last_pic_num= *picture_number;
  258. }else{
  259. int h_chroma_shift, v_chroma_shift;
  260. int size[4] = {0};
  261. int tmpsize;
  262. int unaligned;
  263. AVPicture picture;
  264. int stride_align[4];
  265. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  266. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  267. avcodec_align_dimensions2(s, &w, &h, stride_align);
  268. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  269. w+= EDGE_WIDTH*2;
  270. h+= EDGE_WIDTH*2;
  271. }
  272. do {
  273. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  274. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  275. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  276. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  277. w += w & ~(w-1);
  278. unaligned = 0;
  279. for (i=0; i<4; i++){
  280. unaligned |= picture.linesize[i] % stride_align[i];
  281. }
  282. } while (unaligned);
  283. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  284. if (tmpsize < 0)
  285. return -1;
  286. for (i=0; i<3 && picture.data[i+1]; i++)
  287. size[i] = picture.data[i+1] - picture.data[i];
  288. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  289. buf->last_pic_num= -256*256*256*64;
  290. memset(buf->base, 0, sizeof(buf->base));
  291. memset(buf->data, 0, sizeof(buf->data));
  292. for(i=0; i<4 && size[i]; i++){
  293. const int h_shift= i==0 ? 0 : h_chroma_shift;
  294. const int v_shift= i==0 ? 0 : v_chroma_shift;
  295. buf->linesize[i]= picture.linesize[i];
  296. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  297. if(buf->base[i]==NULL) return -1;
  298. memset(buf->base[i], 128, size[i]);
  299. // no edge if EDGE EMU or not planar YUV
  300. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  301. buf->data[i] = buf->base[i];
  302. else
  303. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  304. }
  305. if(size[1] && !size[2])
  306. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  307. buf->width = s->width;
  308. buf->height = s->height;
  309. buf->pix_fmt= s->pix_fmt;
  310. pic->age= 256*256*256*64;
  311. }
  312. pic->type= FF_BUFFER_TYPE_INTERNAL;
  313. for(i=0; i<4; i++){
  314. pic->base[i]= buf->base[i];
  315. pic->data[i]= buf->data[i];
  316. pic->linesize[i]= buf->linesize[i];
  317. }
  318. s->internal_buffer_count++;
  319. if (s->pkt) {
  320. pic->pkt_pts = s->pkt->pts;
  321. pic->pkt_pos = s->pkt->pos;
  322. } else {
  323. pic->pkt_pts = AV_NOPTS_VALUE;
  324. pic->pkt_pos = -1;
  325. }
  326. pic->reordered_opaque= s->reordered_opaque;
  327. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  328. pic->width = s->width;
  329. pic->height = s->height;
  330. pic->format = s->pix_fmt;
  331. if(s->debug&FF_DEBUG_BUFFERS)
  332. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  333. return 0;
  334. }
  335. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  336. int i;
  337. InternalBuffer *buf, *last;
  338. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  339. assert(s->internal_buffer_count);
  340. if(s->internal_buffer){
  341. buf = NULL; /* avoids warning */
  342. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  343. buf= &((InternalBuffer*)s->internal_buffer)[i];
  344. if(buf->data[0] == pic->data[0])
  345. break;
  346. }
  347. assert(i < s->internal_buffer_count);
  348. s->internal_buffer_count--;
  349. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  350. FFSWAP(InternalBuffer, *buf, *last);
  351. }
  352. for(i=0; i<4; i++){
  353. pic->data[i]=NULL;
  354. // pic->base[i]=NULL;
  355. }
  356. //printf("R%X\n", pic->opaque);
  357. if(s->debug&FF_DEBUG_BUFFERS)
  358. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  359. }
  360. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  361. AVFrame temp_pic;
  362. int i;
  363. /* If no picture return a new buffer */
  364. if(pic->data[0] == NULL) {
  365. /* We will copy from buffer, so must be readable */
  366. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  367. return s->get_buffer(s, pic);
  368. }
  369. /* If internal buffer type return the same buffer */
  370. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  371. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  372. else pic->pkt_pts= AV_NOPTS_VALUE;
  373. pic->reordered_opaque= s->reordered_opaque;
  374. return 0;
  375. }
  376. /*
  377. * Not internal type and reget_buffer not overridden, emulate cr buffer
  378. */
  379. temp_pic = *pic;
  380. for(i = 0; i < 4; i++)
  381. pic->data[i] = pic->base[i] = NULL;
  382. pic->opaque = NULL;
  383. /* Allocate new frame */
  384. if (s->get_buffer(s, pic))
  385. return -1;
  386. /* Copy image data from old buffer to new buffer */
  387. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  388. s->height);
  389. s->release_buffer(s, &temp_pic); // Release old frame
  390. return 0;
  391. }
  392. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  393. int i;
  394. for(i=0; i<count; i++){
  395. int r= func(c, (char*)arg + i*size);
  396. if(ret) ret[i]= r;
  397. }
  398. return 0;
  399. }
  400. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  401. int i;
  402. for(i=0; i<count; i++){
  403. int r= func(c, arg, i, 0);
  404. if(ret) ret[i]= r;
  405. }
  406. return 0;
  407. }
  408. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  409. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  410. ++fmt;
  411. return fmt[0];
  412. }
  413. void avcodec_get_frame_defaults(AVFrame *pic){
  414. memset(pic, 0, sizeof(AVFrame));
  415. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  416. pic->pkt_pos = -1;
  417. pic->key_frame= 1;
  418. pic->sample_aspect_ratio = (AVRational){0, 1};
  419. pic->format = -1; /* unknown */
  420. }
  421. AVFrame *avcodec_alloc_frame(void){
  422. AVFrame *pic= av_malloc(sizeof(AVFrame));
  423. if(pic==NULL) return NULL;
  424. avcodec_get_frame_defaults(pic);
  425. return pic;
  426. }
  427. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  428. {
  429. memset(sub, 0, sizeof(*sub));
  430. sub->pts = AV_NOPTS_VALUE;
  431. }
  432. #if FF_API_AVCODEC_OPEN
  433. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  434. {
  435. return avcodec_open2(avctx, codec, NULL);
  436. }
  437. #endif
  438. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  439. {
  440. int ret = 0;
  441. AVDictionary *tmp = NULL;
  442. if (options)
  443. av_dict_copy(&tmp, *options, 0);
  444. /* If there is a user-supplied mutex locking routine, call it. */
  445. if (ff_lockmgr_cb) {
  446. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  447. return -1;
  448. }
  449. entangled_thread_counter++;
  450. if(entangled_thread_counter != 1){
  451. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  452. ret = -1;
  453. goto end;
  454. }
  455. if(avctx->codec || !codec) {
  456. ret = AVERROR(EINVAL);
  457. goto end;
  458. }
  459. if (codec->priv_data_size > 0) {
  460. if(!avctx->priv_data){
  461. avctx->priv_data = av_mallocz(codec->priv_data_size);
  462. if (!avctx->priv_data) {
  463. ret = AVERROR(ENOMEM);
  464. goto end;
  465. }
  466. if (codec->priv_class) {
  467. *(AVClass**)avctx->priv_data= codec->priv_class;
  468. av_opt_set_defaults(avctx->priv_data);
  469. }
  470. }
  471. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  472. goto free_and_end;
  473. } else {
  474. avctx->priv_data = NULL;
  475. }
  476. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  477. goto free_and_end;
  478. if(avctx->coded_width && avctx->coded_height)
  479. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  480. else if(avctx->width && avctx->height)
  481. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  482. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  483. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  484. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  485. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  486. avcodec_set_dimensions(avctx, 0, 0);
  487. }
  488. /* if the decoder init function was already called previously,
  489. free the already allocated subtitle_header before overwriting it */
  490. if (codec->decode)
  491. av_freep(&avctx->subtitle_header);
  492. #define SANE_NB_CHANNELS 128U
  493. if (avctx->channels > SANE_NB_CHANNELS) {
  494. ret = AVERROR(EINVAL);
  495. goto free_and_end;
  496. }
  497. avctx->codec = codec;
  498. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  499. avctx->codec_id == CODEC_ID_NONE) {
  500. avctx->codec_type = codec->type;
  501. avctx->codec_id = codec->id;
  502. }
  503. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  504. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  505. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  506. ret = AVERROR(EINVAL);
  507. goto free_and_end;
  508. }
  509. avctx->frame_number = 0;
  510. if (HAVE_THREADS && !avctx->thread_opaque) {
  511. ret = ff_thread_init(avctx);
  512. if (ret < 0) {
  513. goto free_and_end;
  514. }
  515. }
  516. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  517. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  518. avctx->codec->max_lowres);
  519. ret = AVERROR(EINVAL);
  520. goto free_and_end;
  521. }
  522. if (avctx->codec->encode) {
  523. int i;
  524. if (avctx->codec->sample_fmts) {
  525. if (avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
  526. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  527. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  528. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  529. break;
  530. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  531. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  532. ret = AVERROR(EINVAL);
  533. goto free_and_end;
  534. }
  535. }
  536. if (avctx->codec->supported_samplerates) {
  537. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  538. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  539. break;
  540. if (avctx->codec->supported_samplerates[i] == 0) {
  541. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  542. ret = AVERROR(EINVAL);
  543. goto free_and_end;
  544. }
  545. }
  546. if (avctx->codec->channel_layouts) {
  547. if (!avctx->channel_layout) {
  548. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  549. } else {
  550. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  551. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  552. break;
  553. if (avctx->codec->channel_layouts[i] == 0) {
  554. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  555. ret = AVERROR(EINVAL);
  556. goto free_and_end;
  557. }
  558. }
  559. }
  560. if (avctx->channel_layout && avctx->channels) {
  561. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  562. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  563. ret = AVERROR(EINVAL);
  564. goto free_and_end;
  565. }
  566. } else if (avctx->channel_layout) {
  567. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  568. }
  569. }
  570. avctx->pts_correction_num_faulty_pts =
  571. avctx->pts_correction_num_faulty_dts = 0;
  572. avctx->pts_correction_last_pts =
  573. avctx->pts_correction_last_dts = INT64_MIN;
  574. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  575. ret = avctx->codec->init(avctx);
  576. if (ret < 0) {
  577. goto free_and_end;
  578. }
  579. }
  580. ret=0;
  581. end:
  582. entangled_thread_counter--;
  583. /* Release any user-supplied mutex. */
  584. if (ff_lockmgr_cb) {
  585. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  586. }
  587. if (options) {
  588. av_dict_free(options);
  589. *options = tmp;
  590. }
  591. return ret;
  592. free_and_end:
  593. av_dict_free(&tmp);
  594. av_freep(&avctx->priv_data);
  595. avctx->codec= NULL;
  596. goto end;
  597. }
  598. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  599. const short *samples)
  600. {
  601. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  602. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  603. return -1;
  604. }
  605. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  606. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  607. avctx->frame_number++;
  608. return ret;
  609. }else
  610. return 0;
  611. }
  612. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  613. const AVFrame *pict)
  614. {
  615. if(buf_size < FF_MIN_BUFFER_SIZE){
  616. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  617. return -1;
  618. }
  619. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  620. return -1;
  621. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  622. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  623. avctx->frame_number++;
  624. emms_c(); //needed to avoid an emms_c() call before every return;
  625. return ret;
  626. }else
  627. return 0;
  628. }
  629. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  630. const AVSubtitle *sub)
  631. {
  632. int ret;
  633. if(sub->start_display_time) {
  634. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  635. return -1;
  636. }
  637. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  638. avctx->frame_number++;
  639. return ret;
  640. }
  641. /**
  642. * Attempt to guess proper monotonic timestamps for decoded video frames
  643. * which might have incorrect times. Input timestamps may wrap around, in
  644. * which case the output will as well.
  645. *
  646. * @param pts the pts field of the decoded AVPacket, as passed through
  647. * AVFrame.pkt_pts
  648. * @param dts the dts field of the decoded AVPacket
  649. * @return one of the input values, may be AV_NOPTS_VALUE
  650. */
  651. static int64_t guess_correct_pts(AVCodecContext *ctx,
  652. int64_t reordered_pts, int64_t dts)
  653. {
  654. int64_t pts = AV_NOPTS_VALUE;
  655. if (dts != AV_NOPTS_VALUE) {
  656. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  657. ctx->pts_correction_last_dts = dts;
  658. }
  659. if (reordered_pts != AV_NOPTS_VALUE) {
  660. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  661. ctx->pts_correction_last_pts = reordered_pts;
  662. }
  663. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  664. && reordered_pts != AV_NOPTS_VALUE)
  665. pts = reordered_pts;
  666. else
  667. pts = dts;
  668. return pts;
  669. }
  670. #if FF_API_VIDEO_OLD
  671. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  672. int *got_picture_ptr,
  673. const uint8_t *buf, int buf_size)
  674. {
  675. AVPacket avpkt;
  676. av_init_packet(&avpkt);
  677. avpkt.data = buf;
  678. avpkt.size = buf_size;
  679. // HACK for CorePNG to decode as normal PNG by default
  680. avpkt.flags = AV_PKT_FLAG_KEY;
  681. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  682. }
  683. #endif
  684. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  685. int *got_picture_ptr,
  686. AVPacket *avpkt)
  687. {
  688. int ret;
  689. *got_picture_ptr= 0;
  690. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  691. return -1;
  692. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  693. avctx->pkt = avpkt;
  694. if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  695. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  696. avpkt);
  697. else {
  698. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  699. avpkt);
  700. picture->pkt_dts= avpkt->dts;
  701. if(!avctx->has_b_frames){
  702. picture->pkt_pos= avpkt->pos;
  703. }
  704. //FIXME these should be under if(!avctx->has_b_frames)
  705. if (!picture->sample_aspect_ratio.num)
  706. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  707. if (!picture->width)
  708. picture->width = avctx->width;
  709. if (!picture->height)
  710. picture->height = avctx->height;
  711. if (picture->format == PIX_FMT_NONE)
  712. picture->format = avctx->pix_fmt;
  713. }
  714. emms_c(); //needed to avoid an emms_c() call before every return;
  715. if (*got_picture_ptr){
  716. avctx->frame_number++;
  717. picture->best_effort_timestamp = guess_correct_pts(avctx,
  718. picture->pkt_pts,
  719. picture->pkt_dts);
  720. }
  721. }else
  722. ret= 0;
  723. return ret;
  724. }
  725. #if FF_API_AUDIO_OLD
  726. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  727. int *frame_size_ptr,
  728. const uint8_t *buf, int buf_size)
  729. {
  730. AVPacket avpkt;
  731. av_init_packet(&avpkt);
  732. avpkt.data = buf;
  733. avpkt.size = buf_size;
  734. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  735. }
  736. #endif
  737. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  738. int *frame_size_ptr,
  739. AVPacket *avpkt)
  740. {
  741. int ret;
  742. avctx->pkt = avpkt;
  743. if (!avpkt->data && avpkt->size) {
  744. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  745. return AVERROR(EINVAL);
  746. }
  747. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  748. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  749. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  750. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  751. return -1;
  752. }
  753. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  754. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  755. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  756. return -1;
  757. }
  758. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  759. avctx->frame_number++;
  760. }else{
  761. ret= 0;
  762. *frame_size_ptr=0;
  763. }
  764. return ret;
  765. }
  766. #if FF_API_SUBTITLE_OLD
  767. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  768. int *got_sub_ptr,
  769. const uint8_t *buf, int buf_size)
  770. {
  771. AVPacket avpkt;
  772. av_init_packet(&avpkt);
  773. avpkt.data = buf;
  774. avpkt.size = buf_size;
  775. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  776. }
  777. #endif
  778. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  779. int *got_sub_ptr,
  780. AVPacket *avpkt)
  781. {
  782. int ret;
  783. avctx->pkt = avpkt;
  784. *got_sub_ptr = 0;
  785. avcodec_get_subtitle_defaults(sub);
  786. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  787. if (*got_sub_ptr)
  788. avctx->frame_number++;
  789. return ret;
  790. }
  791. void avsubtitle_free(AVSubtitle *sub)
  792. {
  793. int i;
  794. for (i = 0; i < sub->num_rects; i++)
  795. {
  796. av_freep(&sub->rects[i]->pict.data[0]);
  797. av_freep(&sub->rects[i]->pict.data[1]);
  798. av_freep(&sub->rects[i]->pict.data[2]);
  799. av_freep(&sub->rects[i]->pict.data[3]);
  800. av_freep(&sub->rects[i]->text);
  801. av_freep(&sub->rects[i]->ass);
  802. av_freep(&sub->rects[i]);
  803. }
  804. av_freep(&sub->rects);
  805. memset(sub, 0, sizeof(AVSubtitle));
  806. }
  807. av_cold int avcodec_close(AVCodecContext *avctx)
  808. {
  809. /* If there is a user-supplied mutex locking routine, call it. */
  810. if (ff_lockmgr_cb) {
  811. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  812. return -1;
  813. }
  814. entangled_thread_counter++;
  815. if(entangled_thread_counter != 1){
  816. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  817. entangled_thread_counter--;
  818. return -1;
  819. }
  820. if (HAVE_THREADS && avctx->thread_opaque)
  821. ff_thread_free(avctx);
  822. if (avctx->codec && avctx->codec->close)
  823. avctx->codec->close(avctx);
  824. avcodec_default_free_buffers(avctx);
  825. avctx->coded_frame = NULL;
  826. if (avctx->codec && avctx->codec->priv_class)
  827. av_opt_free(avctx->priv_data);
  828. av_opt_free(avctx);
  829. av_freep(&avctx->priv_data);
  830. if(avctx->codec && avctx->codec->encode)
  831. av_freep(&avctx->extradata);
  832. avctx->codec = NULL;
  833. avctx->active_thread_type = 0;
  834. entangled_thread_counter--;
  835. /* Release any user-supplied mutex. */
  836. if (ff_lockmgr_cb) {
  837. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  838. }
  839. return 0;
  840. }
  841. AVCodec *avcodec_find_encoder(enum CodecID id)
  842. {
  843. AVCodec *p, *experimental=NULL;
  844. p = first_avcodec;
  845. while (p) {
  846. if (p->encode != NULL && p->id == id) {
  847. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  848. experimental = p;
  849. } else
  850. return p;
  851. }
  852. p = p->next;
  853. }
  854. return experimental;
  855. }
  856. AVCodec *avcodec_find_encoder_by_name(const char *name)
  857. {
  858. AVCodec *p;
  859. if (!name)
  860. return NULL;
  861. p = first_avcodec;
  862. while (p) {
  863. if (p->encode != NULL && strcmp(name,p->name) == 0)
  864. return p;
  865. p = p->next;
  866. }
  867. return NULL;
  868. }
  869. AVCodec *avcodec_find_decoder(enum CodecID id)
  870. {
  871. AVCodec *p, *experimental=NULL;
  872. p = first_avcodec;
  873. while (p) {
  874. if (p->decode != NULL && p->id == id) {
  875. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  876. experimental = p;
  877. } else
  878. return p;
  879. }
  880. p = p->next;
  881. }
  882. return experimental;
  883. }
  884. AVCodec *avcodec_find_decoder_by_name(const char *name)
  885. {
  886. AVCodec *p;
  887. if (!name)
  888. return NULL;
  889. p = first_avcodec;
  890. while (p) {
  891. if (p->decode != NULL && strcmp(name,p->name) == 0)
  892. return p;
  893. p = p->next;
  894. }
  895. return NULL;
  896. }
  897. static int get_bit_rate(AVCodecContext *ctx)
  898. {
  899. int bit_rate;
  900. int bits_per_sample;
  901. switch(ctx->codec_type) {
  902. case AVMEDIA_TYPE_VIDEO:
  903. case AVMEDIA_TYPE_DATA:
  904. case AVMEDIA_TYPE_SUBTITLE:
  905. case AVMEDIA_TYPE_ATTACHMENT:
  906. bit_rate = ctx->bit_rate;
  907. break;
  908. case AVMEDIA_TYPE_AUDIO:
  909. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  910. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  911. break;
  912. default:
  913. bit_rate = 0;
  914. break;
  915. }
  916. return bit_rate;
  917. }
  918. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  919. {
  920. int i, len, ret = 0;
  921. for (i = 0; i < 4; i++) {
  922. len = snprintf(buf, buf_size,
  923. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  924. buf += len;
  925. buf_size = buf_size > len ? buf_size - len : 0;
  926. ret += len;
  927. codec_tag>>=8;
  928. }
  929. return ret;
  930. }
  931. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  932. {
  933. const char *codec_name;
  934. const char *profile = NULL;
  935. AVCodec *p;
  936. char buf1[32];
  937. int bitrate;
  938. AVRational display_aspect_ratio;
  939. if (encode)
  940. p = avcodec_find_encoder(enc->codec_id);
  941. else
  942. p = avcodec_find_decoder(enc->codec_id);
  943. if (p) {
  944. codec_name = p->name;
  945. profile = av_get_profile_name(p, enc->profile);
  946. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  947. /* fake mpeg2 transport stream codec (currently not
  948. registered) */
  949. codec_name = "mpeg2ts";
  950. } else if (enc->codec_name[0] != '\0') {
  951. codec_name = enc->codec_name;
  952. } else {
  953. /* output avi tags */
  954. char tag_buf[32];
  955. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  956. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  957. codec_name = buf1;
  958. }
  959. switch(enc->codec_type) {
  960. case AVMEDIA_TYPE_VIDEO:
  961. snprintf(buf, buf_size,
  962. "Video: %s%s",
  963. codec_name, enc->mb_decision ? " (hq)" : "");
  964. if (profile)
  965. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  966. " (%s)", profile);
  967. if (enc->pix_fmt != PIX_FMT_NONE) {
  968. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  969. ", %s",
  970. av_get_pix_fmt_name(enc->pix_fmt));
  971. }
  972. if (enc->width) {
  973. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  974. ", %dx%d",
  975. enc->width, enc->height);
  976. if (enc->sample_aspect_ratio.num) {
  977. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  978. enc->width*enc->sample_aspect_ratio.num,
  979. enc->height*enc->sample_aspect_ratio.den,
  980. 1024*1024);
  981. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  982. " [PAR %d:%d DAR %d:%d]",
  983. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  984. display_aspect_ratio.num, display_aspect_ratio.den);
  985. }
  986. if(av_log_get_level() >= AV_LOG_DEBUG){
  987. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  988. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  989. ", %d/%d",
  990. enc->time_base.num/g, enc->time_base.den/g);
  991. }
  992. }
  993. if (encode) {
  994. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  995. ", q=%d-%d", enc->qmin, enc->qmax);
  996. }
  997. break;
  998. case AVMEDIA_TYPE_AUDIO:
  999. snprintf(buf, buf_size,
  1000. "Audio: %s",
  1001. codec_name);
  1002. if (profile)
  1003. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1004. " (%s)", profile);
  1005. if (enc->sample_rate) {
  1006. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1007. ", %d Hz", enc->sample_rate);
  1008. }
  1009. av_strlcat(buf, ", ", buf_size);
  1010. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1011. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1012. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1013. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1014. }
  1015. break;
  1016. case AVMEDIA_TYPE_DATA:
  1017. snprintf(buf, buf_size, "Data: %s", codec_name);
  1018. break;
  1019. case AVMEDIA_TYPE_SUBTITLE:
  1020. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1021. break;
  1022. case AVMEDIA_TYPE_ATTACHMENT:
  1023. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1024. break;
  1025. default:
  1026. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1027. return;
  1028. }
  1029. if (encode) {
  1030. if (enc->flags & CODEC_FLAG_PASS1)
  1031. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1032. ", pass 1");
  1033. if (enc->flags & CODEC_FLAG_PASS2)
  1034. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1035. ", pass 2");
  1036. }
  1037. bitrate = get_bit_rate(enc);
  1038. if (bitrate != 0) {
  1039. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1040. ", %d kb/s", bitrate / 1000);
  1041. }
  1042. }
  1043. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1044. {
  1045. const AVProfile *p;
  1046. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1047. return NULL;
  1048. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1049. if (p->profile == profile)
  1050. return p->name;
  1051. return NULL;
  1052. }
  1053. unsigned avcodec_version( void )
  1054. {
  1055. return LIBAVCODEC_VERSION_INT;
  1056. }
  1057. const char *avcodec_configuration(void)
  1058. {
  1059. return FFMPEG_CONFIGURATION;
  1060. }
  1061. const char *avcodec_license(void)
  1062. {
  1063. #define LICENSE_PREFIX "libavcodec license: "
  1064. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1065. }
  1066. void avcodec_init(void)
  1067. {
  1068. static int initialized = 0;
  1069. if (initialized != 0)
  1070. return;
  1071. initialized = 1;
  1072. dsputil_static_init();
  1073. }
  1074. void avcodec_flush_buffers(AVCodecContext *avctx)
  1075. {
  1076. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1077. ff_thread_flush(avctx);
  1078. else if(avctx->codec->flush)
  1079. avctx->codec->flush(avctx);
  1080. }
  1081. void avcodec_default_free_buffers(AVCodecContext *s){
  1082. int i, j;
  1083. if(s->internal_buffer==NULL) return;
  1084. if (s->internal_buffer_count)
  1085. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  1086. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1087. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  1088. for(j=0; j<4; j++){
  1089. av_freep(&buf->base[j]);
  1090. buf->data[j]= NULL;
  1091. }
  1092. }
  1093. av_freep(&s->internal_buffer);
  1094. s->internal_buffer_count=0;
  1095. }
  1096. #if FF_API_OLD_FF_PICT_TYPES
  1097. char av_get_pict_type_char(int pict_type){
  1098. return av_get_picture_type_char(pict_type);
  1099. }
  1100. #endif
  1101. int av_get_bits_per_sample(enum CodecID codec_id){
  1102. switch(codec_id){
  1103. case CODEC_ID_ADPCM_SBPRO_2:
  1104. return 2;
  1105. case CODEC_ID_ADPCM_SBPRO_3:
  1106. return 3;
  1107. case CODEC_ID_ADPCM_SBPRO_4:
  1108. case CODEC_ID_ADPCM_CT:
  1109. case CODEC_ID_ADPCM_IMA_WAV:
  1110. case CODEC_ID_ADPCM_MS:
  1111. case CODEC_ID_ADPCM_YAMAHA:
  1112. return 4;
  1113. case CODEC_ID_ADPCM_G722:
  1114. case CODEC_ID_PCM_ALAW:
  1115. case CODEC_ID_PCM_MULAW:
  1116. case CODEC_ID_PCM_S8:
  1117. case CODEC_ID_PCM_U8:
  1118. case CODEC_ID_PCM_ZORK:
  1119. return 8;
  1120. case CODEC_ID_PCM_S16BE:
  1121. case CODEC_ID_PCM_S16LE:
  1122. case CODEC_ID_PCM_S16LE_PLANAR:
  1123. case CODEC_ID_PCM_U16BE:
  1124. case CODEC_ID_PCM_U16LE:
  1125. return 16;
  1126. case CODEC_ID_PCM_S24DAUD:
  1127. case CODEC_ID_PCM_S24BE:
  1128. case CODEC_ID_PCM_S24LE:
  1129. case CODEC_ID_PCM_U24BE:
  1130. case CODEC_ID_PCM_U24LE:
  1131. return 24;
  1132. case CODEC_ID_PCM_S32BE:
  1133. case CODEC_ID_PCM_S32LE:
  1134. case CODEC_ID_PCM_U32BE:
  1135. case CODEC_ID_PCM_U32LE:
  1136. case CODEC_ID_PCM_F32BE:
  1137. case CODEC_ID_PCM_F32LE:
  1138. return 32;
  1139. case CODEC_ID_PCM_F64BE:
  1140. case CODEC_ID_PCM_F64LE:
  1141. return 64;
  1142. default:
  1143. return 0;
  1144. }
  1145. }
  1146. #if FF_API_OLD_SAMPLE_FMT
  1147. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1148. return av_get_bytes_per_sample(sample_fmt) << 3;
  1149. }
  1150. #endif
  1151. #if !HAVE_THREADS
  1152. int ff_thread_init(AVCodecContext *s){
  1153. return -1;
  1154. }
  1155. #endif
  1156. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1157. {
  1158. unsigned int n = 0;
  1159. while(v >= 0xff) {
  1160. *s++ = 0xff;
  1161. v -= 0xff;
  1162. n++;
  1163. }
  1164. *s = v;
  1165. n++;
  1166. return n;
  1167. }
  1168. #if LIBAVCODEC_VERSION_MAJOR < 53
  1169. #include "libavutil/parseutils.h"
  1170. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  1171. {
  1172. return av_parse_video_size(width_ptr, height_ptr, str);
  1173. }
  1174. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  1175. {
  1176. return av_parse_video_rate(frame_rate, arg);
  1177. }
  1178. #endif
  1179. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1180. int i;
  1181. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1182. return i;
  1183. }
  1184. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1185. {
  1186. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1187. "version to the newest one from Git. If the problem still "
  1188. "occurs, it means that your file has a feature which has not "
  1189. "been implemented.\n", feature);
  1190. if(want_sample)
  1191. av_log_ask_for_sample(avc, NULL);
  1192. }
  1193. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1194. {
  1195. va_list argument_list;
  1196. va_start(argument_list, msg);
  1197. if (msg)
  1198. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1199. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1200. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1201. "and contact the ffmpeg-devel mailing list.\n");
  1202. va_end(argument_list);
  1203. }
  1204. static AVHWAccel *first_hwaccel = NULL;
  1205. void av_register_hwaccel(AVHWAccel *hwaccel)
  1206. {
  1207. AVHWAccel **p = &first_hwaccel;
  1208. while (*p)
  1209. p = &(*p)->next;
  1210. *p = hwaccel;
  1211. hwaccel->next = NULL;
  1212. }
  1213. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1214. {
  1215. return hwaccel ? hwaccel->next : first_hwaccel;
  1216. }
  1217. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1218. {
  1219. AVHWAccel *hwaccel=NULL;
  1220. while((hwaccel= av_hwaccel_next(hwaccel))){
  1221. if ( hwaccel->id == codec_id
  1222. && hwaccel->pix_fmt == pix_fmt)
  1223. return hwaccel;
  1224. }
  1225. return NULL;
  1226. }
  1227. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1228. {
  1229. if (ff_lockmgr_cb) {
  1230. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1231. return -1;
  1232. }
  1233. ff_lockmgr_cb = cb;
  1234. if (ff_lockmgr_cb) {
  1235. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1236. return -1;
  1237. }
  1238. return 0;
  1239. }
  1240. unsigned int ff_toupper4(unsigned int x)
  1241. {
  1242. return toupper( x &0xFF)
  1243. + (toupper((x>>8 )&0xFF)<<8 )
  1244. + (toupper((x>>16)&0xFF)<<16)
  1245. + (toupper((x>>24)&0xFF)<<24);
  1246. }
  1247. #if !HAVE_PTHREADS
  1248. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1249. {
  1250. f->owner = avctx;
  1251. return avctx->get_buffer(avctx, f);
  1252. }
  1253. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1254. {
  1255. f->owner->release_buffer(f->owner, f);
  1256. }
  1257. void ff_thread_finish_setup(AVCodecContext *avctx)
  1258. {
  1259. }
  1260. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1261. {
  1262. }
  1263. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1264. {
  1265. }
  1266. #endif
  1267. #if FF_API_THREAD_INIT
  1268. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1269. {
  1270. s->thread_count = thread_count;
  1271. return ff_thread_init(s);
  1272. }
  1273. void avcodec_thread_free(AVCodecContext *s)
  1274. {
  1275. #if HAVE_THREADS
  1276. ff_thread_free(s);
  1277. #endif
  1278. }
  1279. #endif