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.

1190 lines
34KB

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