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.

1358 lines
40KB

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