yuv4mpeg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 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. #include "avformat.h"
  22. #define Y4M_MAGIC "YUV4MPEG2"
  23. #define Y4M_FRAME_MAGIC "FRAME"
  24. #define Y4M_LINE_MAX 256
  25. struct frame_attributes {
  26. int interlaced_frame;
  27. int top_field_first;
  28. };
  29. #if CONFIG_YUV4MPEGPIPE_MUXER
  30. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  31. {
  32. AVStream *st;
  33. int width, height;
  34. int raten, rated, aspectn, aspectd, n;
  35. char inter;
  36. const char *colorspace = "";
  37. st = s->streams[0];
  38. width = st->codec->width;
  39. height = st->codec->height;
  40. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  41. aspectn = st->sample_aspect_ratio.num;
  42. aspectd = st->sample_aspect_ratio.den;
  43. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  44. inter = 'p'; /* progressive is the default */
  45. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  46. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  47. }
  48. switch(st->codec->pix_fmt) {
  49. case PIX_FMT_GRAY8:
  50. colorspace = " Cmono";
  51. break;
  52. case PIX_FMT_YUV411P:
  53. colorspace = " C411 XYSCSS=411";
  54. break;
  55. case PIX_FMT_YUV420P:
  56. colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
  57. break;
  58. case PIX_FMT_YUV422P:
  59. colorspace = " C422 XYSCSS=422";
  60. break;
  61. case PIX_FMT_YUV444P:
  62. colorspace = " C444 XYSCSS=444";
  63. break;
  64. }
  65. /* construct stream header, if this is the first frame */
  66. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  67. Y4M_MAGIC,
  68. width,
  69. height,
  70. raten, rated,
  71. inter,
  72. aspectn, aspectd,
  73. colorspace);
  74. return n;
  75. }
  76. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  77. {
  78. AVStream *st = s->streams[pkt->stream_index];
  79. ByteIOContext *pb = s->pb;
  80. AVPicture *picture;
  81. int* first_pkt = s->priv_data;
  82. int width, height, h_chroma_shift, v_chroma_shift;
  83. int i, m;
  84. char buf2[Y4M_LINE_MAX+1];
  85. char buf1[20];
  86. uint8_t *ptr, *ptr1, *ptr2;
  87. picture = (AVPicture *)pkt->data;
  88. /* for the first packet we have to output the header as well */
  89. if (*first_pkt) {
  90. *first_pkt = 0;
  91. if (yuv4_generate_header(s, buf2) < 0) {
  92. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  93. return AVERROR(EIO);
  94. } else {
  95. put_buffer(pb, buf2, strlen(buf2));
  96. }
  97. }
  98. /* construct frame header */
  99. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  100. put_buffer(pb, buf1, strlen(buf1));
  101. width = st->codec->width;
  102. height = st->codec->height;
  103. ptr = picture->data[0];
  104. for(i=0;i<height;i++) {
  105. put_buffer(pb, ptr, width);
  106. ptr += picture->linesize[0];
  107. }
  108. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  109. // Adjust for smaller Cb and Cr planes
  110. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  111. width >>= h_chroma_shift;
  112. height >>= v_chroma_shift;
  113. ptr1 = picture->data[1];
  114. ptr2 = picture->data[2];
  115. for(i=0;i<height;i++) { /* Cb */
  116. put_buffer(pb, ptr1, width);
  117. ptr1 += picture->linesize[1];
  118. }
  119. for(i=0;i<height;i++) { /* Cr */
  120. put_buffer(pb, ptr2, width);
  121. ptr2 += picture->linesize[2];
  122. }
  123. }
  124. put_flush_packet(pb);
  125. return 0;
  126. }
  127. static int yuv4_write_header(AVFormatContext *s)
  128. {
  129. int* first_pkt = s->priv_data;
  130. if (s->nb_streams != 1)
  131. return AVERROR(EIO);
  132. if (s->streams[0]->codec->codec_id != CODEC_ID_RAWVIDEO) {
  133. av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  137. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  138. }
  139. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  140. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  141. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  142. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  143. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
  144. return AVERROR(EIO);
  145. }
  146. *first_pkt = 1;
  147. return 0;
  148. }
  149. AVOutputFormat yuv4mpegpipe_muxer = {
  150. "yuv4mpegpipe",
  151. NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  152. "",
  153. "y4m",
  154. sizeof(int),
  155. CODEC_ID_NONE,
  156. CODEC_ID_RAWVIDEO,
  157. yuv4_write_header,
  158. yuv4_write_packet,
  159. .flags = AVFMT_RAWPICTURE,
  160. };
  161. #endif
  162. /* Header size increased to allow room for optional flags */
  163. #define MAX_YUV4_HEADER 80
  164. #define MAX_FRAME_HEADER 80
  165. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  166. {
  167. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  168. char *tokstart,*tokend,*header_end;
  169. int i;
  170. ByteIOContext *pb = s->pb;
  171. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
  172. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  173. AVStream *st;
  174. struct frame_attributes *s1 = s->priv_data;
  175. for (i=0; i<MAX_YUV4_HEADER; i++) {
  176. header[i] = get_byte(pb);
  177. if (header[i] == '\n') {
  178. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  179. header[i+2] = 0;
  180. break;
  181. }
  182. }
  183. if (i == MAX_YUV4_HEADER) return -1;
  184. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  185. s1->interlaced_frame = 0;
  186. s1->top_field_first = 0;
  187. header_end = &header[i+1]; // Include space
  188. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  189. if (*tokstart==0x20) continue;
  190. switch (*tokstart++) {
  191. case 'W': // Width. Required.
  192. width = strtol(tokstart, &tokend, 10);
  193. tokstart=tokend;
  194. break;
  195. case 'H': // Height. Required.
  196. height = strtol(tokstart, &tokend, 10);
  197. tokstart=tokend;
  198. break;
  199. case 'C': // Color space
  200. if (strncmp("420jpeg",tokstart,7)==0)
  201. pix_fmt = PIX_FMT_YUV420P;
  202. else if (strncmp("420mpeg2",tokstart,8)==0)
  203. pix_fmt = PIX_FMT_YUV420P;
  204. else if (strncmp("420paldv", tokstart, 8)==0)
  205. pix_fmt = PIX_FMT_YUV420P;
  206. else if (strncmp("411", tokstart, 3)==0)
  207. pix_fmt = PIX_FMT_YUV411P;
  208. else if (strncmp("422", tokstart, 3)==0)
  209. pix_fmt = PIX_FMT_YUV422P;
  210. else if (strncmp("444alpha", tokstart, 8)==0) {
  211. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  212. return -1;
  213. } else if (strncmp("444", tokstart, 3)==0)
  214. pix_fmt = PIX_FMT_YUV444P;
  215. else if (strncmp("mono",tokstart, 4)==0) {
  216. pix_fmt = PIX_FMT_GRAY8;
  217. } else {
  218. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  219. return -1;
  220. }
  221. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  222. break;
  223. case 'I': // Interlace type
  224. switch (*tokstart++){
  225. case '?':
  226. break;
  227. case 'p':
  228. s1->interlaced_frame=0;
  229. break;
  230. case 't':
  231. s1->interlaced_frame=1;
  232. s1->top_field_first=1;
  233. break;
  234. case 'b':
  235. s1->interlaced_frame=1;
  236. s1->top_field_first=0;
  237. break;
  238. case 'm':
  239. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  240. return -1;
  241. default:
  242. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  243. return -1;
  244. }
  245. break;
  246. case 'F': // Frame rate
  247. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  248. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  249. break;
  250. case 'A': // Pixel aspect
  251. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  252. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  253. break;
  254. case 'X': // Vendor extensions
  255. if (strncmp("YSCSS=",tokstart,6)==0) {
  256. // Older nonstandard pixel format representation
  257. tokstart+=6;
  258. if (strncmp("420JPEG",tokstart,7)==0)
  259. alt_pix_fmt=PIX_FMT_YUV420P;
  260. else if (strncmp("420MPEG2",tokstart,8)==0)
  261. alt_pix_fmt=PIX_FMT_YUV420P;
  262. else if (strncmp("420PALDV",tokstart,8)==0)
  263. alt_pix_fmt=PIX_FMT_YUV420P;
  264. else if (strncmp("411",tokstart,3)==0)
  265. alt_pix_fmt=PIX_FMT_YUV411P;
  266. else if (strncmp("422",tokstart,3)==0)
  267. alt_pix_fmt=PIX_FMT_YUV422P;
  268. else if (strncmp("444",tokstart,3)==0)
  269. alt_pix_fmt=PIX_FMT_YUV444P;
  270. }
  271. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  272. break;
  273. }
  274. }
  275. if ((width == -1) || (height == -1)) {
  276. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  277. return -1;
  278. }
  279. if (pix_fmt == PIX_FMT_NONE) {
  280. if (alt_pix_fmt == PIX_FMT_NONE)
  281. pix_fmt = PIX_FMT_YUV420P;
  282. else
  283. pix_fmt = alt_pix_fmt;
  284. }
  285. if (raten == 0 && rated == 0) {
  286. // Frame rate unknown
  287. raten = 25;
  288. rated = 1;
  289. }
  290. if (aspectn == 0 && aspectd == 0) {
  291. // Pixel aspect unknown
  292. aspectd = 1;
  293. }
  294. st = av_new_stream(s, 0);
  295. if(!st)
  296. return AVERROR(ENOMEM);
  297. st->codec->width = width;
  298. st->codec->height = height;
  299. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  300. av_set_pts_info(st, 64, rated, raten);
  301. st->codec->pix_fmt = pix_fmt;
  302. st->codec->codec_type = CODEC_TYPE_VIDEO;
  303. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  304. st->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  305. return 0;
  306. }
  307. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  308. {
  309. int i;
  310. char header[MAX_FRAME_HEADER+1];
  311. int packet_size, width, height, ret;
  312. AVStream *st = s->streams[0];
  313. struct frame_attributes *s1 = s->priv_data;
  314. for (i=0; i<MAX_FRAME_HEADER; i++) {
  315. header[i] = get_byte(s->pb);
  316. if (header[i] == '\n') {
  317. header[i+1] = 0;
  318. break;
  319. }
  320. }
  321. if (s->pb->error)
  322. return s->pb->error;
  323. else if (s->pb->eof_reached)
  324. return AVERROR_EOF;
  325. else if (i == MAX_FRAME_HEADER)
  326. return AVERROR_INVALIDDATA;
  327. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
  328. return AVERROR_INVALIDDATA;
  329. width = st->codec->width;
  330. height = st->codec->height;
  331. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  332. if (packet_size < 0)
  333. return packet_size;
  334. ret = av_get_packet(s->pb, pkt, packet_size);
  335. if (ret < 0)
  336. return ret;
  337. else if (ret != packet_size)
  338. return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
  339. if (s->streams[0]->codec->coded_frame) {
  340. s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  341. s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
  342. }
  343. pkt->stream_index = 0;
  344. return 0;
  345. }
  346. static int yuv4_probe(AVProbeData *pd)
  347. {
  348. /* check file header */
  349. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  350. return AVPROBE_SCORE_MAX;
  351. else
  352. return 0;
  353. }
  354. #if CONFIG_YUV4MPEGPIPE_DEMUXER
  355. AVInputFormat yuv4mpegpipe_demuxer = {
  356. "yuv4mpegpipe",
  357. NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  358. sizeof(struct frame_attributes),
  359. yuv4_probe,
  360. yuv4_read_header,
  361. yuv4_read_packet,
  362. .extensions = "y4m"
  363. };
  364. #endif