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.

678 lines
17KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. ptr = av_malloc(size);
  26. if (!ptr)
  27. return NULL;
  28. memset(ptr, 0, size);
  29. return ptr;
  30. }
  31. /* allocation of static arrays - do not use for normal allocation */
  32. static unsigned int last_static = 0;
  33. static char*** array_static = NULL;
  34. static const unsigned int grow_static = 64; // ^2
  35. void *__av_mallocz_static(void** location, unsigned int size)
  36. {
  37. int l = (last_static + grow_static) & ~(grow_static - 1);
  38. void *ptr = av_mallocz(size);
  39. if (!ptr)
  40. return NULL;
  41. if (location)
  42. {
  43. if (l > last_static)
  44. array_static = realloc(array_static, l);
  45. array_static[last_static++] = (char**) location;
  46. *location = ptr;
  47. }
  48. return ptr;
  49. }
  50. /* free all static arrays and reset pointers to 0 */
  51. void av_free_static()
  52. {
  53. if (array_static)
  54. {
  55. unsigned i;
  56. for (i = 0; i < last_static; i++)
  57. {
  58. free(*array_static[i]);
  59. *array_static[i] = NULL;
  60. }
  61. free(array_static);
  62. array_static = 0;
  63. }
  64. last_static = 0;
  65. }
  66. /* cannot call it directly because of 'void **' casting is not automatic */
  67. void __av_freep(void **ptr)
  68. {
  69. av_free(*ptr);
  70. *ptr = NULL;
  71. }
  72. /* encoder management */
  73. AVCodec *first_avcodec;
  74. void register_avcodec(AVCodec *format)
  75. {
  76. AVCodec **p;
  77. p = &first_avcodec;
  78. while (*p != NULL) p = &(*p)->next;
  79. *p = format;
  80. format->next = NULL;
  81. }
  82. void avcodec_get_chroma_sub_sample(int fmt, int *h_shift, int *v_shift){
  83. switch(fmt){
  84. case PIX_FMT_YUV410P:
  85. *h_shift=2;
  86. *v_shift=2;
  87. break;
  88. case PIX_FMT_YUV420P:
  89. *h_shift=1;
  90. *v_shift=1;
  91. break;
  92. case PIX_FMT_YUV411P:
  93. *h_shift=2;
  94. *v_shift=0;
  95. break;
  96. case PIX_FMT_YUV422P:
  97. case PIX_FMT_YUV422:
  98. *h_shift=1;
  99. *v_shift=0;
  100. break;
  101. default: //RGB/...
  102. *h_shift=0;
  103. *v_shift=0;
  104. break;
  105. }
  106. }
  107. typedef struct DefaultPicOpaque{
  108. int last_pic_num;
  109. uint8_t *data[4];
  110. }DefaultPicOpaque;
  111. int avcodec_default_get_buffer(AVCodecContext *s, AVVideoFrame *pic){
  112. int i;
  113. const int width = s->width;
  114. const int height= s->height;
  115. DefaultPicOpaque *opaque;
  116. if(pic->opaque){
  117. opaque= (DefaultPicOpaque *)pic->opaque;
  118. for(i=0; i<3; i++)
  119. pic->data[i]= opaque->data[i];
  120. // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
  121. pic->age= pic->coded_picture_number - opaque->last_pic_num;
  122. opaque->last_pic_num= pic->coded_picture_number;
  123. //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
  124. }else{
  125. int align, h_chroma_shift, v_chroma_shift;
  126. int w, h, pixel_size;
  127. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  128. switch(s->pix_fmt){
  129. case PIX_FMT_YUV422:
  130. pixel_size=2;
  131. break;
  132. case PIX_FMT_RGB24:
  133. case PIX_FMT_BGR24:
  134. pixel_size=3;
  135. break;
  136. case PIX_FMT_BGRA32:
  137. case PIX_FMT_RGBA32:
  138. pixel_size=4;
  139. break;
  140. default:
  141. pixel_size=1;
  142. }
  143. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  144. else align=15;
  145. w= (width +align)&~align;
  146. h= (height+align)&~align;
  147. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  148. w+= EDGE_WIDTH*2;
  149. h+= EDGE_WIDTH*2;
  150. }
  151. opaque= av_mallocz(sizeof(DefaultPicOpaque));
  152. if(opaque==NULL) return -1;
  153. pic->opaque= opaque;
  154. opaque->last_pic_num= -256*256*256*64;
  155. for(i=0; i<3; i++){
  156. int h_shift= i==0 ? 0 : h_chroma_shift;
  157. int v_shift= i==0 ? 0 : v_chroma_shift;
  158. pic->linesize[i]= pixel_size*w>>h_shift;
  159. pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  160. if(pic->base[i]==NULL) return -1;
  161. memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
  162. if(s->flags&CODEC_FLAG_EMU_EDGE)
  163. pic->data[i] = pic->base[i];
  164. else
  165. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift);
  166. opaque->data[i]= pic->data[i];
  167. }
  168. pic->age= 256*256*256*64;
  169. }
  170. return 0;
  171. }
  172. void avcodec_default_release_buffer(AVCodecContext *s, AVVideoFrame *pic){
  173. int i;
  174. for(i=0; i<3; i++)
  175. pic->data[i]=NULL;
  176. //printf("R%X\n", pic->opaque);
  177. }
  178. void avcodec_get_context_defaults(AVCodecContext *s){
  179. s->bit_rate= 800*1000;
  180. s->bit_rate_tolerance= s->bit_rate*10;
  181. s->qmin= 2;
  182. s->qmax= 31;
  183. s->rc_eq= "tex^qComp";
  184. s->qcompress= 0.5;
  185. s->max_qdiff= 3;
  186. s->b_quant_factor=1.25;
  187. s->b_quant_offset=1.25;
  188. s->i_quant_factor=-0.8;
  189. s->i_quant_offset=0.0;
  190. s->error_concealment= 3;
  191. s->error_resilience= 1;
  192. s->workaround_bugs= FF_BUG_AUTODETECT;
  193. s->frame_rate = 25 * FRAME_RATE_BASE;
  194. s->gop_size= 50;
  195. s->me_method= ME_EPZS;
  196. s->get_buffer= avcodec_default_get_buffer;
  197. s->release_buffer= avcodec_default_release_buffer;
  198. }
  199. /**
  200. * allocates a AVCodecContext and set it to defaults.
  201. * this can be deallocated by simply calling free()
  202. */
  203. AVCodecContext *avcodec_alloc_context(void){
  204. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  205. if(avctx==NULL) return NULL;
  206. avcodec_get_context_defaults(avctx);
  207. return avctx;
  208. }
  209. /**
  210. * allocates a AVPicture and set it to defaults.
  211. * this can be deallocated by simply calling free()
  212. */
  213. AVVideoFrame *avcodec_alloc_picture(void){
  214. AVVideoFrame *pic= av_mallocz(sizeof(AVVideoFrame));
  215. return pic;
  216. }
  217. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  218. {
  219. int ret;
  220. avctx->codec = codec;
  221. avctx->frame_number = 0;
  222. if (codec->priv_data_size > 0) {
  223. avctx->priv_data = av_mallocz(codec->priv_data_size);
  224. if (!avctx->priv_data)
  225. return -ENOMEM;
  226. } else {
  227. avctx->priv_data = NULL;
  228. }
  229. ret = avctx->codec->init(avctx);
  230. if (ret < 0) {
  231. av_freep(&avctx->priv_data);
  232. return ret;
  233. }
  234. return 0;
  235. }
  236. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  237. const short *samples)
  238. {
  239. int ret;
  240. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  241. avctx->frame_number++;
  242. return ret;
  243. }
  244. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  245. const AVVideoFrame *pict)
  246. {
  247. int ret;
  248. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  249. emms_c(); //needed to avoid a emms_c() call before every return;
  250. avctx->frame_number++;
  251. return ret;
  252. }
  253. /* decode a frame. return -1 if error, otherwise return the number of
  254. bytes used. If no frame could be decompressed, *got_picture_ptr is
  255. zero. Otherwise, it is non zero */
  256. int avcodec_decode_video(AVCodecContext *avctx, AVVideoFrame *picture,
  257. int *got_picture_ptr,
  258. UINT8 *buf, int buf_size)
  259. {
  260. int ret;
  261. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  262. buf, buf_size);
  263. emms_c(); //needed to avoid a emms_c() call before every return;
  264. if (*got_picture_ptr)
  265. avctx->frame_number++;
  266. return ret;
  267. }
  268. /* decode an audio frame. return -1 if error, otherwise return the
  269. *number of bytes used. If no frame could be decompressed,
  270. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  271. *size in BYTES. */
  272. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  273. int *frame_size_ptr,
  274. UINT8 *buf, int buf_size)
  275. {
  276. int ret;
  277. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  278. buf, buf_size);
  279. avctx->frame_number++;
  280. return ret;
  281. }
  282. int avcodec_close(AVCodecContext *avctx)
  283. {
  284. if (avctx->codec->close)
  285. avctx->codec->close(avctx);
  286. av_freep(&avctx->priv_data);
  287. avctx->codec = NULL;
  288. return 0;
  289. }
  290. AVCodec *avcodec_find_encoder(enum CodecID id)
  291. {
  292. AVCodec *p;
  293. p = first_avcodec;
  294. while (p) {
  295. if (p->encode != NULL && p->id == id)
  296. return p;
  297. p = p->next;
  298. }
  299. return NULL;
  300. }
  301. AVCodec *avcodec_find_encoder_by_name(const char *name)
  302. {
  303. AVCodec *p;
  304. p = first_avcodec;
  305. while (p) {
  306. if (p->encode != NULL && strcmp(name,p->name) == 0)
  307. return p;
  308. p = p->next;
  309. }
  310. return NULL;
  311. }
  312. AVCodec *avcodec_find_decoder(enum CodecID id)
  313. {
  314. AVCodec *p;
  315. p = first_avcodec;
  316. while (p) {
  317. if (p->decode != NULL && p->id == id)
  318. return p;
  319. p = p->next;
  320. }
  321. return NULL;
  322. }
  323. AVCodec *avcodec_find_decoder_by_name(const char *name)
  324. {
  325. AVCodec *p;
  326. p = first_avcodec;
  327. while (p) {
  328. if (p->decode != NULL && strcmp(name,p->name) == 0)
  329. return p;
  330. p = p->next;
  331. }
  332. return NULL;
  333. }
  334. AVCodec *avcodec_find(enum CodecID id)
  335. {
  336. AVCodec *p;
  337. p = first_avcodec;
  338. while (p) {
  339. if (p->id == id)
  340. return p;
  341. p = p->next;
  342. }
  343. return NULL;
  344. }
  345. const char *pix_fmt_str[] = {
  346. "yuv420p",
  347. "yuv422",
  348. "rgb24",
  349. "bgr24",
  350. "yuv422p",
  351. "yuv444p",
  352. "rgba32",
  353. "bgra32",
  354. "yuv410p",
  355. "yuv411p",
  356. };
  357. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  358. {
  359. const char *codec_name;
  360. AVCodec *p;
  361. char buf1[32];
  362. char channels_str[100];
  363. int bitrate;
  364. if (encode)
  365. p = avcodec_find_encoder(enc->codec_id);
  366. else
  367. p = avcodec_find_decoder(enc->codec_id);
  368. if (p) {
  369. codec_name = p->name;
  370. } else if (enc->codec_name[0] != '\0') {
  371. codec_name = enc->codec_name;
  372. } else {
  373. /* output avi tags */
  374. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  375. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  376. enc->codec_tag & 0xff,
  377. (enc->codec_tag >> 8) & 0xff,
  378. (enc->codec_tag >> 16) & 0xff,
  379. (enc->codec_tag >> 24) & 0xff);
  380. } else {
  381. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  382. }
  383. codec_name = buf1;
  384. }
  385. switch(enc->codec_type) {
  386. case CODEC_TYPE_VIDEO:
  387. snprintf(buf, buf_size,
  388. "Video: %s%s",
  389. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  390. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  391. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  392. ", %s",
  393. pix_fmt_str[enc->pix_fmt]);
  394. }
  395. if (enc->width) {
  396. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  397. ", %dx%d, %0.2f fps",
  398. enc->width, enc->height,
  399. (float)enc->frame_rate / FRAME_RATE_BASE);
  400. }
  401. if (encode) {
  402. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  403. ", q=%d-%d", enc->qmin, enc->qmax);
  404. }
  405. bitrate = enc->bit_rate;
  406. break;
  407. case CODEC_TYPE_AUDIO:
  408. snprintf(buf, buf_size,
  409. "Audio: %s",
  410. codec_name);
  411. switch (enc->channels) {
  412. case 1:
  413. strcpy(channels_str, "mono");
  414. break;
  415. case 2:
  416. strcpy(channels_str, "stereo");
  417. break;
  418. case 6:
  419. strcpy(channels_str, "5:1");
  420. break;
  421. default:
  422. sprintf(channels_str, "%d channels", enc->channels);
  423. break;
  424. }
  425. if (enc->sample_rate) {
  426. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  427. ", %d Hz, %s",
  428. enc->sample_rate,
  429. channels_str);
  430. }
  431. /* for PCM codecs, compute bitrate directly */
  432. switch(enc->codec_id) {
  433. case CODEC_ID_PCM_S16LE:
  434. case CODEC_ID_PCM_S16BE:
  435. case CODEC_ID_PCM_U16LE:
  436. case CODEC_ID_PCM_U16BE:
  437. bitrate = enc->sample_rate * enc->channels * 16;
  438. break;
  439. case CODEC_ID_PCM_S8:
  440. case CODEC_ID_PCM_U8:
  441. case CODEC_ID_PCM_ALAW:
  442. case CODEC_ID_PCM_MULAW:
  443. bitrate = enc->sample_rate * enc->channels * 8;
  444. break;
  445. default:
  446. bitrate = enc->bit_rate;
  447. break;
  448. }
  449. break;
  450. default:
  451. av_abort();
  452. }
  453. if (encode) {
  454. if (enc->flags & CODEC_FLAG_PASS1)
  455. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  456. ", pass 1");
  457. if (enc->flags & CODEC_FLAG_PASS2)
  458. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  459. ", pass 2");
  460. }
  461. if (bitrate != 0) {
  462. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  463. ", %d kb/s", bitrate / 1000);
  464. }
  465. }
  466. /* Picture field are filled with 'ptr' addresses */
  467. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  468. int pix_fmt, int width, int height)
  469. {
  470. int size;
  471. size = width * height;
  472. switch(pix_fmt) {
  473. case PIX_FMT_YUV420P:
  474. picture->data[0] = ptr;
  475. picture->data[1] = picture->data[0] + size;
  476. picture->data[2] = picture->data[1] + size / 4;
  477. picture->linesize[0] = width;
  478. picture->linesize[1] = width / 2;
  479. picture->linesize[2] = width / 2;
  480. break;
  481. case PIX_FMT_YUV422P:
  482. picture->data[0] = ptr;
  483. picture->data[1] = picture->data[0] + size;
  484. picture->data[2] = picture->data[1] + size / 2;
  485. picture->linesize[0] = width;
  486. picture->linesize[1] = width / 2;
  487. picture->linesize[2] = width / 2;
  488. break;
  489. case PIX_FMT_YUV444P:
  490. picture->data[0] = ptr;
  491. picture->data[1] = picture->data[0] + size;
  492. picture->data[2] = picture->data[1] + size;
  493. picture->linesize[0] = width;
  494. picture->linesize[1] = width;
  495. picture->linesize[2] = width;
  496. break;
  497. case PIX_FMT_RGB24:
  498. case PIX_FMT_BGR24:
  499. picture->data[0] = ptr;
  500. picture->data[1] = NULL;
  501. picture->data[2] = NULL;
  502. picture->linesize[0] = width * 3;
  503. break;
  504. case PIX_FMT_RGBA32:
  505. case PIX_FMT_BGRA32:
  506. picture->data[0] = ptr;
  507. picture->data[1] = NULL;
  508. picture->data[2] = NULL;
  509. picture->linesize[0] = width * 4;
  510. break;
  511. case PIX_FMT_YUV422:
  512. picture->data[0] = ptr;
  513. picture->data[1] = NULL;
  514. picture->data[2] = NULL;
  515. picture->linesize[0] = width * 2;
  516. break;
  517. default:
  518. picture->data[0] = NULL;
  519. picture->data[1] = NULL;
  520. picture->data[2] = NULL;
  521. break;
  522. }
  523. }
  524. int avpicture_get_size(int pix_fmt, int width, int height)
  525. {
  526. int size;
  527. size = width * height;
  528. switch(pix_fmt) {
  529. case PIX_FMT_YUV420P:
  530. size = (size * 3) / 2;
  531. break;
  532. case PIX_FMT_YUV422P:
  533. size = (size * 2);
  534. break;
  535. case PIX_FMT_YUV444P:
  536. size = (size * 3);
  537. break;
  538. case PIX_FMT_RGB24:
  539. case PIX_FMT_BGR24:
  540. size = (size * 3);
  541. break;
  542. case PIX_FMT_RGBA32:
  543. case PIX_FMT_BGRA32:
  544. size = (size * 4);
  545. break;
  546. case PIX_FMT_YUV422:
  547. size = (size * 2);
  548. break;
  549. default:
  550. size = -1;
  551. break;
  552. }
  553. return size;
  554. }
  555. unsigned avcodec_version( void )
  556. {
  557. return LIBAVCODEC_VERSION_INT;
  558. }
  559. unsigned avcodec_build( void )
  560. {
  561. return LIBAVCODEC_BUILD;
  562. }
  563. /* must be called before any other functions */
  564. void avcodec_init(void)
  565. {
  566. static int inited = 0;
  567. if (inited != 0)
  568. return;
  569. inited = 1;
  570. //dsputil_init();
  571. }
  572. /* this should be called after seeking and before trying to decode the next frame */
  573. void avcodec_flush_buffers(AVCodecContext *avctx)
  574. {
  575. MpegEncContext *s = avctx->priv_data;
  576. s->num_available_buffers=0;
  577. }
  578. static int raw_encode_init(AVCodecContext *s)
  579. {
  580. return 0;
  581. }
  582. static int raw_decode_frame(AVCodecContext *avctx,
  583. void *data, int *data_size,
  584. UINT8 *buf, int buf_size)
  585. {
  586. return -1;
  587. }
  588. static int raw_encode_frame(AVCodecContext *avctx,
  589. unsigned char *frame, int buf_size, void *data)
  590. {
  591. return -1;
  592. }
  593. AVCodec rawvideo_codec = {
  594. "rawvideo",
  595. CODEC_TYPE_VIDEO,
  596. CODEC_ID_RAWVIDEO,
  597. 0,
  598. raw_encode_init,
  599. raw_encode_frame,
  600. NULL,
  601. raw_decode_frame,
  602. };