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.

456 lines
17KB

  1. /*
  2. * Raw Video Decoder
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Raw Video Decoder
  24. */
  25. #include "avcodec.h"
  26. #include "bswapdsp.h"
  27. #include "get_bits.h"
  28. #include "internal.h"
  29. #include "raw.h"
  30. #include "libavutil/avassert.h"
  31. #include "libavutil/buffer.h"
  32. #include "libavutil/common.h"
  33. #include "libavutil/intreadwrite.h"
  34. #include "libavutil/imgutils.h"
  35. #include "libavutil/opt.h"
  36. typedef struct RawVideoContext {
  37. AVClass *av_class;
  38. AVBufferRef *palette;
  39. int frame_size; /* size of the frame in bytes */
  40. int flip;
  41. int is_1_2_4_8_bpp; // 1, 2, 4 and 8 bpp in avi/mov
  42. int is_yuv2;
  43. int is_lt_16bpp; // 16bpp pixfmt and bits_per_coded_sample < 16
  44. int tff;
  45. BswapDSPContext bbdsp;
  46. void *bitstream_buf;
  47. unsigned int bitstream_buf_size;
  48. } RawVideoContext;
  49. static const AVOption options[]={
  50. {"top", "top field first", offsetof(RawVideoContext, tff), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM},
  51. {NULL}
  52. };
  53. static const AVClass rawdec_class = {
  54. .class_name = "rawdec",
  55. .option = options,
  56. .version = LIBAVUTIL_VERSION_INT,
  57. };
  58. static av_cold int raw_init_decoder(AVCodecContext *avctx)
  59. {
  60. RawVideoContext *context = avctx->priv_data;
  61. const AVPixFmtDescriptor *desc;
  62. ff_bswapdsp_init(&context->bbdsp);
  63. if ( avctx->codec_tag == MKTAG('r','a','w',' ')
  64. || avctx->codec_tag == MKTAG('N','O','1','6'))
  65. avctx->pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_mov,
  66. avctx->bits_per_coded_sample);
  67. else if (avctx->codec_tag == MKTAG('W', 'R', 'A', 'W'))
  68. avctx->pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_avi,
  69. avctx->bits_per_coded_sample);
  70. else if (avctx->codec_tag && (avctx->codec_tag & 0xFFFFFF) != MKTAG('B','I','T', 0))
  71. avctx->pix_fmt = avpriv_find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
  72. else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
  73. avctx->pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_avi,
  74. avctx->bits_per_coded_sample);
  75. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  76. if (!desc) {
  77. av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
  78. return AVERROR(EINVAL);
  79. }
  80. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL)) {
  81. context->palette = av_buffer_alloc(AVPALETTE_SIZE);
  82. if (!context->palette)
  83. return AVERROR(ENOMEM);
  84. if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  85. avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
  86. else {
  87. memset(context->palette->data, 0, AVPALETTE_SIZE);
  88. if (avctx->bits_per_coded_sample == 1)
  89. memset(context->palette->data, 0xff, 4);
  90. }
  91. }
  92. if ((avctx->extradata_size >= 9 &&
  93. !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
  94. avctx->codec_tag == MKTAG('c','y','u','v') ||
  95. avctx->codec_tag == MKTAG(3, 0, 0, 0) ||
  96. avctx->codec_tag == MKTAG('W','R','A','W'))
  97. context->flip = 1;
  98. if (avctx->codec_tag == AV_RL32("yuv2") &&
  99. avctx->pix_fmt == AV_PIX_FMT_YUYV422)
  100. context->is_yuv2 = 1;
  101. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && avctx->bits_per_coded_sample == 1)
  102. avctx->pix_fmt = AV_PIX_FMT_NONE;
  103. return 0;
  104. }
  105. static void flip(AVCodecContext *avctx, AVFrame *frame)
  106. {
  107. frame->data[0] += frame->linesize[0] * (avctx->height - 1);
  108. frame->linesize[0] *= -1;
  109. }
  110. /*
  111. * Scale sample to 16-bit resolution
  112. */
  113. #define SCALE16(x, bits) (((x) << (16 - (bits))) | ((x) >> (2 * (bits) - 16)))
  114. /**
  115. * Scale buffer to 16 bits per coded sample resolution
  116. */
  117. #define MKSCALE16(name, r16, w16) \
  118. static void name(AVCodecContext *avctx, uint8_t * dst, const uint8_t *buf, int buf_size, int packed) \
  119. { \
  120. int i; \
  121. if (!packed) { \
  122. for (i = 0; i + 1 < buf_size; i += 2) \
  123. w16(dst + i, SCALE16(r16(buf + i), avctx->bits_per_coded_sample)); \
  124. } else { \
  125. GetBitContext gb; \
  126. init_get_bits(&gb, buf, buf_size * 8); \
  127. for (i = 0; i < avctx->width * avctx->height; i++) { \
  128. int sample = get_bits(&gb, avctx->bits_per_coded_sample); \
  129. w16(dst + i*2, SCALE16(sample, avctx->bits_per_coded_sample)); \
  130. } \
  131. } \
  132. }
  133. MKSCALE16(scale16be, AV_RB16, AV_WB16)
  134. MKSCALE16(scale16le, AV_RL16, AV_WL16)
  135. static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
  136. AVPacket *avpkt)
  137. {
  138. const AVPixFmtDescriptor *desc;
  139. RawVideoContext *context = avctx->priv_data;
  140. const uint8_t *buf = avpkt->data;
  141. int buf_size = avpkt->size;
  142. int linesize_align = 4;
  143. int avpkt_stride;
  144. int res, len;
  145. int need_copy;
  146. AVFrame *frame = data;
  147. if (avctx->height <= 0) {
  148. av_log(avctx, AV_LOG_ERROR, "height is not set\n");
  149. return AVERROR_INVALIDDATA;
  150. }
  151. avpkt_stride = avpkt->size / avctx->height;
  152. if (avpkt_stride == 0) {
  153. av_log(avctx, AV_LOG_ERROR, "Packet too small (%d) height (%d)\n", avpkt->size, avctx->height);
  154. return AVERROR_INVALIDDATA;
  155. }
  156. if (avctx->pix_fmt == AV_PIX_FMT_NONE &&
  157. avctx->bits_per_coded_sample == 1 &&
  158. avctx->frame_number == 0 &&
  159. context->palette &&
  160. AV_RB64(context->palette->data) == 0xFFFFFFFF00000000
  161. ) {
  162. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  163. if (!pal) {
  164. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  165. } else
  166. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  167. }
  168. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  169. if ((avctx->bits_per_coded_sample == 8 || avctx->bits_per_coded_sample == 4
  170. || avctx->bits_per_coded_sample == 2 || avctx->bits_per_coded_sample == 1) &&
  171. (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_MONOWHITE) &&
  172. (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))) {
  173. context->is_1_2_4_8_bpp = 1;
  174. if (avctx->bits_per_coded_sample == 1 && avctx->pix_fmt == AV_PIX_FMT_MONOWHITE) {
  175. int row_bytes = avctx->width / 8 + (avctx->width & 7 ? 1 : 0);
  176. context->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
  177. FFALIGN(row_bytes, 16) * 8,
  178. avctx->height, 1);
  179. } else
  180. context->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
  181. FFALIGN(avctx->width, 16),
  182. avctx->height, 1);
  183. } else {
  184. context->is_lt_16bpp = av_get_bits_per_pixel(desc) == 16 && avctx->bits_per_coded_sample && avctx->bits_per_coded_sample < 16;
  185. context->frame_size = av_image_get_buffer_size(avctx->pix_fmt, avctx->width,
  186. avctx->height, 1);
  187. }
  188. if (context->frame_size < 0)
  189. return context->frame_size;
  190. need_copy = !avpkt->buf || context->is_1_2_4_8_bpp || context->is_yuv2 || context->is_lt_16bpp;
  191. frame->pict_type = AV_PICTURE_TYPE_I;
  192. frame->key_frame = 1;
  193. res = ff_decode_frame_props(avctx, frame);
  194. if (res < 0)
  195. return res;
  196. av_frame_set_pkt_pos (frame, avctx->internal->pkt->pos);
  197. av_frame_set_pkt_duration(frame, avctx->internal->pkt->duration);
  198. if (context->tff >= 0) {
  199. frame->interlaced_frame = 1;
  200. frame->top_field_first = context->tff;
  201. }
  202. if ((res = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  203. return res;
  204. if (need_copy)
  205. frame->buf[0] = av_buffer_alloc(FFMAX(context->frame_size, buf_size));
  206. else
  207. frame->buf[0] = av_buffer_ref(avpkt->buf);
  208. if (!frame->buf[0])
  209. return AVERROR(ENOMEM);
  210. // 1, 2, 4 and 8 bpp in avi/mov
  211. if (context->is_1_2_4_8_bpp) {
  212. int i, j, row_pix = 0;
  213. uint8_t *dst = frame->buf[0]->data;
  214. buf_size = context->frame_size -
  215. (avctx->pix_fmt == AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
  216. if (avctx->bits_per_coded_sample == 8 || avctx->pix_fmt == AV_PIX_FMT_MONOWHITE) {
  217. int pix_per_byte = avctx->pix_fmt == AV_PIX_FMT_MONOWHITE ? 8 : 1;
  218. for (i = 0, j = 0; j < buf_size && i<avpkt->size; i++, j++) {
  219. dst[j] = buf[i];
  220. row_pix += pix_per_byte;
  221. if (row_pix >= avctx->width) {
  222. i += avpkt_stride - (i % avpkt_stride) - 1;
  223. j += 16 - (j % 16) - 1;
  224. row_pix = 0;
  225. }
  226. }
  227. } else if (avctx->bits_per_coded_sample == 4) {
  228. for (i = 0, j = 0; 2 * j + 1 < buf_size && i<avpkt->size; i++, j++) {
  229. dst[2 * j + 0] = buf[i] >> 4;
  230. dst[2 * j + 1] = buf[i] & 15;
  231. row_pix += 2;
  232. if (row_pix >= avctx->width) {
  233. i += avpkt_stride - (i % avpkt_stride) - 1;
  234. j += 8 - (j % 8) - 1;
  235. row_pix = 0;
  236. }
  237. }
  238. } else if (avctx->bits_per_coded_sample == 2) {
  239. for (i = 0, j = 0; 4 * j + 3 < buf_size && i<avpkt->size; i++, j++) {
  240. dst[4 * j + 0] = buf[i] >> 6;
  241. dst[4 * j + 1] = buf[i] >> 4 & 3;
  242. dst[4 * j + 2] = buf[i] >> 2 & 3;
  243. dst[4 * j + 3] = buf[i] & 3;
  244. row_pix += 4;
  245. if (row_pix >= avctx->width) {
  246. i += avpkt_stride - (i % avpkt_stride) - 1;
  247. j += 4 - (j % 4) - 1;
  248. row_pix = 0;
  249. }
  250. }
  251. } else {
  252. av_assert0(avctx->bits_per_coded_sample == 1);
  253. for (i = 0, j = 0; 8 * j + 7 < buf_size && i<avpkt->size; i++, j++) {
  254. dst[8 * j + 0] = buf[i] >> 7;
  255. dst[8 * j + 1] = buf[i] >> 6 & 1;
  256. dst[8 * j + 2] = buf[i] >> 5 & 1;
  257. dst[8 * j + 3] = buf[i] >> 4 & 1;
  258. dst[8 * j + 4] = buf[i] >> 3 & 1;
  259. dst[8 * j + 5] = buf[i] >> 2 & 1;
  260. dst[8 * j + 6] = buf[i] >> 1 & 1;
  261. dst[8 * j + 7] = buf[i] & 1;
  262. row_pix += 8;
  263. if (row_pix >= avctx->width) {
  264. i += avpkt_stride - (i % avpkt_stride) - 1;
  265. j += 2 - (j % 2) - 1;
  266. row_pix = 0;
  267. }
  268. }
  269. }
  270. linesize_align = 16;
  271. buf = dst;
  272. } else if (context->is_lt_16bpp) {
  273. uint8_t *dst = frame->buf[0]->data;
  274. int packed = (avctx->codec_tag & 0xFFFFFF) == MKTAG('B','I','T', 0);
  275. int swap = avctx->codec_tag >> 24;
  276. if (packed && swap) {
  277. av_fast_padded_malloc(&context->bitstream_buf, &context->bitstream_buf_size, buf_size);
  278. if (!context->bitstream_buf)
  279. return AVERROR(ENOMEM);
  280. if (swap == 16)
  281. context->bbdsp.bswap16_buf(context->bitstream_buf, (const uint16_t*)buf, buf_size / 2);
  282. else if (swap == 32)
  283. context->bbdsp.bswap_buf(context->bitstream_buf, (const uint32_t*)buf, buf_size / 4);
  284. else
  285. return AVERROR_INVALIDDATA;
  286. buf = context->bitstream_buf;
  287. }
  288. if (desc->flags & AV_PIX_FMT_FLAG_BE)
  289. scale16be(avctx, dst, buf, buf_size, packed);
  290. else
  291. scale16le(avctx, dst, buf, buf_size, packed);
  292. buf = dst;
  293. } else if (need_copy) {
  294. memcpy(frame->buf[0]->data, buf, buf_size);
  295. buf = frame->buf[0]->data;
  296. }
  297. if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
  298. avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
  299. buf += buf_size - context->frame_size;
  300. len = context->frame_size - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
  301. if (buf_size < len && ((avctx->codec_tag & 0xFFFFFF) != MKTAG('B','I','T', 0) || !need_copy)) {
  302. av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected frame_size %d\n", buf_size, len);
  303. av_buffer_unref(&frame->buf[0]);
  304. return AVERROR(EINVAL);
  305. }
  306. if ((res = av_image_fill_arrays(frame->data, frame->linesize,
  307. buf, avctx->pix_fmt,
  308. avctx->width, avctx->height, 1)) < 0) {
  309. av_buffer_unref(&frame->buf[0]);
  310. return res;
  311. }
  312. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  313. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
  314. NULL);
  315. if (pal) {
  316. av_buffer_unref(&context->palette);
  317. context->palette = av_buffer_alloc(AVPALETTE_SIZE);
  318. if (!context->palette) {
  319. av_buffer_unref(&frame->buf[0]);
  320. return AVERROR(ENOMEM);
  321. }
  322. memcpy(context->palette->data, pal, AVPALETTE_SIZE);
  323. frame->palette_has_changed = 1;
  324. }
  325. }
  326. if ((avctx->pix_fmt==AV_PIX_FMT_BGR24 ||
  327. avctx->pix_fmt==AV_PIX_FMT_GRAY8 ||
  328. avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
  329. avctx->pix_fmt==AV_PIX_FMT_RGB555BE ||
  330. avctx->pix_fmt==AV_PIX_FMT_RGB565LE ||
  331. avctx->pix_fmt==AV_PIX_FMT_MONOWHITE ||
  332. avctx->pix_fmt==AV_PIX_FMT_PAL8) &&
  333. FFALIGN(frame->linesize[0], linesize_align) * avctx->height <= buf_size)
  334. frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
  335. if (avctx->pix_fmt == AV_PIX_FMT_NV12 && avctx->codec_tag == MKTAG('N', 'V', '1', '2') &&
  336. FFALIGN(frame->linesize[0], linesize_align) * avctx->height +
  337. FFALIGN(frame->linesize[1], linesize_align) * ((avctx->height + 1) / 2) <= buf_size) {
  338. int la0 = FFALIGN(frame->linesize[0], linesize_align);
  339. frame->data[1] += (la0 - frame->linesize[0]) * avctx->height;
  340. frame->linesize[0] = la0;
  341. frame->linesize[1] = FFALIGN(frame->linesize[1], linesize_align);
  342. }
  343. if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
  344. (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)) {
  345. frame->buf[1] = av_buffer_ref(context->palette);
  346. if (!frame->buf[1]) {
  347. av_buffer_unref(&frame->buf[0]);
  348. return AVERROR(ENOMEM);
  349. }
  350. frame->data[1] = frame->buf[1]->data;
  351. }
  352. if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
  353. ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
  354. frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
  355. if (context->flip)
  356. flip(avctx, frame);
  357. if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2') ||
  358. avctx->codec_tag == MKTAG('Y', 'V', '1', '6') ||
  359. avctx->codec_tag == MKTAG('Y', 'V', '2', '4') ||
  360. avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
  361. FFSWAP(uint8_t *, frame->data[1], frame->data[2]);
  362. if (avctx->codec_tag == AV_RL32("I420") && (avctx->width+1)*(avctx->height+1) * 3/2 == buf_size) {
  363. frame->data[1] = frame->data[1] + (avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height;
  364. frame->data[2] = frame->data[2] + ((avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height)*5/4;
  365. }
  366. if (avctx->codec_tag == AV_RL32("yuv2") &&
  367. avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
  368. int x, y;
  369. uint8_t *line = frame->data[0];
  370. for (y = 0; y < avctx->height; y++) {
  371. for (x = 0; x < avctx->width; x++)
  372. line[2 * x + 1] ^= 0x80;
  373. line += frame->linesize[0];
  374. }
  375. }
  376. if (avctx->field_order > AV_FIELD_PROGRESSIVE) { /* we have interlaced material flagged in container */
  377. frame->interlaced_frame = 1;
  378. if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
  379. frame->top_field_first = 1;
  380. }
  381. *got_frame = 1;
  382. return buf_size;
  383. }
  384. static av_cold int raw_close_decoder(AVCodecContext *avctx)
  385. {
  386. RawVideoContext *context = avctx->priv_data;
  387. av_buffer_unref(&context->palette);
  388. return 0;
  389. }
  390. AVCodec ff_rawvideo_decoder = {
  391. .name = "rawvideo",
  392. .long_name = NULL_IF_CONFIG_SMALL("raw video"),
  393. .type = AVMEDIA_TYPE_VIDEO,
  394. .id = AV_CODEC_ID_RAWVIDEO,
  395. .priv_data_size = sizeof(RawVideoContext),
  396. .init = raw_init_decoder,
  397. .close = raw_close_decoder,
  398. .decode = raw_decode,
  399. .priv_class = &rawdec_class,
  400. .capabilities = AV_CODEC_CAP_PARAM_CHANGE,
  401. };